Jump to content

Mysql Tutorial Won't Work?


villermen

Recommended Posts

I followed the MySQL tutorial on the site...I created a database, and I created a table.When I insert data into the table, everything works fine and it won't give any errors.I have this php script so far:

<?phpmysql_connect("localhost","root","*******");mysql_query("CREATE DATABASE my_db");mysql_select_db("my_db");$sql = "CREATE TABLE Persons (personID int NOT NULL AUTO_INCREMENT,PRIMARY KEY(personID),FirstName varchar(15),LastName varchar(15),Age int)";mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')");mysql_query($sql); echo mysql_error();?>

To see if this code worked well, I opened the MySQL command line client.I used the my_db database.So far that worked.I then inserted: select * from persons;It simply returned the message: Empty setIt shouldn't be empty!Is it a fault of mine? In that case: What am I doing wrong?Please help me.

Link to comment
Share on other sites

You'll only be able to run that script once without errors (if that), if you try to run it a second time it will have an error when you try to create a database and table that are already there. Your SQL user might also not have permission to create a database. Also, you're trying to insert a record into the table before you create the table. You run the query to create the table after the query to insert data. It would also be a good idea to print out errors:

mysql_connect("localhost","root","*******") or exit(mysql_error());mysql_query("CREATE DATABASE my_db") or exit(mysql_error());mysql_select_db("my_db") or exit(mysql_error());

Link to comment
Share on other sites

Ingolme:Thanks, your right... I shouldn't have copy pasted it.I normally only use capitalized letters if my variable has more than 2 words...Justsomeguy:I know I can run this script only once, and have to remove the database afterwards, but this was just to test how to do it. I'm gonna make a script for real now. The new one doesn't create a database or table (I'll do that manually and only once). Thanks for the troubleshooting though =)Gr.Villermen

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...