I can't connect to mysql using php

Codes here !

Moderators: gesf, Michalio

Post Reply
CompleteNewb

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

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.
Strider64

Here's another good link on PDO - https://phpdelusions.net/pdo
Post Reply