创建搜索功能时,为什么会出现“警告:第 29 行 /path/test.php 中的除以零查询为空”?

发布于 2024-11-28 12:55:44 字数 2921 浏览 1 评论 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;
?>

I building a simple search feature that checks against a column "desc" in mysql table "products"

This is my code for the results, where $find is the user input string that has been formatted to uppercase.

 $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>
 ';
 } 

Why am I getting the following error:

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

Line 29 is this line:

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

This query produces results in php myadmin, but when used in my script it gives the error.

Does anyone have any ideas on this?

EDIT:

Here is the full script with db connection info removed:

<?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;
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悸初 2024-12-05 12:55:44

您不正确地嵌套引用。

尝试:

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

单引号不会扩展您的 $find 变量,事实上,单引号在 LIKE 引导 PHP 将 % 计算为模运算符之后终止。

You are improperly nesting your quotes.

Try:

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

Single quotes will not expand your $find variable, and in fact the single quotes are terminated just after the LIKE leading PHP to evaluate % as the modulus operator.

飘然心甜 2024-12-05 12:55:44

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

It looks like you're terminating the string with the quote before %$find%. Perhaps it's trying to perform a modulus with $find and if $find is non-numeric it'd be trying to mod by an integer value of zero.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文