1

试图从 mysql 查询中检索一些信息并将其打印在字符串中,但无法理解问题,根据这里http://php.net/manual/en/function.mysql-result.php它应该是正确的,但它返回空的。

$sql = new mysqli('xxxx', 'xxxx', 'xxxxxx', 'xxxxx');
if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
exit;
}
echo " user id  ", $user_id, "\n";
echo "event id ", $event_id, "\n";
$query ="SELECT A1.event_name,A1.start_date,A2.first_name,A2.last_name  FROM OWN_EVENTS A1 INNER JOIN  USERS A2 ON  A1.event_id  =  $event_id WHERE  A2.user_id=A1.user_id ";
$result = $sql->query($query);    
if (!$result) {
    sendResponse(417, json_encode("Query failed"));
exit;
}
$row = mysql_fetch_row($result);
echo "row[2] ", $row[2], "\n";
echo "row[3] ", $row[3], "\n";
//echo "result",$result,"\n";

$querySend ="SELECT email FROM USERS WHERE user_id = $user_id";
$resultSend = $sql->query($querySend);    
if (!resultSend) {
    sendResponse(417, json_encode("Query failed"));
exit;
}
$rowSend = mysql_fetch_row($resultSend);
echo "rowSend[0] ", $rowSend[0], "\n";
echo $rowSend["email"]; 

日志:

user id  8
event id 95

Warning: mysql_fetch_row() expects parameter 1 to be resource, object given in /var/insertToNotifications.php on line 180
row[2] 
row[3] 

Notice: Use of undefined constant resultSend - assumed 'resultSend' in /var/insertToNotifications.php on line 187

Warning: mysql_fetch_row() expects parameter 1 to be resource, object given in /var/insertToNotifications.php on line 191
rowSend[0] 

结果phpmyadmin:

在此处输入图像描述

在此处输入图像描述

我的目标是能够打印类似$body = "Hi,\n\n $row[2],$row[3] has invited you to $row[0] starting $row[1] \n\n \n\n ";

我怎样才能做到这一点?

4

2 回答 2

1

您正在将 mysqli_* 函数与已弃用的 mysql_* 函数混合使用。您想继续使用 mysqli 函数来获得如下结果:

$query ="SELECT A1.event_name,A1.start_date,A2.first_name,A2.last_name  FROM OWN_EVENTS A1 INNER JOIN  USERS A2 ON  A1.event_id  =  $event_id WHERE  A2.user_id=A1.user_id ";
$result = $sql->query($query);    
if (!$result) {
    sendResponse(417, json_encode("Query failed"));
exit;
}
$row = $result->fetch_row($result);

$result这是您可以在对象上调用的方法的文档: http ://www.php.net/manual/en/class.mysqli-result.php

于 2013-06-27T14:51:59.930 回答
1

您正在混合 mysqli (注意i)和 mysql (注意an的LACKi)。这两个库不可互操作,一个库的句柄/结果对另一个库绝对没有任何意义。

你可能想要

$row = mysqli_fetch_row($result);
            ^----

并且可能不应该混合库的程序和 OOP 版本。选择一种风格并坚持下去。

于 2013-06-27T14:52:02.403 回答