hi guys, please help me out on this issue.

i had this code to connect to mysql using php but there is no any message displayed.

nothing is displayed on the screen, whether the connection is successful or there is an error.

any ideas?

i can connect to mysql using command prompt without a problem, but through this code below nothing is displayed on the screen.

any help is greatly appreciated.

<html>
<body>


<?php

$link = mysql_connect('localhost', 'my_username', 'my_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

?>

</body>
</html> 

Dani AI

Generated

A few targeted troubleshooting steps and a short, modern test you can drop into a page will normally uncover why a connection script prints nothing.

As reported no output at all, the two most common causes are: (a) PHP is running but errors are suppressed, or (b) the PHP used by your webserver differs from the CLI/phpinfo you checked (different php.ini or missing SAPI integration). was right to point at configuration as the likely root cause, and ’s reminder about choosing a database is useful once a connection works.

Try this checklist in order:

  • Make sure you are loading the script through the webserver URL (for example, ), not via a file:// path.
  • Verify the webserver’s phpinfo() page (not the CLI output) and note the “Loaded Configuration File” and whether the MySQL/Mysqli extension appears there.
  • Turn on full error reporting temporarily to see suppressed messages (see code example below).
  • Check the webserver error log and the PHP error log right after requesting the page — those logs usually show parse errors, missing extensions, or fatal errors.
  • Confirm the file is saved with a .php extension and is inside the server’s document root.
  • If you have both CLI PHP and an Apache/Nginx PHP, compare php -v/php -i (CLI) with phpinfo() (web) to spot differences.

Add this to the top of the page while debugging:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>

If you want a small, modern connection test (mysqli or PDO is recommended over the old mysql extension), use a brief mysqli test to surface connection errors immediately.

Note: the old mysql_* extension was removed from PHP 7 onward — migrating to mysqli or PDO is recommended. Also remember to turn display_errors off again in production. Thanks to and for the diagnostic direction that led to the WAMP solution.

Recommended Answers

All 7 Replies

You didn't select the database you are going to use.. Do it like this..

<?php
$link=mysql_connect("localhost","my_username", "my_password");
if(!$link)
{
    die("Could not connect:" .mysql_error());
}

mysql_select_db("name_of_your_database"); //here goes the database selection
echo "Connected successfully!";
?>

Hope this helped.. :))))

commented: Thanks buddy :) +8

Selecting the database is optional, not required. Anyway, check your error log, to see if perhaps the mysql extension is not installed, or perhaps some other configuration errors. The code the OP provided is correct.

check sql extension care fully so it is good habit.

commented: thank you :) +8

hi guys, thanks for your replies.

there's nothing on the error log as well, I was hoping that there would be an error so there's something i could start with but i got no clue.

i'm just starting on this please bear with me, the extension we're talking about is it
the extension_dir on php.ini?

if that's the extension_dir on php.ini, i had set that one already.

i had checked the php info, i could see that mysql is loaded.

i'm really stucked, any ideas? thanks :)

any ideas?

Sorry. The code is fine, so it has to be a configuration thing, but that's difficult to guess.

commented: thanks :) +8

have u tried using double quotes instead of single quote.

hi guys, thanks for all your help.
i had installed WAMP and it works fine.
it's a bit troublesome installing the package one by one.

installing WAMP does everything. :) thanks again..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.