我正在尝试将sender_id和receiver_id添加到我的变量中,但它会让我跟踪错误
警告:mysql_fetch_array()期望参数1是资源, 在/opt/lampp/htdocs/Task/php/insertdatanew.php中给出的对象在线 36
警告:mysql_fetch_row()期望参数1是资源,对象 在第42行的/opt/lampp/htdocs/Task/php/insertdatanew.php中给出
CODE:
<?php
$sender_id = 0;
$receiver_id = 0;
session_start();
if(isset($_SESSION['login_user'])) {
//header('location: chat.php');
}
else {
header('location: chatlogin.php');
}
if(isset($_REQUEST['sender']) AND isset($_REQUEST['msg']) AND isset($_REQUEST['time']) AND isset($_REQUEST['receiver']) ){
$msg = $_REQUEST['msg'];
$time = $_REQUEST['time'];
$sender = $_REQUEST['sender'];
$receiver = $_REQUEST['receiver'];
echo $sender;
echo $receiver;
echo $msg;
echo $time;
//echo $msg;
if ( empty($_REQUEST['msg']) ) {
}
else{
require_once 'dc_chat.php';
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$result = $mysqli -> query("SELECT id from users where username LIKE '{$sender}'");
$row = mysql_fetch_row($result);
$sender_id = $row[0];
$result = $mysqli -> query("SELECT id from users where username LIKE '{$receiver}'");
$row = mysql_fetch_row($result);
$receiver_id = $row[0];
//echo $receiver_id;
$sql = $mysqli -> query("INSERT INTO `messages` (`sender_id`, `receiver_id`, `msg`, 'chattime') VALUES ('{$sender_id}', '{$receiver_id}', '{$msg}' ,'{$time}')");
if(! $sql ) {
echo 'N';
}
else {
echo 'Y';
}
}
}
else{
echo "hello";
}
?>
我从ajax得到$ msg,$ time,$ sender,$ receiver,数据类型是JSON
答案 0 :(得分:0)
你正在混合mysql和mysqli函数。 mysql_fetch_row
应该是mysqli_fetch_row
。
将if (!$sql)
替换为if ($mysqli->affected_rows == 0)
以测试INSERT语句是否成功。
如果您怀疑SQL语句中有错误,请在查询行后使用echo $mysqli->error;
将其放在屏幕上。