1) Connect MySQL
<?php
$con = mysql_connect ("localhost", "root", "12345");
if (! $con) {
die (' Could not connect: '. Mysql_error ());
}
Mysql_close ($con);
?>
2) PHP MySQL CREATE database and table
<?php
$con =mysql_connect ("localhost", "root", "123456");
if (! $con) {
die (' Could not connect: '. Mysql_error () ' );
}
if (mysql_query ("CREATE Database my_db", $con));
{
echo "Database created";
}else
{
echo "Error Create Database: '". Mysql_error ();
}
mysql_close ($con);
?>
3) PHP MySQL CREATE database and table
<?php
$con = mysql_connect ("localhost", "root", "123456");
if (! $con) {
Die (' Could not connect: '. Mysql_error () ');
}
if (mysql_query ("Crate Database my_db", $con)) {
echo "Database careated";
}else{
echo "Error CREATE Database:". Mysql_error ();
}
mysql_select_db ("my_db", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar (15),
LastName varchar (15),
Age int
)";
mysql_query ($sql, $con);
Mysql_close ($con);
?>
3) PHP MySQL CREATE database and table
<?php
$con =mysql_connect ("localhost", "root", "123456");
if ($con) {
Die (' Could not connect: '. Mysql_error ());
}
if (mysql_query ("CREATE Database my_db", $con));
{
echo "Database created";
}else
{
echo "Error Create Database: '". Mysql_error ();
}
mysql_select_db ("my_db", $con);
mysql_query ("INSERT into Persons (FirstName, LastName, age)
VALUES (' Peter ', ' Griffin ', ' 35 ') ');
mysql_query ("Nsert into Persons (FirstName, LastName, age)
VALUES (' Glenn ', ' Quagmire ', ' 33 ') ");
Mysql_close ($con);
?>
4) PHP MySQL CREATE table
<?php
$con = mysql_connect ("Loaclhost", "root", "123456");
if (! $con) {
die (' Could not conenct: '. mysql_error ());
}
Create a database
if (mysql_query ("CREATE Database my_db", $con));
{
echo "Database created";
}else
{
echo "Error Create Database: '". Mysql_error ();
}
Select the Created Database my_db
mysql_select_db ("my_db", $con);
Create a table
mysql_select_db ("my_db", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar (15),
LastName varchar (15),
Age int
)";
mysql_query ($sql, $con);
Inserting data
$sql _in = "INSERT into Persons (FirstName, LastName, age)
VALUES
(' $_post[firstname] ', ' $_post[lastname] ', ' $_post[age] ') ";
if (!mysql_query ($sql _n, $con)) {
Die (' Error: '. mysql_error ());
}
echo "1 row added";
Mysql_close ($con);
?>
5) PHP query MySQL data
<?php
$con = mysql_connect ("localhost", "Peter", "abc123");
if (! $con)
{
Die (' Could not connect: '. Mysql_error ());
}
mysql_select_db ("my_db", $con);
$result = mysql_query ("SELECT * from Persons");
echo "<table border= ' 1 ' >
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr> ";
while ($row = Mysql_fetch_array ($result))
{
echo "<tr>";
echo "<td>". $row [' FirstName ']. "</td>";
echo "<td>". $row [' LastName ']. "</td>";
echo "</tr>";
}
echo "</table>";
Mysql_close ($con);
?>
PHP Operation MySQL Database