Re: Stuck on Couldn't execute query error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux