2012-09-15 60 views
0

我一直在下面的腳本中收到此錯誤,不確定如何解決問題。我搜索並閱讀了大量的文章,但沒有得到它。我錯過了什麼?任何幫助真誠感謝!如何解決:警告:mysql_num_rows():提供的參數不是有效的MySQL結果資源

「警告:mysql_num_rows():提供的參數不是在/home/content/37/8642937/html/settingupsearch.php一個有效的MySQL結果資源第42行」

<?php 

$var = @$_GET['q'] ; // get the query for the search engine (if applicable) 
$trimmed = trim($var); //trim whitespace from the stored variable 

{ 
     $con = mysql_connect("***","***","***"); 
     if (!$con) 
      { 
      die('Could not connect: ' . mysql_error()); 
      } 
} 

$field = "title"; 
$query = mysql_query("SELECT * FROM Listings WHERE ($field) LIKE'%$trimmed%' ORDER BY id"); 

$result = mysql_query($query); 

?> 

<form name="search" method="GET" action="<?=$PHP_SELF?>"> 
Search the database for: <input type="text" name="q" /> 
<input type="submit" name="search" value="Search" /> 
</form> 

<? 
if ($trimmed == "") 
{ 
echo "<p>Please enter a search...</p>"; 
exit; 
} 

// check for a search parameter 
if (!isset($var)) 
{ 
echo "<p>We dont seem to have a search parameter!</p>"; 
exit; 
} 

$numresults=mysql_query($query); 
$numrows=mysql_num_rows($numresults); 

if ($numrows == 0) 
{ 
echo "<h4>Results</h4>"; 
echo "<p>Sorry, your search: &quot" . $trimmed . "&quot returned zero results</p>"; 
} 

// next determine if s has been passed to script, if not use 0 
if (empty($s)) 
{ 
$s=0; 
} 

// get results 
$result = mysql_query($query) or die("Couldn't execute query"); 

if($numrows > 1){ $return = "results";} 
else{ $return = "result"; } 

// display what the person searched for 
echo "<p>Your search for &quot" . $var . "&quot returned $numrows $return.</p>"; 

// begin to show results set 
$count = 1 + $s ; 

while ($r= mysql_fetch_array($result)) 
{ 
$id = $r["ID"]; 
$title = $r["title"]; 
$date = $r["price"]; 
$city = $r["city"]; 

$count++ ; 

?> 

<a href="http://www.***.com/archive/<? echo $title ?>/<? echo $id ?>.html"><? echo $price ?></a> 

<? echo $city ?> 


<? } ?> 
+0

sql injection .. –

+0

請顯示您的數據庫 –

+0

@MikeB - 我是新手。不明白你的意思? –

回答

2

在看着你代碼看起來你有一個錯誤:

$query = mysql_query("SELECT * FROM Listings WHERE ($field) LIKE'%$trimmed%' ORDER BY id"); 

這是包裝在一個mysql_query,然後你再次調用它與mysql_query。

相關問題