Some commonly used php functions and examples

WBOY
Release: 2016-07-25 09:05:22
Original
879 people have browsed it
  1. $DB_HOST ="localhost";
  2. $DB_LOGIN ="root";
  3. $DB_PASSWORD =" 123456";
  4. $conn=mysql_connect($DB_HOST,$DB_LOGIN,$DB_PASSWORD);
Copy code

mysql_data_seek Move the inner query pointer to the query row grammar bool mysql_data_seek(resource result_indetifier,int row_number) Example:

  1. $DB_HOST ="localhost";
  2. $DB_LOGIN ="root";
  3. $DB_PASSWORD ="123456";
  4. $DB_NAME ="flag";
  5. $conn=mysql_connect($DB_HOST ,$DB_LOGIN,$DB_PASSWORD);
  6. mysql_select_db($DB_NAME);
  7. $res=mysql_query("SELECT * FROM PRODUCT");
  8. $row=mysql_fetch_array($res);
  9. for($i=0;$i< $num;$i++)
  10. $row=mysql_fetch_array($res);
  11. mysql_data_seek($res,0);//Move the pointer back to the first row of the query result
Copy code

mysql_fetch_array Store the query results in an array (each array element stores a record) grammar array mysql_fetch_array(resource result[,int result_type]) Example:

  1. $DB_HOST ="localhost";
  2. $DB_LOGIN ="root";
  3. $DB_PASSWORD ="123456";
  4. $DB_NAME ="flag";
  5. $conn=mysql_connect($DB_HOST ,$DB_LOGIN,$DB_PASSWORD);
  6. mysql_select_db($DB_NAME);
  7. $res=mysql_query("SELECT * FROM PRODUCT");
  8. $row=mysql_fetch_array($res);
Copy code

mysql_fetch _object Obtain a row of query results and store it as an object type. The usage method is exactly the same as MySQL_fetch_array(). The difference is that mysql_fetch_object() can only obtain query results through field names. echo $row->fieldname; //Correct usage echo $row->0; //wrong usage grammar object mysql_fetch_object(resource result) Example:

  1. $DB_HOST ="localhost";
  2. $DB_LOGIN ="root";
  3. $DB_PASSWORD ="123456";
  4. $DB_NAME ="flag";
  5. $conn=mysql_connect($DB_HOST ,$DB_LOGIN,$DB_PASSWORD);
  6. mysql_select_db($DB_NAME);
  7. $res=mysql_query("SELECT * FROM PRODUCT");
  8. $row=$mysql_fetch_object($res);
  9. while($row)
  10. {
  11. echo $rowàp_id;
  12. echo $rowàp_name;
  13. }
Copy code

mysql_insert_id After using the INSERT command to add a piece of information, you can use this function to obtain the unique id of the record just added. grammar int mysql_insert_id([esource link_identifier]) Example:

  1. $DB_HOST ="localhost";
  2. $DB_LOGIN ="root";
  3. $DB_PASSWORD ="123456";
  4. $DB_NAME ="flag";
  5. $conn=mysql_connect($DB_HOST ,$DB_LOGIN,$DB_PASSWORD);
  6. mysql_select_db($DB_NAME);
  7. $SQLStr"INSERT INTO produce (p_id,p_name)VALUES('','PHP book')";
  8. $res=mysql_query($res);
  9. $p_id=mysql_insert_id();
Copy code

mysql_num_rows Get how many rows there are in the query results grammar int mysql_num_rows(resource result) Example:

  1. $DB_HOST ="localhost";
  2. $DB_LOGIN ="root";
  3. $DB_PASSWORD ="123456";
  4. $DB_NAME ="flag";
  5. $conn=mysql_connect($DB_HOST ,$DB_LOGIN,$DB_PASSWORD);
  6. mysql_select_db($DB_NAME);
  7. $res=mysql_query("SELECT * FROM PRODUCT");
  8. $num=mysql_num_rows($res);
Copy code

mysql_query Send a SQL syntax query statement grammar resource mysql_query(string query[,resource link_identifier]) Example:

  1. $DB_HOST ="localhost";
  2. $DB_LOGIN ="root";
  3. $DB_PASSWORD="123456";
  4. $DB_NAME ="flag";
  5. $conn=mysql_connect($DB_HOST ,$DB_LOGIN,$DB_PASSWORD);
  6. mysql_select_db($DB_NAME);
  7. $res=mysql_query("SELECT * FROM PRODUCT");
Copy code

mysql_select_db Select the name of the database you want to access grammar bool mysql_select_db(string database_name[,resource link_identifier]) Example:

  1. $DB_HOST ="localhost";
  2. $DB_LOGIN ="root";
  3. $DB_PASSWORD ="123456";
  4. $DB_NAME ="flag";
  5. $conn=mysql_connect($DB_HOST ,$DB_LOGIN,$DB_PASSWORD);
  6. mysql_select_db($DB_NAME);
Copy code

1 2 3 Next page Last page



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!