Here is my query to create a table using mysql and php.
<?php
$con=mysql_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysql_connect())
{
echo "Failed to connect to MySQL: " . mysql_error();
}
// Create table
$sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT,)";
// Execute query
if (mysql_query($con,$sql))
{
echo "Table persons created successfully";
}
else
{
echo "Error creating table: " . mysql_error($con);
}
?>
I am getting the Error as Check your Syntax
How can I resolve this error ?
You are using ,
after INT
$sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT,)";
use as
$sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT)";
And if you remove your error is solved.
I recommend you to use mysqli
and not to use mysql
. Because this extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used.
Source : http://www.php.net/manual/en/intro.mysql.php
Here is the mysqli
code for you.
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create table
$sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT)";
// Execute query
if (mysqli_query($con,$sql))
{
echo "Table persons created successfully";
}
else
{
echo "Error creating table: " . mysqli_error($con);
}
?>
$sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT,)";
replace it with
$sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT)";
I have removed a ',' after last INT :)
You have an extra ,
in the end of the query. Change query to
$sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT)";
you have added extra comma(,) in last of your query
so try to change
$sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT,)";
to
$sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT)";
for more about create table query http://www.w3schools.com/sql/sql_create_table.asp
also if (mysql_connect())
to if (!$con)
your if (mysql_connect()) will be always true if connection found and will echo condition result
There's an extra comma.
$sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT,)";
Change it to:
$sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT)";
you need to change this condition:
if (mysql_connect())
{
echo "Failed to connect to MySQL: " . mysql_error();
}
to:
if (!$con)
{
echo "Failed to connect to MySQL: " . mysql_error();
}
and change your sql to :
$sql="CREATE TABLE student(Name CHAR(30),Email CHAR(30),Age INT)"; //remove your last `,`
Try the query as given below:
CREATE TABLE `student` (
`Name` VARCHAR( 200 ) NOT NULL ,
`Email` VARCHAR( 200 ) NOT NULL ,
`Age` INT( 11 ) NOT NULL
) ENGINE = INNODB;
Please specify clearly when create table.