Cannot connect to MySQL

I’m trying to connect to MySQL with PHP (IIS 10, Win 10, MySQL5.7) using data from an O’Reilly book. Following the opening php tag I have:-
require_once ‘login.php’;
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die ("Unable to connect: " . mysql.error());
The usename is the first 4 letters of my name (obtained from the mysql ini file) and the password is the same one I use if using MySQL via the command line. Hostname is ‘localhost’.
The if…die statement doesn’t seem to be reached. Instead I get:-
PHP Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\inetpub\wwwroot\php\sqlquery.php:3 Stack trace: #0 {main} thrown in C:\inetpub\wwwroot\php\sqlquery.php on line 3
As this is my first attempt at PHP/MySQL (hence very green!), and the book doesn’t cover it, I’m hoping someone can point me in the right direction??
Many thanks in anticipation.

The book is way too old to cover that situation then. You have not mentioned which PHP version you use, but i guess it’s something like 7.0 or above, but as you can read from the manual

This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.

I don’t know what the rest of the book says, but i would recommend to buy a new one that covers a PHP version of 7.0 and above, or use online tutorials. You can start here:

Hmm Checking my book it’s, as you say, way out of date! And yes PHP 7.3. Looks like I’m gonna be spending money on a book (again!).
Thanks v much for your reply and the web link.

Maybe save some money and check out php resources such as on w3 schools, pretty comprehensive

$servername = “localhost”;
$username = “username”;
$password = “password”;

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
echo "Connected successfully";

Thanks for reply and yes, that sounds better.
Sorry for the delay in response - I’m in Bali and access to the web is not always good.
I tried you code (the newer mysqli bits) and that works so I am up and running!
Again, many thanks

Heh - I’m in Egypt - so I understand ! Happy to help my friend - good luck!

Worth mentioning here:

  • Most mysql_ functions have an equivalent mysqli_ function, but you sometimes have to define the connection.
  • mysql_pconnect() would be handled by mysqli_connect() instead of having its own function.
  • mysql_result() has no equivalent in mysqli_, so converting older scripts using this function may cause a bit of trouble. Luckily you can re-implement it using some code from the PHP docs’ comments on the mysqli::result page.
  • mysql_connect() returns a boolean, mysqli_connect() returns an object. (If I remember rightly.)

Thanks for this. I am just discovering the mysqli changes. I’ve got the connection working, but still a way for me to go as it’s all new.
Thanks again for your help.

Sponsor our Newsletter | Privacy Policy | Terms of Service