PHP 6 -- bind MySQL variables and bind results

Source: Internet
Author: User

<? PHP
// Database connection Parameters
$ Host = "";
$ User = "";
$ Pass = "";
$ Db = "";

// Establish a connection
$ Connection = mysql_connect ($ host, $ user, $ pass) or die ("unable to connect! ");
Mysql_select_db ($ dB) or die ("unable to select dB! "); // Select a database
Mysql_set_charset ('utf8', $ connection );

$ Query = ""; // SQL statement

// Execute the SQL statement
$ Resutl = mysql_query ($ query) or die ("error in query: $ query.". mysql_error ());

// Display returned records
If (mysql_num_rows ($ result)> 0 ){
While ($ ROW = mysql_fetch_row ($ result )){
// Mysql_fetch_array () ---> row [0] or $ row ['id'], mysql_fetch_assoc () --> $ row ['name'], mysql_fetch_object () -- $ row-> ID
Echo $ row [0];
Echo $ row [1];
Echo $ row [2];
}
} Else {
Echo "no related records ";
}
// Release the memory occupied by the record
Mysql_free_result ($ result );
// Close the database connection
Mysql_close ($ connection );

List () method

$ SQL = "select ID, title, thumb, URL from 'v9 _ News' where catid = 9 ";

$ Result = mysqli_query ($ db_conn, $ SQL) or die ("err in query 3:". $ SQL. mysqli_error ());;
While (List ($ id, $ title, $ thumb, $ URL) = mysqli_fetch_row ($ result )){
Echo "<tr> \ n ".
"<TD> <a href = \" info. php? Id = $ ID \ "> $ title </a> </TD> \ n ".
"<TD> $ thumb ---- $ URL </TD> \ n ".
"</Tr> \ n ";
}

 

List (), each (), reset (), current (), next (), end (), Prev ()

Each () returnsArrayThe key/value pair of the current pointer position in the array and move the array Pointer Forward. The key-value pair is returned as an array of four units, with the key name
0,1,Key
AndValue. Unit0And
KeyKey name that contains an array unit,1And
ValueContains data.


<? PHP

$ Array= Array ('Step one','Step two','Step three','Step four');

// By default, the pointer is on the first element
EchoCurrent($ Array)."<Br/> \ n";// "Step one"

// skip two steps
next ( $ array );
next ( $ array );
echo current ( $ array ). "
\ n" ; //" Step three "

// Reset pointer, start again on step one
Reset($ Array);
EchoCurrent($ Array)."<Br/> \ n";// "Step one"

// Die () and exit (0)

?>

 

 

<? PHP

// General handling method
$ Db_host = "localhost ";
$ Db_user = "root ";
$ Db_pass = "";
$ Db_name = "Feng ";

$ Db_conn = mysqli_connect ($ db_host, $ db_user, $ db_pass, $ db_name) or die ("dblink is bad! ");
// Mysqli_select_db ($ db_name) or die ("unable to select dB! ");
$ Db_ SQL = "select * From 'newtable '";
$ Db_result = mysqli_query ($ db_conn, $ db_ SQL) or die ("err in query:". mysqli_error ());

If (mysqli_num_rows ($ db_result)> 0)
{
Echo "begin ";
While ($ ROW = mysqli_fetch_row ($ db_result ))
{
Echo $ row [0];
Echo $ row [1];
Echo $ row [3];
}
Echo "end ";
}
Else
{
Echo "No recoder ";
}
Mysqli_free_result ($ db_result );// Release the result set
Mysqli_close ($ db_conn); // release the connection

// Use the binding method to submit data and prevent SQL Injection
$ Mysqli = new mysqli ($ db_host, $ db_user, $ db_pass, $ db_name );
If (mysqli_connect_errno ())
{
Printf ("dblink is bad! % S/n ", mysqli_connect_error ());
Exit ();
}
$ Db_sql2 = "select ID, uid, regdate from 'newtable' where id =? ";
$ Stmt = $ mysqli-> prepare ($ db_sql2 );// Preprocessing
$ Stmt-> bind_param ("I", $ id); // bind a variable. The variable format is [I value, s character, d floating point type, B blobs type]

If (! Get_magic_quotes_gpc ()){
$ Id = addslashes ($ id = $ _ Get ["ID"]); // handle single quotes
// $ Id = mysql_real_escape_string ($ _ Get ["ID"]);

/* The problem with addslashes is that hackers can use 0xbf27 instead of single quotes for processing single-byte strings.
Mysql_real_escape_string can be used only when (PHP 4> = 4.3.0 PHP 5. Otherwise, only mysql_escape_string can be used. The difference between the two is:
Mysql_real_escape_string considers the current character set to be connected, while mysql_escape_string does not.
*/
} Else {
$ Id = $ _ Get ["ID"];
}
// $ Id = $ _ Get ["ID"];

$ Stmt-> execute ();
$ Stmt-> bind_result ($ col1, $ col2, $ col3); // bind the result
While ($ stmt-> fetch ())
{
Echo $ col1. "<br> ";
Echo $ col2;
Echo $ col3;
}
Echo $ db_sql2;
$ Stmt-> close ();
$ Mysqli-> 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.