2011-04-15 47 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); 
    } 
?>