php syntax

Hello I seem to be getting an error with this ive tried a few things and am getting annoyed.

$conn = mysql_connect(“localhost”, “root”, “”);
$sql = ‘CREATE TABLE default
(
X_coord INT,
Y_coord INT,
Piece varchar(30)
)’;
// Execute query
if (mysql_query($sql, $conn)){
echo “Table created successfully”;
} else {
echo 'Error creating table: ’ . mysql_error($conn);
}
}

Thanks for the help!

Is that the full code?

Do you use an IDE?


This is all the relevant code I believe. And no I just use notepad++.

function create_db() { // This should initialize a connection to the database
$conn = mysql_connect(“localhost”, “root”, “”);
$sql = ‘CREATE DATABASE IF NOT EXISTS default_board’; // Creates it if not there
if (mysql_query($sql,$conn)) {
echo “Database default_board created successfully\n”;
} else {
die('Could not create database: ’ . mysql_error());
}
mysql_close();
}
function create_table() {
$conn = mysql_connect(“localhost”, “root”, “”);
$sql = ‘CREATE TABLE default
(
X_coord INT,
Y_coord INT,
Piece varchar(30)
)’;
// Execute query
if (mysql_query($sql, $conn)){
echo “Table created successfully”;
} else {
echo 'Error creating table: ’ . mysql_error($conn);
}
}

$width = “80px”;
$height = “80px”;
$y_coord = 50;

echo “

”;

create_db();
create_table();

for ($y=8;$y!=0;–$y) {
$y_coord += $height;
$x_coord = 50;
for ($x=8;$x!=0;–$x){
//store_data() // STORES DATA IN TABLE
$x_coord += $width;
$cell_sum = $x + $y;
$remainder = $cell_sum%2;
if ($remainder == 0) $background_color = “#ffd700”;
else $background_color = “#000000”;
echo “<div style=“border: 1px solid black; background-color: $background_color; position: absolute;left: $x_coord; top: $y_coord; width: $width; height: $height;”>”;
echo “”;
}
print ("
");
}

I get this error using that code

Error creating table: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'default ( X_coord INT, Y_coord INT, Piece varchar(30) ' at line 1

Which is this query

CREATE TABLE default ( X_coord INT, Y_coord INT, Piece varchar(30) )

Because you are using the reserved word “default”

Change to this and it should work

CREATE TABLE `default` ( X_coord INT, Y_coord INT, Piece varchar(30) )

Sponsor our Newsletter | Privacy Policy | Terms of Service