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