Database simple additions and deletions to review

Source: Internet
Author: User

<?php
@ $con = mysql_connect (' localhost ', ' zsg ', ' 123456 ');

if (! $con) {
Die (' Could not connect: '. Mysql_error ());
}
mysql_select_db ("my_db", $con);
Create a database
/* IF (mysql_query ("CREATE Database my_db", $con)) {
echo "Database created";
}else{
echo "Error Creating Database:". Mysql_error ();
} */

Creating a data Table CREATE table
The primary key field is always indexed, there is no exception to this rule, and you must index the primary key field. This allows the database engine to quickly locate the rows that are given the converted values and to add a not NULL setting for the primary key.
/* mysql_select_db ("my_db", $con);
$sql = "CREATE TABLE Persons (
Id Int (one) primary key auto_increment,
Firstname varchar (15),
Lastname varchar (15),
Age Int (11)
)";
if (mysql_query ($sql, $con)) {
echo "TABLE Persons created successfully";
} */

Inserting a new record into the database
/* $query = mysql_query ("INSERT into Persons (firstname,lastname,age) VALUES (' Zhang San ', ' John Doe ', ' 5 ')");
if ($query) {
echo "successfully inserted data";
} */

Querying data into a database, outputting it in tabular form
//
Handling of result
Where to specify a condition query
Order by sort
$result = mysql_query ("SELECT * from Persons ORDER by age ASC");
Var_dump ($result);
Echo ' <center><table border= ' "width=" 300px "height=" 200px "style=" text-align:center; Background-color:yellow; " > ';
echo "<tr><th>Firstname</th><th>Lastname</th><th>Age</th></tr>" ;
while ($row = Mysql_fetch_array ($result)) {
echo "<tr>";
echo "<td>". $row [' Firstname ']. " </td> ";
echo "<td>". $row [' Lastname ']. " </td> ";
echo "<td>". $row [' age ']. " </td> ";
echo "</tr>";
}
Echo ' </table></center> ';


To update a database
/* $sql = mysql_query ("update Persons set age = '" where Firstname = ' Zhang San ' and Lastname = ' John Doe ' ");
if ($sql) {
echo "successfully updated data";
} */
Mysql_close ($con);

Internet Information Server (IIS) Internet Information Services

Database simple additions and deletions to review

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.