码迷,mamicode.com
首页 > 数据库 > 详细

PHP use MySQLi

时间:2015-06-04 07:29:08      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

1. connection

  $mysqli = new mysqli($host,$user,$passwd,$db,$port);
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }

2. select query

$sql="SELECT name FROM users2";
    if (!$mysqli->real_query($sql)){
        printf("Errormessage: %s<br>", $mysqli->error);
    }
    $result = $mysqli->use_result();

    echo "Result set :<br>";
    // use numeric array
    while ($row = $result->fetch_array()) {
        echo " name = " . $row[0] ."<br>";
    }
    // use associate array
//    while ($row = mysqli_fetch_assoc($result)){
//        echo " name = " . $row[‘name‘] ."<br>";
//    }

 3. complete code

<?php
    $host="localhost";
    $user="root";
    $passwd="123";
    $db="test";
    $port="3307";

    $mysqli = new mysqli($host,$user,$passwd,$db,$port);
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }
    $sql="SELECT name FROM users2";
    if (!$mysqli->real_query($sql)){
        printf("Errormessage: %s<br>", $mysqli->error);
    }
    $result = $mysqli->use_result();

    echo "Result set :<br>";
    // use numeric array
    while ($row = $result->fetch_array()) {
        echo " name = " . $row[0] ."<br>";
    }
    // use associate array
//    while ($row = mysqli_fetch_assoc($result)){
//        echo " name = " . $row[‘name‘] ."<br>";
//    }
?>

 

PHP use MySQLi

标签:

原文地址:http://www.cnblogs.com/phoenix13suns/p/4550778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!