Basic operation of PHP database

Source: Internet
Author: User
Tags php database

<?php

Connecting to a database
/* $con =mysql_connect ("localhost", "root", "19960621");/or Die (' Error: '. mysql_error ());
if (! $con) {
Die ("Connection failed!". Mysql_errno ());
}else{
echo "Connection to database Success";
mysql_query ("CREATE DATABASE dbmysql", $con);
mysql_select_db ("Dbmysql", $con);
$sql = "CREATE TABLE person" (PersonID int not NULL auto_increment,firstname varchar (), LastName varchar (+), age int, PRIMARY KEY (PersonID)) ";
$str =mysql_query ($sql, $con);
echo "<br>". $str. " <br> ";
Mysql_close ($con);
}
Inserting data into the table
$con =mysql_connect ("localhost", "root", "19960621");
mysql_select_db ("Dbmysql", $con);
mysql_query ("INSERT into person (' FirstName ', ' LastName ', ' age ') VALUES (' Tom ', ' Tony ', ' 20 ')");
mysql_query ("INSERT into person (' FirstName ', ' LastName ', ' age ') VALUES (' LH ', ' h ', ' 16 ')");
mysql_query ("INSERT into person (' FirstName ', ' LastName ', ' age ') VALUES (' Jy ', ' j ', ' 24 ')");
Querying data
$con =mysql_connect ("localhost", "root", "19960621");
mysql_select_db ("Dbmysql", $con);
$result =mysql_query ("SELECT * from person");
while ($row =mysql_fetch_array ($result)) {
echo $row ["FirstName"]. "". $row ["LastName"];
echo "<br>";
}
Update data
$con =mysql_connect ("localhost", "root", "19960621");
mysql_select_db ("Dbmysql", $con);
mysql_query ("Update person set firstname= ' dx ', lastname= ' x ', ' age ' =18 where ' personID ' = 3");
Delete data
$con =mysql_connect ("localhost", "root", "19960621");
mysql_select_db ("Dbmysql", $con);
mysql_query ("Delete from person where ' personID ' = 2");
Mysql_close ($con);

Mysql_list_dbs () function ==> lists all databases in the MySQL server
$con =mysql_connect ("localhost", "root", "19960621");
$dl =mysql_list_dbs ($con);
while ($db =mysql_fetch_object ($DL)) {
echo $db->database. " <br> ";
}
Mysql_close ($con);
Mysql_num_fields () function ==> returns the number of fields in the result set
$con = mysql_connect ("localhost", "root", "19960621");
if (! $con) {
Die (' Could not connect: '. Mysql_error ());
}
$db _selected = mysql_select_db ("Dbmysql", $con);
$sql = "SELECT * from person";
$result = mysql_query ($sql, $con);
echo Mysql_num_fields ($result);//number of output columns
Mysql_close ($con);
Mysql_num_rows () function ==> returns the number of rows in the result set
$con = mysql_connect ("localhost", "root", "19960621");
if (! $con) {
Die (' Could not connect: '. Mysql_error ());
}
$db _selected = mysql_select_db ("Dbmysql", $con);
$sql = "SELECT * from person";
$result = mysql_query ($sql, $con);
echo mysql_num_rows ($result);//Total number of rows queried
Mysql_close ($con);
Mysql_affected_rows () function ==> returns the number of record rows affected by the previous MySQL operation
$con = mysql_connect ("localhost", "root", "19960621");
if (! $con) {
Die (' Could not connect: '. Mysql_error ());
}
mysql_select_db ("Dbmysql");
mysql_query ("DELETE from person WHERE PersonID = 3");
$RC = Mysql_affected_rows ();
echo "Records deleted:". $RC;
Mysql_close ($con);
Mysql_real_escape_string () function ==> escape special characters in strings used in SQL statements
Code to get the user name and password
$user = "A\R\NBC";
$pwd = "A\R\NBC";
Escape the user name for use in SQL
$user = mysql_real_escape_string ($user);
$pwd = ($PWD);
$sql = "SELECT * from Users WHERE user= '". $user. "' and password= '". $pwd. "‘";
Echo $sql; User name and password comparison
The mysql_result () function returns the value of a field in the result set
$con = mysql_connect ("localhost", "root", "19960621");
if (! $con) {
Die (' Could not connect: '. Mysql_error ());
}
$db _selected = mysql_select_db ("Dbmysql", $con);
$sql = "SELECT * from person";
$result = mysql_query ($sql, $con);
echo mysql_result ($result);
Mysql_close ($con); * *

Basic operation of PHP database

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.