SQL输出无法正常显示

时间:2013-12-02 05:20:44

标签: php sql

我有一个启动并运行的代码,由于某种原因突然停止工作,每当我进行搜索时,这是我得到的唯一结果,而且表格没有显示:Searched For: pam

现在,当我尝试转储结果时,在下面的代码中添加了以下代码:{var_dump($result);}

while($result = mysql_fetch_array($sql)) {var_dump($result);}

我已经获得了这张桌子,但它是空的,结果如下:

array(32) { [0]=> string(3) "valueA" ["testA"]=> string(3) "valueA" [1]=> string(2) "valueB" ["testB"]=> string(2) "valueB" [2]=> string(7) "valueC" ["testC"]=> string(7) "valueC" [3]

这是我的整个代码:

<body>

 <form name="search" method="post" action="">
 <fieldset style='width: 500px'>
 <legend align='left'><strong>Search</strong></legend>
 <p>
 Seach for: <input type="text" name="find" id="find" /> in 
 <Select NAME="field" id="field">
 <Option VALUE="testA">A</option>
 <Option VALUE="testB">B</option>
 <Option VALUE="testC">C</option>
 <Option VALUE="testD">D</option>
 </Select>
 <input type="hidden" name="searching" value="yes" />
 <input type="submit" name="search" id="search" value="Search" />
 </p>
 </fieldset>
 </form>

<?php

 //This is only displayed if they have submitted the form 
if (isset($_POST['searching']) && $_POST['searching'] == "yes") 
{ 

//If they did not enter a search term we give them an error 
if (empty($_POST['find'])) 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 

 // Otherwise we connect to our Database 
 mysql_connect("host", "username", "passw") or die(mysql_error()); 
 mysql_select_db("testdb") or die(mysql_error()); 

 // We preform a bit of filtering 

 $find = strtoupper($_POST['find']); 
 $find = strip_tags($_POST['find']); 
 $find = trim ($_POST['find']); 
 $field = trim ($_POST['field']);

 //Now we search for our search term, in the field the user specified 
 $data = mysql_query("SELECT * FROM testtable WHERE UPPER($field) LIKE UPPER('%$find%')"); 


 //And we display the results 
 while($result = mysql_fetch_array( $data )) 
 { 
 $myRes = "<form action='' method='post'>
          <fieldset style='width: 10px'>
          <legend align='left'><strong>Results</strong></legend>
          <p>
          <table width='auto' border='1' cellspacing='1' cellpadding='1' align='center'>
          <tr>
          <th align='center' scope='row'>A</th>
          <td><textarea class=readonly name=testA id=testA cols=65 rows=3>" . $result['testA'] . "</textarea></td>
          </tr>
          <tr>
          <th scope='row'>B</th>
          <td><textarea class=readonly name=testB id=testB cols=65 rows=3>" . $result['testB'] . "</textarea></td>
          </tr>
          </table>
          </p>
          </fieldset>
          </form>";
 }
echo $myRes;  

 //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) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 

 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find; 
 } 

?>
</body>

1 个答案:

答案 0 :(得分:1)

查看代码,myRes变量可能存在一个小问题。你只需要为每个结果反复设置它,你应该在while内部进行连接,而不仅仅是设置值。所以而不是:

$myRes = "......"

你应该这样做:

$myRes .= "......"

在使用它之前做一个init,做一些像

这样的事情
$myRes = ""; 
while (....){

希望这有助于您更接近答案。