如何在PHP中从数据库检索多行 [英] How to retrieve multiple rows from database in PHP
本文介绍了如何在PHP中从数据库检索多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从数据库中检索多行并使用php代码将其处理为服务,但我不会检索所有值,仅显示表格的第一行,如何使dis工作?
I am trying to retrieve multiple rows from the database and process it for response as service using php code, but I wouldn't retreive all values, only first row from the table is displaying, how can I make dis to work?
这是我的代码:
$servername="localhost";
$username="root";
$conn= mysql_connect($servername,$username)or die(mysql_error());
mysql_select_db("testing",$conn);
$sql="insert into login (src,dest)values('$from','$tona')";
$result=mysql_query($sql,$conn) or die(mysql_error());
$res = mysql_query("SELECT * FROM login");
$numrows = mysql_num_rows($res);
setcookie('a',$numrows);
推荐答案
Note the use of _mysql
is discouraged for new development ... please read this on selecting a new API
这是非常基本的,但是您需要像这样循环返回的结果:
This is pretty basic but you need to loop the returned result like so :
$res = mysql_query("SELECT * FROM login");
while ($row = mysql_fetch_array($res, MYSQL_BOTH)) {
// your columns are accessible using
$row['columnname'];
// or
$row[columnnumber];
}
Docs for mysql_fetch_array
are here
mysql_query
返回成功的资源或失败的资源
mysql_query
returns a resource on success or false on failure
这篇关于如何在PHP中从数据库检索多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文