2013-03-24 79 views
-1

我试图从表'学生'中提取所有行。 但它只返回数组中的第一行。 如何获取数组中的所有行?Mysql使用php选择查询

我的代码是 -

<?php 
    // con ref 
    $dbCon = mysql_connect("localhost","root","") or die("connection problem".mysql_error()); 
    // db con 
    mysql_select_db("mysql_db",$dbCon); 
    $sql = "select * from student "; 
    $data = mysql_query($sql); 
    $row = mysql_fetch_array($data); 
    print_r($row); 
?> 
+0

谷歌的功能是你的朋友。 “获取数组中的所有行”=许多结果:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&ie=UTF-8#hl=zh-CN&safe=active&sclient=psy-ab&q=fetch%20all %20th%20rows%20in%20an%20array&oq =&gs_l =&pbx = 1&fp = d76eed8efe782f95&ion = 1&bav = on.2,or.r_cp.r_qf。&bvm = bv.44158598,d.aWM&biw = 1680&bih = 903 – Robert 2013-03-24 17:41:30

+0

[Please,don'在新代码中使用mysql_ *函数](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。他们不再被维护,并[正式弃用](https://wiki.php.net/rfc/mysql_deprecation)。看到红色框?请改为了解准备好的语句,并使用[pdo](https://wiki.php.net/rfc/mysql_deprecation)或[mysqli](http://stackoverflow.com/questions/tagged/mysqli)。 – zessx 2013-03-24 17:41:53

回答

1

您可以通过结果需要循环。

while($row = mysql_fetch_array($data)) { 
    print_r($row); 
} 
0
<?php 
// Make a MySQL Connection 
$query = "SELECT * FROM example"; 

$result = mysql_query($query) or die(mysql_error()); 


while($row = mysql_fetch_array($result)){ 
echo $row['name']. " - ". $row['age']; 
echo "<br />"; 
} 
?> 

一个实例如何遍历的结果。

http://www.tizag.com/mysqlTutorial/mysqlfetcharray.php

+0

请不要鼓励在PHP MYSqL库的恶作剧noobie – 2013-03-24 17:49:11

2

按照该文档上mysql_fetch_array()

从结果集中取得一行作为关联阵列,数字数组,或两者

一个结果。

你将要遍历所有的行,像:

while($row = mysql_fetch_array($data)) { 
    // Use $row here.. 
} 

此外,应该注意的是,使用mysql_*功能是弃用。看到大红色框here。考虑使用PDOMySQLi代替。