Some common PHP functions and examples

Source: Internet
Author: User
Tags php book unique id
    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 internal query pointer to the query line syntax 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 line of the query fruit
Copy Code

Mysql_fetch_array The result of the query exists in the array (one record per array element) syntax 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 gets a row of query results, and is stored as object type, with Mysql_fetch_array () using exactly the same method, except that mysql_fetch_object () can only get query results by field name Echo $ row->fieldname; Correct usage echo $row->0; Error Usage Syntax 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 adding a piece of information using the Insert command, you can take advantage of this function to get an example of the unique ID syntax int mysql_insert_id ([esource Link_identifier]) that just added the record:

    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 query Results There are several lines of syntax 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 sends out an example of a SQL syntax query statement syntax resource mysql_query (string Query[,resource link_identifier]):

    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 database name to access syntax 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 last page

  • 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.