mysqli prepare statement using select failing for some unknown reason
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\n", $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_开发者_StackOverflow社区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
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` = ?
精彩评论