到目前为止我有这段代码:
<?php
include('config.php');
$sql = "SELECT senderID as receiverID, msg, time
FROM msg
WHERE senderID = 1";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row["receiverID"];
echo $row["msg"];
echo $row["time"];
}
mysql_free_result($result);
?>
现在,这只是获取信息和内容,它可以工作,唯一的问题是处理部分。看,我可能可以在 Python 中轻松地做到这一点,但我是 PHP 新手,所以,如果有人可以帮助我,因为这是我返回的内容的示例:
1Hello! How are you?!?!?!876765981admin to user 3574769
现在,我实际上想知道如何将它们分割成它自己的变量或者只是单独打印,我不太确定,这就是它应该如何寻找Python的东西:
firstRow = ['1','Hello! How are you?!?!?!','876765981']
secondRow = ['1','admin to user 3','574769']
现在,我不太确定 php 结构或代码。
最佳答案
在 php 中,你得到的是:
[
1 => ["receiverId"=>'1',
"msg"=>'Hello! How are you?!?!?!',
"time"=>'876765981'],
2 => ["receivedId"=>'1',
"msg"=>'admin to user 3',
"time"=>'574769']
]
获取第一行
$firstrow = $result[1]
打印
echo $firstrow["msg"];
$result
是一个嵌套关联数组
关于php - 使用 PHP 检索 MySQL 多个查询并处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20987075/