Re: Stuck on Couldn't execute query error

From: Date: Sun, 18 Jan 2004 16:18:33 +0000
Subject: Re: Stuck on Couldn't execute query error
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-32479@lists.php.net to get a copy of this message
VIPP - wrote:
I am working through exercises from the book PHP Essentials 1st edition by Julie C Meloni. [snip]
I edited script error and now I am stuck with Couldn't execute query.
    $sql = "CREATE TABLE $table_name (";
     for ($i = 0; $i < count($field_name); $i++) {
        $sql .= "$field_name[$i] $field_type[$i]";
        if ($field_length[$i] != "") {
            $sql .= " ($field_length[$i]),";
        } else {
            $sql .= ",";
        }
    }
    $sql = substr($sql, 0, -1);
    $sql .= ")";
    $connection = mysql_connect("hostname","username","password")
        or die("Couldn't connect to server.");
    $db = mysql_select_db("database_name", $connection)
        or die("Couldn't select database.");
    $sql_result = mysql_query($sql,$connection)
        or die("Couldn't execute query.");
    if (!$sql_result) {
        echo "<P>Couldn't create table!";
    } else {
        echo "<P>$table_name has been created!";
    }
How old is this book? That code is horrible. Instead of displaying "Couldn't execute query.", make use of mysql_error() which will actually tell you what's going on (or not going on). or die("Couldn't execute query: " . mysql_error()); If you just displayed $sql to the screen (echo $sql), you'll probably notice the error. I'm guessing that your $field_name, $field_type, etc arrays aren't formatted correctly. -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals – www.phparch.com

« previous php.db (#32479) next »