One: Link database
mysql_connect die ("Connection database Failed"); mysql_pconnect(); // Persistent Links
Two: Select the database:
mysql_select_db ("MySQLdb") or Die ("Database selection failed");
Three: Splicing database
$sql= "SELECT * From ...";
Four: Execution database:
$result=$mysql _query($sql);
Five: Judging whether it is wrong:
1. Mysql_errno (); Error number// 2.mysql_error(); error message if(! Result) { echo "1. Error number, 2. Error message"; exit; }
Six: Remove the record from the result set:
1. Non-select:
A. Get the number of lines that grow automatically:
mysql_insert_id // INSERT INTO (only used in tables with autogrow)
B. Getting the number of rows affected:
mysql_affected_rows (); // you can tell if the record changes, change the value, do not change the value if (mysql_affected_rows() > 0"execution success <br>"; }Else{ echo "does not change record <br>"; }
2.select: Remove the record from the result set:
Mysql_fetch_row ($result); // returns an indexed array of Mysql_fetch_assoc($result); // returns an associative array (subscript: is the list) Mysql_fetch_array ($result); // returns an index and associates an array of two Mysql_fetch_object ($result); // returns a record as an object
while($data=Mysql_fetch_row($result)) {Print_r($data); Echo"<br>"; } while($data=Mysql_fetch_assoc($result)) {Print_r($data); Echo"<br>"; }Mysql_data_seek($result, 3);//move the pointer, starting from the third one; while($data=Mysql_fetch_object($result)) {Print_r($data); Echo"<br>"; }
Echo"<table align= ' center ' width= ' border= ' 1 ' >"; Echo"<caption>; while($row=Mysql_fetch_assoc($result)){ Echo"<tr>"; foreach($row as $val){ Echo"<td>".$val." </td> "; Echo"</tr>"; }Echo"</table>";
3. Get field information:
A. Getting the information for a column:
Mysql_num_fields ($result); // number of columns; mysql_num_rows ($result); // number of rows; Mysql_field_name ($result,$i// Gets the name of the data field; $result: result set $i : The first few columns
Eight: Release result set:
Mysql_free_resutl ($result);
$link=mysql_connect("localhost", "root", "") or die("No Value selection"); mysql_select_db("Sqldb") or die("No database you want to select"); $sql= "SELECT * FROM Products"; $result=mysql_query($sql); if(!$result){ Echo Mysql_errno()." <br> ".Mysql_error(); } Echo"<table align= ' center ' border= ' 1 ' >"; for($i= 0;$i<mysql_num_rows($result);$i++){ Echo"<th>" [email protected]Mysql_field_name($result,$i)." </th> "; } while($row=Mysql_fetch_assoc($result)){ Echo"<tr>"; foreach($row as $val){ Echo"<td>".$val." </td> "; } Echo"</tr>"; } Echo"<td colspan= ' 7 ' align= ' Right ' > column:".Mysql_num_fields($result)." Line: ".mysql_num_rows($result)." </td> "; Echo"<table>";
Nine: Close connection:
Close ()