We have learned about connecting to the MySQL database and running queries using PHP. It is now time create a MySQL table and Inserting data into the table.Both of these two works is done by the PHP function mysql_query().
Creating Table:
We can execute a MySQL command by sending the command as a string into the PHP function mysql_query().The following code will show the process to create a table.
$query="CREATE TABLE tablename( ";
$query.="Name VARCHAR(50) ";
$query.="Age INT ";
$query.=")";
mysql_query($query) or die(mysql_error());
Inserting Into The Table:
To insert data into the table the corresponding command to insert data in MySQL is executed in the same way though mysql_query().The following code fragment demonstrate the process.
$query="INSERT INTO tablename ";
$query.="(Name,Age) ";
$query.="VALUES('name',10)";
mysql_query($query) or die( mysql_error() );
In the second line 'Name' and 'Age' are the attributes of the table created earlier.Third line determines what values you want to insert to the specific attribute.Finally the mysql_query() function executes the command.
The die function in both of the code fragment handles the fact if the query does not execute correctly.
0 Responses to "Creating MySQL Table And Inserting Data Using PHP"
Post a Comment