phoenix13

导航

 

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>";
//    }
?>

 

posted on 2015-06-04 06:47  phoenix13  阅读(288)  评论(0编辑  收藏  举报