PHP Operation MySQL Database

Source: Internet
Author: User
Tags mysql create mysql create database php mysql mysql create table

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.