执行 mysqli_query 时遇到问题

2021-12-25 00:00:00 php mysql mysqli

问题来了:我今天开始交换使用mysqli.没什么大不了的,只需要改变一些陈述.一切顺利,没有错误......除了我根本无法执行任何查询.我对我的语法进行了两次和三次检查.我什至开始创建它应该返回错误的情况(尝试将其插入到不存在的表或值超过列限制或不匹配类型的表)和......什么都没有.它不返回错误,也不写入.如果参数 1 不是 mysqli 类型,它会报错.

Here's the problem: I started a swap today to use mysqli. No biggie, just had to change a few statements. Everything went fine, no errors... Except that I can't get it to execute any queries at all. I have double and triple checked my syntax. I even started creating situations where it SHOULD return an error (Trying to get it to INSERT to Tables that don't exist or with values that exceeded column limits or didn't match type) and... nothing. It doesn't return an error, it doesn't write. It WILL complain if parameter one is NOT a mysqli type.

相关代码如下:

$con = mysqli_connect("localhost", "root", "","test");

if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}

$query = "INSERT INTO files VALUES (NULL, 5, 'hello')";
mysqli_query($con, $query);

什么都没有.它运行没有问题,但它从不写入记录.我什至可以将 $query 更改为hdjhkfhhjfkd"并且它运行没有问题.mysqli_query() 只是没有执行,期间.我可以让它做出反应的唯一时间是,如果我将 $con 更改为其他任何内容,然后它会抱怨它需要一个 mysqli 类型.

And nothing. It runs without a problem, but it never writes the record. I can even change the $query to "hdjhkfhhjfkd" and it runs no problem. mysqli_query() just isn't executing, period. The only time I can get it to react is if I change $con to anything else, then it complains that it needs a mysqli type.

想法?这让我发疯.

推荐答案

你检查过 mysqli_query() 的返回值了吗?如果发生错误,则返回 FALSE.在这种情况下,mysqli_error() 会为您提供有关错误的更多信息.

Have you checked the return value of mysqli_query()? It returns FALSE if an error occurred. In that case mysqli_error() gives you more information about the error.

<?php
$con = mysqli_connect("localhost", "root", "", "test");
if (mysqli_connect_errno()) {
  printf("Connect failed: %s
", mysqli_connect_error());
  exit();
}

$query = "INSERT INTO files VALUES (NULL, 5, 'hello')";
echo "<pre>Debug: $query</pre>m";
$result = mysqli_query($con, $query);
if ( false===$result ) {
  printf("error: %s
", mysqli_error($con));
}
else {
  echo 'done.';
}

相关文章