我正在尝试这段代码几个小时。我无法弄清楚我的错误是什么。
$conn = mysqli_connect('localhost', 'user', 'pass', 'mydb')
or die( "Unable to connect");
$check = mysqli_query($conn, "select * from Users where Fuid='$fbid'",
MYSQLI_STORE_RESULT);
$rows = mysqli_num_rows($check);
if (empty($rows)) { // if new user . Insert a new record
$query = "INSERT INTO Users (Fuid,Funame,Ffname,Femail)
VALUES ('$fbid','$funame','$ffname','$femail')";
mysqli_query($conn, $query);
}
可能会有一个非常小的错误,但我只是被炸了。
答案 0 :(得分:3)
这似乎很难做很多事情。试试这个......
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli('localhost', 'user', 'pass', 'mydb');
$conn->set_charset('utf8'); // or whatever is appropriate
$check = $conn->prepare('SELECT 1 FROM `Users` WHERE Fuid = ?');
$check->bind_param('i', $fbid); // assuming $fbid is an integer
$check->execute();
if (!$check->fetch()) {
$sql = 'INSERT INTO `Users` (Fuid, Funame, Ffname, Femail) VALUES (?, ?, ?, ?)';
$stmt = $conn->prepare($sql);
$stmt->bind_param('isss', $fbid, $funame, $ffname, $femail);
$stmt->execute();
}
答案 1 :(得分:0)
使用
mysqli_query($conn, $query);
而不是
mysqli_query($query, $conn);
答案 2 :(得分:-1)
你颠倒了参数!
mysqli_query($query, $conn);
应该是
mysqli_query($conn, $query);
试试这个:
使用mysqli_connect_errno
代替die
$conn = mysqli_connect('localhost', 'user', 'pass', 'mydb');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$check = mysqli_query($conn, "select * from Users where Fuid='$fbid'",
MYSQLI_STORE_RESULT);
$rows = mysqli_num_rows($check);
if (empty($rows)) { // if new user . Insert a new record
$query = "INSERT INTO Users (Fuid,Funame,Ffname,Femail)
VALUES ('$fbid','$funame','$ffname','$femail')";
mysqli_query($conn,$query);
}