0

我构建了一个简单的搜索功能,用于检查 mysql 表“products”中的“desc”列

这是我的结果代码,其中$find是已格式化为大写的用户输入字符串。

 $dataQuery = 'SELECT * FROM `products` WHERE upper(`desc`) LIKE'%$find%'';
 $data = mysql_query($dataQuery) or die(mysql_error());

 //And we display the results 
 $pageContent = '';
 while($result = mysql_fetch_array( $data )) 
 { 
 $pageContent .= '
 <p>Desc:'.$result['desc'].' Price:'.$result['price1'].'</p>
 ';
 } 

为什么我会收到以下错误:

Warning: Division by zero in /path_to/test.php on line 29

Warning: Division by zero in /path_to/test.php on line 29
Query was empty

第 29 行是这一行:

$dataQuery = 'SELECT * FROM `products` WHERE upper(`desc`) LIKE'%$find%'';

此查询在 php myadmin 中产生结果,但在我的脚本中使用时会出现错误。

有人对此有任何想法吗?

编辑:

这是删除了数据库连接信息的完整脚本:

<?php

//This is only displayed if they have submitted the form 
if ($searching == 'yes') 
{ 
$pageContent .= '<h2>Results</h2>'; 

//If they did not enter a search term we give them an error 
if ($find == '') 
{ 
$pageContent .= '<p>You forgot to enter a search term</p>'; 
exit; 
} 

// Otherwise we connect to our Database 
$bccConn   = mysql_connect($bccHost, $bccUser, $bccPass) or exit(mysql_error());
             mysql_select_db($bccDB, $bccConn) or exit(mysql_error());

 // We preform a bit of filtering 
 $find = strtoupper($find); 
 $find = strip_tags($find); 
 $find = trim ($find); 

 //Now we search for our search term, in the field the user specified 
 $dataQuery = "SELECT * FROM `products` WHERE upper(`desc`) LIKE'%$find%'";
 $data = mysql_query($dataQuery) or die(mysql_error());

 //And we display the results 
 $pageContent = '';
 while($result = mysql_fetch_array( $data )) 
 { 
 $pageContent .= '
 <p>Desc:'.$result['desc'].' Price:'.$result['price1'].'</p>
 ';
 } 

 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
 $anymatches=mysql_num_rows($data); 
 if ($anymatches == 0) 
 {
$pageContent .= '
 <p>Sorry, but we can not find an entry to match your query</p>
 ';
 } 

 //And we remind them what they searched for 
$pageContent .= '
 <p><b>Searched For:</b>  '.$find.'</p>
 ';
 } 

ob_start();
require_once $_SERVER['DOCUMENT_ROOT'].'/includes/config.php';
require_once($docRoot . '/includes/layout.php');

$pageContent = '
<h2>orders</h2>
<form name="search" method="post" action="'.$PHP_SELF.'">
<p>Seach for: <input type="text" name="find" />
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" /></p>
</form>
';

echo $head1 . $pageDetails . $head2 . $header . $menu . $belowMenu . $content . $pageContent . $footer . $pageScripts;
exit;
?>
4

2 回答 2

1

您不正确地嵌套了引号。

尝试:

$dataQuery = "SELECT * FROM `products` WHERE upper(`desc`) LIKE'%$find%'";

单引号不会扩展您的$find变量,实际上单引号在LIKE领先的 PHP 将 % 评估为模运算符之后终止。

于 2011-08-08T19:50:58.417 回答
1

看起来您正在用 %$find% 之前的引号终止字符串。也许它正在尝试使用 $find 执行模数,如果 $find 是非数字的,它会尝试通过整数值零进行模数。

于 2011-08-08T19:52:28.247 回答