1连接数据库.
//object oriented style (recommended) $mysqli = new mysqli('host','username','password','database_name');
推荐下面的方式
//Output any connection error if ($mysqli->connect_error) { die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); }
?>
2.选择多行 mysqli_fetch_assoc() : Below is the code to fetch multiple records as an associative array. The returned array holds the strings fetched from database, where the column names will be the key used to access the internal data. As you can see below, data is displayed in an HTML table.
//Output any connection error if ($mysqli->connect_error) { die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); }
//MySqli Select Query $results = $mysqli->query("SELECT id, product_code, product_desc, price FROM products");
print '<table border="1">'; while($row = $results->fetch_assoc()) { print '<tr>'; print '<td>'.$row["id"].'</td>'; print '<td>'.$row["product_code"].'</td>'; print '<td>'.$row["product_name"].'</td>'; print '<td>'.$row["product_desc"].'</td>'; print '<td>'.$row["price"].'</td>'; print '</tr>'; } print '</table>';
// Frees the memory associated with a result $results->free();
// close connection $mysqli->close(); ?>
3.选择 fetch_array() : Function returns an array of both mysqli_fetch_row and mysqli_fetch assoc merged together, it is an extended version of the mysqli_fetch_row() function and both numeric and string can be used as keys to access the data.
//Output any connection error if ($mysqli->connect_error) { die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); }
//MySqli Select Query $results = $mysqli->query("SELECT id, product_code, product_desc, price FROM products");
print '<table border="1"'; while($row = $results->fetch_array()) { print '<tr>'; print '<td>'.$row["id"].'</td>'; print '<td>'.$row["product_code"].'</td>'; print '<td>'.$row["product_name"].'</td>'; print '<td>'.$row["product_desc"].'</td>'; print '<td>'.$row["price"].'</td>'; print '</tr>';
} print '</table>';
// Frees the memory associated with a result $results->free(); // close connection $mysqli->close(); ?>
4.选择 fetch_object() : To fetch database result set as an objects, just use MySqli fetch_object(). The attributes of the object represent the names of the fields found within the result set.
//Output any connection error if ($mysqli->connect_error) { die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); }
//MySqli Select Query $results = $mysqli->query("SELECT id, product_code, product_desc, price FROM products");
print '<table border="1">'; while($row = $results->fetch_object()) { print '<tr>'; print '<td>'.$row->id.'</td>'; print '<td>'.$row->product_code.'</td>'; print '<td>'.$row->product_name.'</td>'; print '<td>'.$row->product_desc.'</td>'; print '<td>'.$row->price.'</td>'; print '</tr>'; }
print '</table>';
// close connection $mysqli->close(); ?>
5.选择单行
//Output any connection error if ($mysqli->connect_error) { die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); }
//chained PHP functions $product_name = $mysqli->query("SELECT product_name FROM products WHERE id = 1")->fetch_object()->product_name; print $product_name; //output value $mysqli->close(); ?>
6.选择行数 <?php
//Output any connection error if ($mysqli->connect_error) { die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); }
//get total number of records $results = $mysqli->query("SELECT COUNT(*) FROM users"); $get_total_rows = $results->fetch_row(); //hold total records in variable $mysqli->close(); ?>
7.选择预处理
//create a prepared statement $query = "SELECT id, product_code, product_desc, price FROM products WHERE product_code=?"; $statement = $mysqli->prepare($query);
//bind parameters for markers, where (s = string, i = integer, d = double, b = blob) $statement->bind_param('s', $search_product);
//execute query $statement->execute();
//bind result variables $statement->bind_result($id, $product_code, $product_desc, $price);
print '<table border="1">';
//fetch records while($statement->fetch()) { print '<tr>'; print '<td>'.$id.'</td>'; print '<td>'.$product_code.'</td>'; print '<td>'.$product_desc.'</td>'; print '<td>'.$price.'</td>'; print '</tr>';
} print '</table>';
//close connection $statement->close();
$query = "SELECT id, product_code, product_desc, price FROM products WHERE ID=? AND product_code=?"; $statement = $mysqli->prepare($query); $statement->bind_param('is', $search_ID, $search_product); $statement->execute(); $statement->bind_result($id, $product_code, $product_desc, $price);
print '<table border="1">'; while($statement->fetch()) { print '<tr>'; print '<td>'.$id.'</td>'; print '<td>'.$product_code.'</td>'; print '<td>'.$product_desc.'</td>'; print '<td>'.$price.'</td>'; print '</tr>';
} print '</table>';
//close connection $statement->close();
8.插入数据库
//MySqli Insert Query $insert_row = $mysqli->query("INSERT INTO products (product_code, product_name, price) VALUES($product_code, $product_name, $product_price)");
32432432 if($insert_row){ print 'Success! ID of last inserted record is : ' .$mysqli->insert_id .'<br />'; }else{ die('Error : ('. $mysqli->errno .') '. $mysqli->error); }
?>
9.插入预处理
$query = "INSERT INTO products (product_code, product_name, price) VALUES(?, ?, ?)"; $statement = $mysqli->prepare($query);
//bind parameters for markers, where (s = string, i = integer, d = double, b = blob) $statement->bind_param('sss', $product_code, $product_name, $product_price);
if($statement->execute()){ print 'Success! ID of last inserted record is : ' .$statement->insert_id .'<br />'; }else{ die('Error : ('. $mysqli->errno .') '. $mysqli->error); } $statement->close();
10.批量插入
//product 2 $product_code2 = '"'.$mysqli->real_escape_string('P2').'"'; $product_name2 = '"'.$mysqli->real_escape_string('Apple iPad 2').'"'; $product_price2 = '"'.$mysqli->real_escape_string('217').'"';
//product 3 $product_code3 = '"'.$mysqli->real_escape_string('P3').'"'; $product_name3 = '"'.$mysqli->real_escape_string('Samsung Galaxy Note').'"'; $product_price3 = '"'.$mysqli->real_escape_string('259').'"';
//Insert multiple rows $insert = $mysqli->query("INSERT INTO products(product_code, product_name, price) VALUES ($product_code1, $product_name1, $product_price1), ($product_code2, $product_name2, $product_price2), ($product_code3, $product_name3, $product_price3)");
if($insert){ //return total inserted records using mysqli_affected_rows print 'Success! Total ' .$mysqli->affected_rows .' rows added.<br />'; }else{ die('Error : ('. $mysqli->errno .') '. $mysqli->error); }
11.更新删除
//MySqli Delete Query //$results = $mysqli->query("DELETE FROM products WHERE ID=24"); if($results){ print 'Success! record updated / deleted'; }else{ print 'Error : ('. $mysqli->errno .') '. $mysqli->error;
} 12.预处理
$query = "UPDATE products SET product_name=?, product_code=? WHERE ID=?"; $statement = $mysqli->prepare($query);
//bind parameters for markers, where (s = string, i = integer, d = double, b = blob) $results = $statement->bind_param('ssi', $product_name, $product_code, $find_id);
if($results){ print 'Success! record updated'; }else{ print 'Error : ('. $mysqli->errno .') '. $mysqli->error; }
13.删除
if($results){ print 'Success! deleted one day old records'; }else{ print 'Error : ('. $mysqli->errno .') '. $mysqli->error; }