Jump to content

MySQL: Problem Creating Table


OtagoHarbour

Recommended Posts

I use the following code to create a table (and database) with PHP:MySQL.<?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } else echo "Connected to MySql Database<br \>"; if (mysql_query("CREATE DATABASE IF NOT EXISTS DraculaDB",$con)) { echo "Database created<br \>"; } else { echo "Error creating database: <br \>" . mysql_error(); } // Delete table if it already exists mysql_select_db("DraculaDB", $con); mysql_query("DROP TABLE IF EXISTS Subscribers"); // Create table $sql = "CREATE TABLE IF NOT EXISTS Subscribers ( personID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(personID), FirstName varchar(25), LastName varchar(25), UserName varchar(25), EmailAddress varchar(25), PassWord varchar(25), Gender varchar(16), MailingList char(1) )"; if (!$sql) { echo "Error creating creating table: Subscribers<br \>" . mysql_error(); } else { echo "Table, Subscribers, created<br \>" . mysql_error(); } echo "Tables in DraculaDB: "; mysql_query("SHOW TABLES FROM DraculaDB"); echo "<br \>"; // Close connection mysql_close($con);?>If I use mysql_query("DROP TABLE Subscribers");instead ofmysql_query("DROP TABLE IF EXISTS Subscribers");I get a message that the table, Subscribers, does not exist no matter how many times I run the code.Also echo "Tables in DraculaDB: "; mysql_query("SHOW TABLES FROM DraculaDB"); echo "<br \>";simply returnsTables in DraculaDB: So it would appear that a table is not being created despite$sql = "CREATE TABLE IF NOT EXISTS Subscribers();"returning a non-NULL value.I would be grateful for any help in creating this table.Thanks,Peter.

Link to comment
Share on other sites

$sql = "CREATE TABLE IF NOT EXISTS Subscribers();"This creates the query string, but you forgot to execute the query for this one.
I changed that part to // Create table mysql_query("CREATE TABLE IF NOT EXISTS Subscribers ( personID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(personID), FirstName varchar(25), LastName varchar(25), UserName varchar(25), EmailAddress varchar(25), PassWord varchar(25), Gender varchar(16), MailingList char(1) )"); // mysql_query($sql); echo "Tables in DraculaDB: "; mysql_query("SHOW TABLES FROM DraculaDB"); echo "<br \>";But the output is stillTables in DraculaDB: Thanks,Peter.
Link to comment
Share on other sites

Oops. Missed that. mysql_query doesn't print anything. You need to assign the result to a variable, and then pass the variable to a function that extracts the data. Then you can think about printing. Example 1 here shows one way to do the whole process: http://www.php.net/manual/en/function.mysql-fetch-assoc.php

Link to comment
Share on other sites

Oops. Missed that. mysql_query doesn't print anything. You need to assign the result to a variable, and then pass the variable to a function that extracts the data. Then you can think about printing. Example 1 here shows one way to do the whole process: http://www.php.net/manual/en/function.mysql-fetch-assoc.php
Thanks again for your help.I figured out that the table had been created because I was able to insert a record into it w/o the table not found error I had been getting. This sequence $result = mysql_query("SHOW TABLES FROM DraculaDB"); echo "Tables in DraculaDB:" . mysql_tablename($result, 0) . "<br \>";works in that it givesTables in DraculaDB:subscriberswhich is what I want. Unfortunately it appears that mysql_tablename is deprecated for some reason.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...