Php 简单select语句的where子句中的MySQL未知列

Php 简单select语句的where子句中的MySQL未知列,php,mysql,where,collation,Php,Mysql,Where,Collation,我有一个非常基本的select语句,它导致了一个列未知错误。当我试图在变量中使用字符而不仅仅是数字时,查询就会出现问题。不知道这是否与排序有关 以下是我目前掌握的情况: $titleno=$_REQUEST['title_no']; $titleno=mysql_real_escape_string($titleno); $titleno = utf8_decode($titleno); //tried without this before but didn't work $query="SE

我有一个非常基本的select语句,它导致了一个列未知错误。当我试图在变量中使用字符而不仅仅是数字时,查询就会出现问题。不知道这是否与排序有关

以下是我目前掌握的情况:

$titleno=$_REQUEST['title_no'];
$titleno=mysql_real_escape_string($titleno);
$titleno = utf8_decode($titleno); //tried without this before but didn't work
$query="SELECT * FROM `Titles` WHERE `title-no` = '".$titleno."'"; 
//tried various versions of this query - left it as single quotes as that seems to be the correct way. This only fails when a character is entered. Numbers work fine.  

echo "query - <br> $query <br>";    
$get_title_result=mysql_query($query) or die(mysql_error());
//here I get the unknown column name error - MySQL treats the titleno as the column name 
如果我没有在标题“否”中使用“d”,那么效果很好……此外,我尝试了一个没有连字符的不同列名,但仍然得到相同的行为。DB将标题编号的排序规则定义为latin1_swedish_ci。(当我将查询粘贴到mysqladmin时,不会出现此问题)

以下是表格定义:

CREATE TABLE `Titles` (  
 `id` int(11) NOT NULL auto_increment,  
 `title-no` varchar(15) NOT NULL,  
  UNIQUE KEY `title-no` (`title-no`),  
  KEY `id` (`id`)  
  ) ENGINE=MyISAM  
  AUTO_INCREMENT=9090949 DEFAULT CHARSET=latin1 AUTO_INCREMENT=9090949 ;
已解决:此问题与此查询无关。这是一个后续的查询。我很困惑,因为我只是在重复这个问题。我的错。谢谢大家的支持!:)

试试:

$query = "SELECT * FROM Titles WHERE `Titles`.`title-no` = '" . $titleno . "'"; 

下面是一个到基于语句查询的快速转换(使用MySQLi,根据需要调整代码或本例)。假设底层的prepared语句引擎知道您不能在prepared语句中指定带有占位符的列名,因此它应该正确地传递它(这里是希望:-)


也可以粘贴表定义吗?请在字段名周围保留反勾号。但千万不要在值周围使用反勾号,否则mysql会将它们解释为字段名并抛出错误?你能用添加到代码中的echo语句更新你的问题吗?@Jocelyn-添加了echo语句。就在运行查询之前…查询看起来很好。也许这是一个愚蠢的提示,但请确保此查询是引发错误的查询,并且它不是脚本后面的另一个查询,您忘记了正确的单引号:)只有这样,我才能开始深入研究奇怪的字符编码问题…问题解决了。我忘了带撇号,因为表中也有
-
。@Gabriel我尝试了你的建议(修正引号后),但没有效果。由于连字符的缘故,你需要在标题“否”上加上记号或引号(我认为,否则它只会将其视为标题,这是错误的)谢谢。@Jocelyn-我尝试过各种引号、单引号、记号等。查询可以处理数字,我想这与在其中添加字符有关……不确定发生了什么。谢谢
$query = "SELECT * FROM Titles WHERE `Titles`.`title-no` = '" . $titleno . "'"; 
$titleno=$_REQUEST['title_no'];
$statement=mysqli_prepare($your_mysqli_link, "SELECT `id` FROM `Titles` WHERE `title-no` = ?"); 
mysqli_stmt_bind_param($statement, 's', $titleno);
mysqli_stmt_execute($statement);
mysqli_stmt_bind_result($statement, $found_id);
mysqli_stmt_fetch($statement);
echo "found id: $found_id";