[PHP-DB] Stuck on Couldn't execute query error
Hi: I am working on a computer with RedHat Linux 7.2 I am running Apache web server and MySQL and Netscape browser I am working local as the root I am working through exercises from the book PHP Essentials 1st edition by Julie C Meloni. I am having difficulty with an exercise in chapter 4 regarding Create a Database Table. I was receiving a Couldn't connect to server. in Netscape browser. I edited script error and now I am stuck with Couldn't execute query. So I guess I am able to connect to the server and the database, but not execute the query. The following is the do_creatable.php script: !DOCTYPE HTML PUBLIC -//W3C//DTD HTML 3.2//EN HTML HEAD TITLECreate a Database Table: Step 3/TITLE/HEAD BODY h1Adding table ?php echo $table_name; ?/h1 ?php $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 PCouldn't create table!; } else { echo P$table_name has been created!; } ? /BODY /HTML Any suggestions? Thanks in advance. Vip _ Get a FREE online virus check for your PC here, from McAfee. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Stuck on Couldn't execute query error
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 PCouldn'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