php - 使用 PHP 检索 MySQL 多个查询并处理

标签 php python mysql sorting structure

到目前为止我有这段代码:

<?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/

相关文章:

php - Symfony2 Doctrine 子查询

php - Apache 似乎没有检测到我的 .htaccess 文件

python - 使用平均成本基础法计算成本

python - 设置复印警告

mysql - mysql 子查询中字段名称区分大小写(版本 5.6 与版本 8)

php - 发送类似于职位提醒的电子邮件通知需要一些帮助

php - 如何在 openstreetmap 中从 map 外部打开标记?

php - 如何使用 Amazon S3 创建一次性下载链接?

python - python web scraper 中的错误无法正常运行

mysql - 错误#1064 : phpMyAdmin import showing html errors in a MYSQL database - why html?