I can't connect to mysql using php

Codes here !

Moderators: egami, macek, gesf

Post Reply
CompleteNewb
New php-forum User
New php-forum User
Posts: 8
Joined: Thu Dec 10, 2020 3:02 pm

Thu Dec 10, 2020 3:20 pm

Here's my code

<?php


$hostname = "localhost";
$username = "rootuser";
$password = "#############";

$databaseName ="alphacrm";

$dbConnected = mysql_connect($hostname, $username, $password);

$dbSelected = mysql_select_db($databaseName, $dbConnected);

if ($dbConnected) {
echo "My SQL connected OK<br /><br />";

if ($dbSelected) {
echo "DB connected OK<br /><br />";
} else {
echo "DB connection Failed<br /><br />";
}
} else {
echo "MySQL connection failed<br /><br />";
}


?>

I know my apache, virtualhost and php is working because i can open a helloworld.php file.
And I know that mysql and my password is working because i can connect to it through my terminal, but this code just shows a blank screen. i don't even have the echo function telling me DB connection failed.

Please help

*Note that my knowledge in computer programming is very low


**I have checked on the internet and i think that my problem might be
1.Something like this

I have a hunch that the problem here is the host you granted it to, though it's really not more than an educated guess. If you grant access myuser@'127.0.0.1' or the servers actual ip address, you won't be allowed to connect using localhost as host. This is due to the fact that when "localhost" is specified as host, php will assume that you want to use a unix socket instead of network sockets, and in that context 127.0.0.1 isn't the same as localhost.

But I don't understand it and I don't even know where to start looking to learn about that. So if you think my problem is related to that, please point me in a direction where I can educated myself.

OR

2. It might be my browser blocking my script, but I don't know what to look for.
simonbrahan
php-forum Fan User
php-forum Fan User
Posts: 90
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Fri Dec 11, 2020 12:08 am

You're probably not seeing an error due to error reporting being disabled. Changing the following settings in your php.ini:

Code: Select all

error_reporting = E_ALL
display_errors = On
This will give you more information.

Note that the "mysql_" functions you are using are removed in modern versions of PHP and shouldn't be used. PDO is considered best practice for working with databases.
User avatar
Strider64
php-forum GURU
php-forum GURU
Posts: 398
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI
Contact:

Fri Dec 11, 2020 2:45 am

Here's another good link on PDO - https://phpdelusions.net/pdo
Life is a fig newton of your imagination! https://www.miniaturephotographer.com
Post Reply