简体   繁体   English

mysqli 致命错误:查询/准备语句中未使用索引

[英]mysqli fatal error: No index used in query/prepared statement

I want to execute a simple prepared Statement using mysqli, but it won't work.我想使用 mysqli 执行一个简单的准备好的语句,但它不起作用。

I have this table:我有这张桌子:

CREATE TABLE IF NOT EXISTS `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(100) COLLATE latin1_german2_ci NOT NULL,
  `password` varchar(100) COLLATE latin1_german2_ci NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci AUTO_INCREMENT=4 ;

And want to print the id of a specific email.并想打印特定电子邮件的 id。

$mysqli = new mysqli($server,$user,$pass,$db);

if(mysqli_connect_errno()) {
    echo "Connection Failed: " . mysqli_connect_errno();
    exit();
 }
 $user = "test@dada.com";
 $pass = "dada";
/* Create a prepared statement */
if($stmt = $mysqli -> prepare("SELECT * FROM account WHERE email=?
AND password=?")) {

  /* Bind parameters
     s - string, b - blob, i - int, etc */
  $stmt -> bind_param("ss", $user, $pass);

  /* Execute it */
  $stmt -> execute();

  /* Bind results */
  $stmt -> bind_result($result);

  /* Fetch the value */
  $stmt -> fetch();

  echo $user . "id of user is " . $result;

  /* Close statement */
  $stmt -> close();
}

/* Close connection */
$mysqli -> close();

But i get following Error:但我收到以下错误:

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'No index used in query/prepared statement SELECT * FROM account WHERE email=?致命错误:未捕获的异常 'mysqli_sql_exception' 带有消息 'No index used in query/prepared statement SELECT * FROM account WHERE email=? AND password=?' AND 密码=?' mysqli_stmt->execute() #1 {main} mysqli_stmt->execute() #1 {main}

Well I think you have to do this:好吧,我认为你必须这样做:

CREATE TABLE IF NOT EXISTS `account` (
  `id` PRIMARY KEY int(11) NOT NULL AUTO_INCREMENT,
   // the rest

The above code makes the id field of your table as PRIMARY KEY so it never repeats itself and it remains the index of your table.上面的代码使您的表的 id 字段作为PRIMARY KEY因此它永远不会重复,它仍然是您的表的索引。

Your problem is that the query you are executing is going to be inefficient without using an INDEX.您的问题是,如果不使用 INDEX,您正在执行的查询将效率低下。

SELECT * FROM account WHERE email=? AND password=?

There's no index on any of the two fields you have used in the WHERE clause.您在 WHERE 子句中使用的两个字段中的任何一个都没有索引。 One solution would be to create an index on both fields which should make the error go away.一种解决方案是在两个字段上创建一个索引,这应该会使错误消失。

ALTER TABLE account ADD INDEX `index_on_email_and_password` (email, password);

In many situations, you know that the INDEX is not going to improve the performance, so you can safely ignore this error.在许多情况下,您知道 INDEX 不会提高性能,因此您可以放心地忽略此错误。 To do so, replace the following line of code:为此,请替换以下代码行:

mysqli_report(MYSQLI_REPORT_ALL);

with one of these与其中之一

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// or 
mysqli_report(MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX);

This will keep on reporting normal SQL errors, but it will ignore all warnings about bad indexes.这将继续报告正常的 SQL 错误,但它会忽略所有关于坏索引的警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM