Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php $conn->;prepare总是返回false_Php_Mysql_Mysqli - Fatal编程技术网

Php $conn->;prepare总是返回false

Php $conn->;prepare总是返回false,php,mysql,mysqli,Php,Mysql,Mysqli,$conn是一个数据库连接,我以前使用过它来获取信息,因此它可以正常工作。准备返回错误,我不知道为什么 $updateWeight = "insert into $username (weight, date) values (?,?)"; if($stmt = $conn->prepare($updateWeight)) { $stmt->bind_param("is",$weight,$date); $stmt->execute(); die("Ad

$conn是一个数据库连接,我以前使用过它来获取信息,因此它可以正常工作。准备返回错误,我不知道为什么

$updateWeight = "insert into $username (weight, date) values (?,?)";
if($stmt = $conn->prepare($updateWeight))
{
    $stmt->bind_param("is",$weight,$date);
    $stmt->execute();
    die("Added ". $weight . " " . $date);
}
else
{
    die( "$updateWeight with $weight and $date error 1");
}
这将返回“插入测试(重量、日期)值(?),其中包含150和2015/06/16错误1”

就我所知,这是桌子。一切都很好

mysql> select * from testing;
+----+--------+------------+
| id | weight | date       |
+----+--------+------------+
|  1 |    200 | 2015/06/10 |
+----+--------+------------+
1 row in set (0.01 sec)
我确信这个错误是很小很愚蠢的,但我似乎找不到它

我将该命令复制并粘贴到mysql中,并在替换变量后运行

mysql> insert into testing (weight, date) values (150,"2015/06/15"); 
Query OK, 1 row affected (0.02 sec)
注: 您的查询使用-
日期
。您可以做的是使用倒勾(`)来绕过这一点

如果它不能解决您的问题,那么就让我们来确定返回false的错误原因

将其放入您的
else()
条件中:

printf("Errormessage: %s\n", $conn->error);

我通过放置$conn->close()修复了它;然后重新创建$conn,就像我以前使用的那样,它在测试(
weight
date
)中插入150和2015/06/16的值(?),错误1我不知道如何在这里转义,但背景在那里你的
$username
$conn
?您是否在给定代码之前建立了您的
$conn
?是的,在此之前,我在select中使用了相同的$conn,因此我知道它可以工作,$username正在测试,当else打印statement@dlrdlrdlr-更新了我的答案。在您的
else()
中添加一个错误报告,让我们看看它返回false的错误原因。我尝试了使用backtics和不使用backtics,两次都得到了
错误消息:插入测试(
weight
date
)值(?),其中包含150和2015/06/16错误1
错误消息在其自身的行中,并且为空。新代码是`printf(“错误消息:%s\n”,$conn->error);模具(“
$updatewight,带$weight和$date错误1”)`
printf("Errormessage: %s\n", $conn->error);