1

I'm trying to use php to add a table to a database I created in MAMP.

I have explored these answers here:
Cannot connect to mysql server with MAMP nor with Community Server
Connect to MySQL in MAMP

I have also tried using this code on a server, this free hosting site called. biz.nf. There I get no connection error, but the table is not created.

Really stumped here, would appreciate any advice, thanks.

<?php

$con = mysql_connect("localhost:3306", "paul", "paul");
mysql_select_db("magusblog", $con);

$table = "ENTRIES";
mysql_query("CREATE TABLE IF NOT EXISTS '$table' ( 'ID' INT NOT NULL AUTO_INCREMENT , PRIMARY KEY ( 'ID' ) )");
mysql_query("ALTER TABLE '$table' ADD 'PHOTO' TEXT NOT NULL");
mysql_query("ALTER TABLE '$table' ADD 'TITLE' TEXT NOT NULL");
mysql_query("ALTER TABLE '$table' ADD 'DATE' TEXT NOT NULL");
mysql_query("ALTER TABLE '$table' ADD 'CONTENT' TEXT NOT NULL");

?>
Community
  • 1
  • 1
  • 1
    If this is a new project, please do not use the mysql_* functions, as they have been deprecated. You should use [mysqli](http://us2.php.net/manual/en/class.mysqli.php) or [PDO](http://us2.php.net/manual/en/book.pdo.php) instead. – Patrick Q Jan 17 '14 at 17:58
  • @PatrickQ - don't you mean mysqli ? - And he's right - use mysqli instead - PDO is a bit harder, but it is also more portable - You might want to look into ORMs like idiorm, propel, or doctrine. – rm-vanda Jan 17 '14 at 17:59
  • 1
    @rm-vanda Yup. Typo. Edited. – Patrick Q Jan 17 '14 at 18:00
  • so just use mysqli_connect and mysqli_select_db instead? I just tried that and still got the same could not connect error, I'll look into PDO too. – error-error Jan 17 '14 at 18:05
  • Thanks Patrick, Mark B, Michael Budd, was banging my head against the wall. Correcting those syntax errors solved the problem, and thanks for the tip about using or die(mysql_error()) as well – error-error Jan 17 '14 at 18:15

2 Answers2

0

Was just a couple of syntax issues... see below.. just tested and successfully created the table. Let me know if any problems.

<?php

$table = "ENTRIES";
mysql_query("CREATE TABLE IF NOT EXISTS " . $table . " (ID INT NOT NULL AUTO_INCREMENT , PRIMARY KEY ( ID ) )");
mysql_query("ALTER TABLE " . $table . " ADD PHOTO TEXT NOT NULL");
mysql_query("ALTER TABLE " . $table . " ADD TITLE TEXT NOT NULL");
mysql_query("ALTER TABLE " . $table . " ADD DATE TEXT NOT NULL");
mysql_query("ALTER TABLE " . $table . " ADD CONTENT TEXT NOT NULL");
?>
0

All of your queries have syntax errors. You do NOT use ' quotes to delimit field names. If you'd bothered actually CHECKING if errors were occuring, you'd have been informed about this:

CREATE TABLE IF NOT EXISTS '$table' ( 'ID' INT NOT NULL AUTO_INCREMENT , PRIMARY KEY ( 'ID' ) )
                           ^--    ^-- ^--^---                                          ^--^-

remove ALL of the indicated quotes, on ALL of your queries. And then rewrite them as:

$result = mysql_query(...) or die(mysql_error());
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Thank you so much, correcting the syntax errors fixed the problems. Now I will never mistake a backtick for a ' again. Thanks for the tip about or die(mysql_error()) as well. I thought they would just show up in the firefox web console. – error-error Jan 17 '14 at 18:20