search
top

Bug found in ‘createdb.php’ script

Hello Joy of PHP readers,

An asture reader has pointed out a bug in one of my scripts and, frankly, it was so embarassing that I just had to share it with you all. I’d like to think it’s a mistake any beginner can make, and as such I want to share it with you so that you don’t learn the wrong things.

The mistake I made was related to using PHP to interact with the mySQL database.  There are actually two different libraries for interacting with mySQL and those two libraries are 1) mysql and 2) mysq1i

The mysql library was the original way to access mySQL using PHP, but along the way some smart people figured out a better way to do it, and came out with the mysqli (for mysql – improved) library.  In general, the mysqli is the preferred way to access mySQL from PHP now.  Here’s a link to more documentation about mysqli

At page 84 of the book (Kindle readers can search for th phrase ‘Code Listing: createdb.php’ since Kindle doesn’t have page numbers) you will find a script that creates the database which stores information about the cars.  The mistake I made was to use mysqli to create the database, but I used mysql to display any errors.  You can’t mix mysql and mysqli in the same script, you have to pick.

In particular lines 9 – 12 worked fine if your username and password were correct, but didn’t really trap the error if there was a mistake. Again, the problem was incorrectly mixing mysqli and mysql.  The corrected code is shown below.

Original Code:


<?php
/**
* Joy of PHP sample code
* Demonstrates how to create a database, create a table, and insert records.
*/

$mysqli = new mysqli('localhost', 'root', 'mypassword' );

if (!$mysqli) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully to mySQL. <BR>';
/* Create table doesn't return a resultset */
if ($mysqli->query("CREATE DATABASE Cars") === TRUE) {
 echo "<p>Database Cars created</P>";
}
else
{
echo "Error creating Cars database: " . mysql_error()."<br>";
}

Corrected Code


<!--?php <br ?-->
/**
* Joy of PHP sample code
* Demonstrates how to create a database, create a table, and insert records.
*/

$mysqli = new mysqli('localhost', 'root', 'mypassword' );

if (mysqli_connect_error()) {
die('Could not connect: ' . mysqli_connect_error());
echo 'Connected successfully to mySQL.';

/* Create table doesn't return a resultset */

if ($mysqli->query("CREATE DATABASE Cars") === TRUE) {

echo "Database Cars created";
}
else
{
echo "Error creating Cars database: " . $mysqli->error."<br>";
}

A special thanks to Matteo Sisti Sette for taking the time to point this out to me!  I appreciate it.

I will fix this in the book and make a few other changes too.  Look for a second edition in approximately the next 30 days or so.

Happy coding,

Alan

2 Responses to “Bug found in ‘createdb.php’ script”

  1. GD90 says:

    Hello, something is really wrong with my code. When I try the createdb.php in my browser – it just repeats the whole code back to me.
    So I came here and saw this, corrected it, but still no luck 🙁

    My first guess was that I don’t have a clue to what my password for phpmyadmin is – or how to find out…
    But since it doesn’t tell me that there was an error connecting, I think I’m in much deeper trouble.

    I hope you’re going to reply – and I’ll be happy to send you my code 🙂

    Thanks in advance.

    • GD90 says:

      Whoops!
      The newbie that I am – I was running the script directly from Notepad++… Instead of going to localhost/createdb.php

      Now I just need to find my password for phpmyadmin, but that can’t be too hard 😛

Leave a Reply

top