<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    大漠駝鈴

    置身浩瀚的沙漠,方向最為重要,希望此blog能向大漠駝鈴一樣,給我方向和指引。
    Java,Php,Shell,Python,服務(wù)器運(yùn)維,大數(shù)據(jù),SEO, 網(wǎng)站開(kāi)發(fā)、運(yùn)維,云服務(wù)技術(shù)支持,IM服務(wù)供應(yīng)商, FreeSwitch搭建,技術(shù)支持等. 技術(shù)討論QQ群:428622099
    隨筆 - 238, 文章 - 3, 評(píng)論 - 117, 引用 - 0
    數(shù)據(jù)加載中……

    PHP MysqlI操作數(shù)據(jù)庫(kù)

    1連接數(shù)據(jù)庫(kù).
    //procedural style
    $mysqli =  mysqli_connect('host','username','password','database_name');

    //object oriented style (recommended)
    $mysqli = new mysqli('host','username','password','database_name');


    推薦下面的方式

    <?php
    //Open a new connection to the MySQL server
    $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.
    <?php
    //Open a new connection to the MySQL server
    $mysqli = new mysqli('host','username','password','database_name');

    //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.
    <?php
    //Open a new connection to the MySQL server
    $mysqli = new mysqli('host','username','password','database_name');

    //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.
    <?php
    //Open a new connection to the MySQL server
    $mysqli = new mysqli('host','username','password','database_name');

    //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.選擇單行
    <?php
    //Open a new connection to the MySQL server
    $mysqli = new mysqli('host','username','password','database_name');

    //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.選擇行數(shù)
     
    <?php
    //Open a new connection to the MySQL server
    $mysqli = new mysqli('host','username','password','database_name');

    //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.選擇預(yù)處理
    $search_product = "PD1001"; //product id

    //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();
    $search_ID = 1; 
    $search_product = "PD1001"; 

    $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.插入數(shù)據(jù)庫(kù)
    <?php
    //values to be inserted in database table
    $product_code = '"'.$mysqli->real_escape_string('P1234').'"';
    $product_name = '"'.$mysqli->real_escape_string('42 inch TV').'"';
    $product_price = '"'.$mysqli->real_escape_string('600').'"';

    //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.插入預(yù)處理
    //values to be inserted in database table
    $product_code = 'P1234';
    $product_name = '42 inch TV';
    $product_price = '600';

    $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 1
    $product_code1 = '"'.$mysqli->real_escape_string('P1').'"';
    $product_name1 = '"'.$mysqli->real_escape_string('Google Nexus').'"';
    $product_price1 = '"'.$mysqli->real_escape_string('149').'"';

    //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 Update Query
    $results = $mysqli->query("UPDATE products SET product_name='52 inch TV', product_code='323343' WHERE ID=24");

    //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.預(yù)處理
    $product_name = '52 inch TV';
    $product_code = '9879798';
    $find_id = 24;

    $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.刪除

    //MySqli Delete Query
    $results = $mysqli->query("DELETE FROM products WHERE added_timestamp < (NOW() - INTERVAL 1 DAY)");

    if($results){
        print 'Success! deleted one day old records'; 
    }else{
        print 'Error : ('. $mysqli->errno .') '. $mysqli->error;
    }
    親們,如果有PHP相關(guān)的問(wèn)題可以隨時(shí)聯(lián)系我們,bjwill

    posted on 2015-01-20 10:03 草原上的駱駝 閱讀(11937) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): PHP

    主站蜘蛛池模板: 亚洲色大成网站WWW久久九九| 日韩电影免费观看| 亚洲av无码专区国产不乱码| 好男人www免费高清视频在线| 最近免费字幕中文大全视频| 国产免费AV片在线观看| 亚洲精品无码中文久久字幕| 中文字幕无码精品亚洲资源网久久| 亚洲妓女综合网99| 亚洲成a人片在线观| 亚洲手机中文字幕| 亚洲av午夜精品无码专区| 亚洲国产成人久久综合区| 亚洲成A人片77777国产| 国产成人精品日本亚洲专区| 丝袜熟女国偷自产中文字幕亚洲| 激情97综合亚洲色婷婷五| 国产精品亚洲一区二区三区在线| 亚洲成Av人片乱码色午夜| 亚洲Av熟妇高潮30p| 亚洲成无码人在线观看| 久久狠狠爱亚洲综合影院 | 19禁啪啪无遮挡免费网站| 最近中文字幕免费mv在线视频| 97视频免费在线| 大学生a级毛片免费观看| 免费国产a国产片高清| 亚洲午夜国产精品无码| 久久久无码精品亚洲日韩按摩 | 永久中文字幕免费视频网站| 四虎永久在线精品免费影视| 国产精品亚洲不卡一区二区三区 | 国产精品亚洲精品日韩已方| 国产亚洲综合久久系列| 亚洲经典在线中文字幕| 亚洲伊人久久大香线蕉AV| 日本一区二区三区免费高清在线| 最新亚洲成av人免费看| 两性刺激生活片免费视频| 又大又硬又爽免费视频| 亚洲AV无码一区二区乱子伦|