使用PHP和Process检索MySQL多个查询

时间:2014-01-08 04:30:09

标签: 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!你好吗?!?!?!876765981admin给用户3574769

现在,我实际上想知道如何能够将这些变成自己的变量或者单独打印,我不是很确定,这就是它想要寻找python的假设:

  

firstRow = ['1','你好!你好吗?!?!?!','876765981']

     

secondRow = ['1','admin to user 3','574769']

现在,我不太确定php结构或代码。

1 个答案:

答案 0 :(得分:0)

在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是一个嵌套的关联数组