php 执行 mysqli_query 时遇到问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2304894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:58:01  来源:igfitidea点击:

Having a problem getting mysqli_query to execute

phpmysqlmysqli

提问by GilloD

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.

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

Here's the relevant code:

这是相关的代码:

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

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

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

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.

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

Thoughts? THis is driving me bonkers.

想法?这让我发疯。

回答by VolkerK

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.

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

<?php
$con = mysqli_connect("localhost", "root", "", "test");
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", 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\n", mysqli_error($con));
}
else {
  echo 'done.';
}