PHP and MySQL

Source: Internet
Author: User
Tags php and mysql

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 ()
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.