Object-oriented and process-oriented connection database

Source: Internet
Author: User

Define (' dbhost ', ' localhost ');
Define (' DBUSER ', ' root ');
Define (' dbpd ', ');
Define (' MYDB ', ' Java ');



Two forms of database operations are only 1. Object-Oriented Programming
@ $mysqli =new mysqli (dbhost,dbuser,dbpd,mydb);
$mysqli->select_db (dbname); You can use this function to select a database
$name = "Qweqwe";
$query = "SELECT * from User WHERE username=". "' $name ' ";
$result = $mysqli->query ($query);
$row = $result->fetch_row ();//equivalent to the following method
$num _results=mysqli_num_rows ($result);//Returns the number of rows
for ($i =0; $i < $num _results; $i + +) {
$row = $result 2->fetch_assoc ();//Get the value of each bar in turn

}
$result->free ();//Release resources
$mysqli->close ();//Close database connection





Database Operations 2. Process-oriented programming
$mysqli 2=mysqli_connect (DBHOST,DBUSER,DBPD,MYDB);//returns a resource rather than an object
mysqli_select_db ($mysqli 2, "Sbname"); Need to pass in Mysqli2 this resource to select the database note: In process-oriented
$name 2= "Qweqwe";
$query 2= "SELECT * from User WHERE username=". "' $name 2 ' ";
$result 2=mysqli_query ($mysqli 2, $query 2);
$row 2= $mysqli _fetch_row ($result 2);//equivalent to the following method
$num _results2=mysqli_num_rows ($result 2);//Returns the number of rows
for ($i =0; $i < $num _results2; $i + +) {
$row 2= $mysqli _fetch_assoc ($result 2);
}




The optional second parameter in mysql_fetch_array () result_type is a constant that accepts the following values:Mysql_assoc, Mysql_num,and MySQL _both. This feature is for PHP 3.0.7 New Plus. The default value for this parameter is Mysql_both.

If you use Mysql_both, you will get an array that contains both associative and numeric indexes. With Mysql_assoc, only the associated index (as in Mysql_fetch_assoc () ) is used, and the mysql_num only gets the numeric index (as in mysql_fetch_row ( ) .

Object-oriented and process-oriented connection 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.