如何从 MySQL table 中获取 php 网络服务中的所有记录
how to get all the records from MySQL table in php web services
$sql=mysql_query("SELECT friendName,createdDate FROM friends where userId='$userId'");
$result=mysql_fetch_assoc($sql);
if(!empty($result))
{
$json=$result;
}
else
{
$json = array( "msg" => "No infomations Found");
}
header('Content-type: application/json');
我正在尝试使用 about 代码从数据库中获取所有值,但只有一条记录可以 come.please 帮助我
试试这个:
$sql=mysql_query("SELECT friendName,createdDate FROM friends where userId='$userId'");
$json= array();
while($json = mysql_fetch_assoc($sql)){}
if(empty($json))
{
$json = array( "msg" => "No infomations Found");
}
header('Content-type: application/json');
参见下面的示例:
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "EMP ID :{$row['emp_id']} <br> ".
"EMP NAME : {$row['emp_name']} <br> ".
"EMP SALARY : {$row['emp_salary']} <br> ".
"--------------------------------<br>";
}
首先停止使用 mysql_*
,因为它现在已弃用库。使用 mysqli_*
或 PDO
其次,您需要按以下方式对返回的查询进行迭代 result-set object
:-
$sql=mysql_query("SELECT friendName,createdDate FROM friends where userId='$userId'");
$json = array(); // create empty array
$i = 0; // start a counter
while($result=mysql_fetch_assoc($sql)){ // start iteration on result-set object
// fetch values one-by-one and assing to them in the array
$json[$i]['friendName'] = $result['friendName'];
$json[$i]['createdDate'] = $result['createdDate'];
$i++; // increase the counter so that values will assign to the new indexes every-time (prevent from over-writing)
}
if(count($json)>0){ // check array have some values or not
// now you have all records in $json array and here you can do your next code whatever you want based on this resultant array
}else{
$json = array( "msg" => "No infomations Found");
}
header('Content-type: application/json'); //i don't know what purpose this line will serve, so check your self.
$sql=mysql_query("SELECT friendName,createdDate FROM friends where userId='$userId'");
$result=mysql_fetch_assoc($sql);
if(!empty($result))
{
$json=$result;
}
else
{
$json = array( "msg" => "No infomations Found");
}
header('Content-type: application/json');
我正在尝试使用 about 代码从数据库中获取所有值,但只有一条记录可以 come.please 帮助我
试试这个:
$sql=mysql_query("SELECT friendName,createdDate FROM friends where userId='$userId'");
$json= array();
while($json = mysql_fetch_assoc($sql)){}
if(empty($json))
{
$json = array( "msg" => "No infomations Found");
}
header('Content-type: application/json');
参见下面的示例:
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "EMP ID :{$row['emp_id']} <br> ".
"EMP NAME : {$row['emp_name']} <br> ".
"EMP SALARY : {$row['emp_salary']} <br> ".
"--------------------------------<br>";
}
首先停止使用 mysql_*
,因为它现在已弃用库。使用 mysqli_*
或 PDO
其次,您需要按以下方式对返回的查询进行迭代 result-set object
:-
$sql=mysql_query("SELECT friendName,createdDate FROM friends where userId='$userId'");
$json = array(); // create empty array
$i = 0; // start a counter
while($result=mysql_fetch_assoc($sql)){ // start iteration on result-set object
// fetch values one-by-one and assing to them in the array
$json[$i]['friendName'] = $result['friendName'];
$json[$i]['createdDate'] = $result['createdDate'];
$i++; // increase the counter so that values will assign to the new indexes every-time (prevent from over-writing)
}
if(count($json)>0){ // check array have some values or not
// now you have all records in $json array and here you can do your next code whatever you want based on this resultant array
}else{
$json = array( "msg" => "No infomations Found");
}
header('Content-type: application/json'); //i don't know what purpose this line will serve, so check your self.