MySQL Database ADODB Function Example detailed explanation (1/3)

Source: Internet
Author: User
Tags smarty template

1. GetAll method We can use the GetAll method instead of the Execute () method, which returns the result as a two-dimensional associated data, which can be easily handled using either a foreach or a For loop statement. In addition, the array obtained by GetAll is very well matched with the Smarty template's foreach.
Let's look at the following script example:

  code is as follows copy code
<?php
include_once ("libs/adodb/adodb.inc.php");
//Create a MySQL Connection instance object
$db = newadoconnection ("MySQL");
//Open a database connection
$db->connect ("localhost", "root", "root", "library") or Die ("Unable to connect");
//Construct and execute a check Consult the
$query = "SELECT * from library";
$result = $db->getall ($query) or Die ("Error in Query: $query." $db->e Rrormsg ());
//Clear unwanted objects
$db->close ();
//You can print the contents of the array by using Print_r
//Print_r ($result); exit (0);
//Traverse recordsets. Displays the contents of the column: TITLE and author
foreach ($result as $row) {
echo $row [1]. " - " . $row [2]. "N";
}
//Get and display the number of rows returned by
Echo "n[". sizeof ($result). "Line records are returned]n";

After the GetAll () method obtains the recordset, it produces a two-dimensional array, similar to the following:
Array
(
[0] => Array
(
[0] => 14
[ID] => 14
[1] => mystic River
[title] => Mystic River
[2] => Dennis Lehane
[author] => Dennis Lehane
)
[1] => Array
(
[0] => 15
[ID] => 15
[1] => for kicks
[title] => for kicks
[2] => Dick Francis
[Author] => Dick Francis
)
Next slightly
)

We have mentioned in the array chapter that this kind of hybrid array is best suited for foreach processing. This method complements or replaces the Execute () method, especially for use when traversing the entire table of queries.
In addition, ADODB also provides a way to obtain a record: GetOne ().
2. The GetOne () method ADODB a more straightforward way to detect whether a record exists, and that is its getone ($sql) method.
This method returns the value of the 1th and 1th field name of the query record, and returns a Boolean value False if an error occurs during execution.
We can detect if this value exists:

The code is as follows Copy Code
<?php
Include_once ("libs/adodb/adodb.inc.php");
Create a MySQL Connection instance object
$db = newadoconnection ("MySQL");
Open a database connection
$db->connect ("localhost", "root", "passwd", "ADODB") or Die ("Unable to connect!");
$rs = $db->getone ("SELECT * from library WHERE id= ' $id ')";
if ($rs) {
echo ' record presence ';
}else {
Echo ' record does not exist ';
}
?>

However, the problem is that if there are more than one id= $id record in the datasheet, not only to know if there is such a record, but also to extract the record, you can use the ADODB GetRow () method.
3. GetRow () method

  code is as follows copy code
<?php
Include_once ("libs/adodb/adodb.inc.php");
//Create a MySQL Connection instance object
$db = newadoconnection ("MySQL");
//Open a database connection
$db->connect ("localhost", "root", "" passwd "," ADODB ") or Die (" Unable to connect! ");
$rs = $db->getrow ("SELECT * from library WHERE id= ' $id ')";
if (Is_array ($rs)) {
   echo ' record exists ';
   print_r ($rs);
} else {
   echo ' note Record does not exist ';
}

The

should be noted that GetOne ($sql) and GetRow ($sql) can either get a specific record or get information that the record does not exist, but if there are multiple records that match the query criteria, the two methods return only the first record, and the others are automatically discarded.
You can use the RecordCount () method in the result set method if you only get the number of rows for the query result.
4. Get the number of rows returned ADODB also provides a batch of practical features, such as providing a very useful recordcount () and FieldCount () method to return the number of records and the number of fields, respectively, in the case of queries, and the following are examples of applying these two methods.

  code is as follows copy code
<?php
Include ("libs/adodb/adodb.inc.php");
//Create a MySQL Connection instance object
$db = newadoconnection ("MySQL");
//Open a database connection
$db->connect ("localhost", "root", "" passwd "," ADODB ") or Die (" Unable to connect! ");
//Construct and execute a query
$query = "SELECT * from Library",
$result = $db->execute ($query) or Die ("Error in query: $que Ry. ". $db->errormsg ());
//Get and display the number of rows returned
Echo $result->recordcount (). "Line record is returned n";
//Get and display the number of fields returned
Echo $result->fieldcount (). "A field is returned N";
//Clea up
$db->close ();
?

We can use the Fetchfield () method to get information about the field, which contains the details of the field, including the name and type, and so on, see the script example below.

  code is as follows copy code
<?php
Include ("libs/adodb/adodb.inc.php");
//Create a MySQL Connection instance object
$db = newadoconnection ("MySQL");
//Open a database connection
$db->connect ("localhost", "root", "" passwd "," ADODB ") or Die (" Unable to connect! ");
//Construct and execute a query
$query = "SELECT * from Library",
$result = $db->execute ($query) or Die ("Error in query: $que Ry. ". $db->errormsg ());
//Get the structure information for the field in the Recordset
for ($x =0; $x < $result->fieldcount (); $x + +) {
Print_r ($result->fetchfield ($x));
}
//Clean up unwanted objects
$db->close ()
?

The structure information for the ID field is output below.
StdClass mymagicbject
(
[name] => ID
[table] => library
[def] =>
[max_length] => 3
[Not_null] => 1
[Primary_key] => 1
[Multiple_key] => 0
[Unique_key] => 0
[numeric] => 1 [blob] => 0
[type] => int
[unsigned] => 1
[Zerofill] => 0
[binary] =>
]
5. Other related methods when you execute an INSERT query, if the primary key of the table is an AutoIncrement field, you can use the ADODB insert_id () method to get the increment value that was automatically generated when the last data was inserted.

The code is as follows Copy Code
<?php
Include_once ("libs/adodb/adodb.inc.php");
Create a MySQL Connection instance object
$db = newadoconnection ("MySQL");
Open a database connection
$db->connect ("localhost", "root", "root", "ADODB") or Die ("Unable to connect!");
Construct and perform an insert insert operation
$title = $db->qstr ("Detailed explanation of PHP5 and MySQL5 Web Development Technology");
$author = $db->qstr ("Dujiang");
$query = "INSERT into library (title, author) VALUES ($title, $author)";
$result = $db->execute ($query) or Die ("Error in Query: $query." $db->errormsg ());
Display the inserted record number
if ($result) {
echo "Last inserted record ID:". $db->insert_id ();
}
To clean up unwanted objects
$db->close ();
?>

The Qstr () method in a script that filters illegal characters in a SQL query.

Home 1 2 3 last
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.