2011-04-15 65 views
0

我有這樣的表例如檢索許多行的PHP MySQL的

+------+---------+------+ 
| id | item_id | type | 
+------+---------+------+ 
| 1 |  2 | book | 
| 1 |  1 | pen | 
+------+---------+------+ 

我想要檢索的所有數據,其中ID = 1 PHP腳本,這裏是我用

<?php 
    $stat="select item_id, type from tb where id=1"; 
    $query = mysqli_query($con, $stat); 
    $result = mysqli_fetch_array($query,MYSQLI_ASSOC); 
    print_r($result); 
    ?> 

代碼結果是:Array ([item_id] => 2 [type] => book) 那只是第一行,如何檢索php代碼中的所有行?

+1

查看答案[這個問題](http://stackoverflow.com/questions/4643340/mysql-fetch-array-add-all-rows) – 2011-04-15 07:41:56

回答

5

使用此

<?php 
    $stat="select item_id, type from tb where id=1"; 
    $query = mysqli_query($con, $stat); 
    while($result = mysqli_fetch_array($query,MYSQLI_ASSOC)){ 
     print_r($result); 
    } 
    ?> 

順便說一句:我會打電話的$stat變量$query(因爲它是你的查詢)。該$query變量實際上包含了結果,所以我會說這就是$result和你獲取數組可以被稱爲別的東西太..

+0

+1了BTW讓我輕笑。 – Khez 2011-04-15 07:53:20

1

您有一個名爲mysql_fetch_array功能在http://php.net/manual/en/function.mysql-fetch-array.php

閱讀有關試試這個例子:

<?php 
$con = mysql_connect("localhost","peter","abc123"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("my_db", $con); 

$result = mysql_query("SELECT * FROM Persons"); 

while($row = mysql_fetch_array($result)) 
    { 
    echo $row['FirstName'] . " " . $row['LastName']; 
    echo "<br />"; 
    } 

mysql_close($con); 
?> 

更多http://www.w3schools.com/php/php_mysql_select.asp

1

嘛。對於oneyou能做到這一點:

<?php 
    $stat = "SELECT `item_id`, `type` FROM `tb` WHERE `id` = 1"; 
    $query = mysqli_query($con, $stat); 

    while (null !== ($result = mysqli_fetch_assoc($query))) 
    { 
     print_r($result); 
    } 
?>