dttphb59319 2010-07-31 22:26
浏览 64
已采纳

mysqli使用选择失败的准备语句,原因不明

Why is this failing? I am connected to the correct db, and keys is definitely there.

//verify key against key list in database
if ($prep_statement = $db->prepare("SELECT id FROM key_list WHERE keys=?"))
{
    $prep_statement->bind_param('s',$key);
    $prep_statement->execute();
    echo $prep_statement->num_rows;
     $prep_statement->close();
} else {
    printf("Prepared Statement Error: %s
", $db->error); 
    die();      
}

I get the following error, and the if statement is returning false

Prepared Statement Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key=?' at line 1 

Here's my table structure:

-- MySQL dump 10.11
--
-- Host: localhost    Database: gameserverdev
-- ------------------------------------------------------
-- Server version   5.0.51a-24+lenny4

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `key_list`
--

DROP TABLE IF EXISTS `key_list`;
SET @saved_cs_client     = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `key_list` (
  `id` bigint(20) unsigned NOT NULL auto_increment,
  `keys` char(16) NOT NULL,
  `datetime_registered` datetime NOT NULL,
  `banned` tinyint(1) NOT NULL,
  `comment` char(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;

--
-- Dumping data for table `key_list`
--

LOCK TABLES `key_list` WRITE;
/*!40000 ALTER TABLE `key_list` DISABLE KEYS */;
INSERT INTO `key_list` VALUES (2,'1111222233334444','2010-07-31 16:04:30',0,NULL);
/*!40000 ALTER TABLE `key_list` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Dumping routines for database 'gameserverdev'
--
DELIMITER ;;
DELIMITER ;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2010-07-31 23:00:56
  • 写回答

1条回答 默认 最新

  • dsxfa26482 2010-07-31 22:46
    关注

    You are binding a result, not binding a parameter. Change bind_result to bind_param and it should work correctly.


    key is a MySQL reserved word. You'll need to either change the column name to something that is not a reserved word, or you'll need to quote it. In MySQL, the default identifier quote character is the backtick, `, though if ANSI Quotes are enabled, you can use the double quote, ", instead.

    So, your statement should be:

    SELECT id FROM key_list WHERE `key` = ?
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料