1. Connection
$mysqli new mysqli ($host,$user,$passwd,$db,$port); if ($mysqli-connect_errno) { echo$mysqli$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=NewMysqli ($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