Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php/Mysql while-loop-Div不会对每个可能的结果重复。只显示最后一个结果?(有一些代码)_Php_Mysql_Loops_While Loop - Fatal编程技术网

Php/Mysql while-loop-Div不会对每个可能的结果重复。只显示最后一个结果?(有一些代码)

Php/Mysql while-loop-Div不会对每个可能的结果重复。只显示最后一个结果?(有一些代码),php,mysql,loops,while-loop,Php,Mysql,Loops,While Loop,我遇到了从mysql数据库中提取数据的while循环问题。本质上,用户搜索一个字符串,然后将该字符串与数据库中可能的名称进行比较。如果字符串与数据库中的结果匹配,则返回该行,并在其“自己的div”中显示与结果相关的信息 以下是与while循环相关的有点错误的代码: (LOTS OF CODE RELATING TO EVERYTHING ELSE BEFORE THIS) $tempVar = 0; while ($data = mysql_fetch_array($data))

我遇到了从mysql数据库中提取数据的while循环问题。本质上,用户搜索一个字符串,然后将该字符串与数据库中可能的名称进行比较。如果字符串与数据库中的结果匹配,则返回该行,并在其“自己的div”中显示与结果相关的信息

以下是与while循环相关的有点错误的代码:

    (LOTS OF CODE RELATING TO EVERYTHING ELSE BEFORE THIS)
    $tempVar = 0;
while ($data = mysql_fetch_array($data)) {
    $lstId = $data['id'];
    $lstName = $data['name'];
    $lstDesc = $data['description'];
    $lstAddress = $data['address'];
    $lstArea = $data['area'];
    $lstProvince = $data['province'];
    $lstPostcode = $data['postcode'];
    $lstTel = $data['tel'];
    $lstCell = $data['cell'];
    $lstFax = $data['fax'];
    $lstEmail = $data['email'];

if ($tempVar == 0) {
    $pageContent .= '
        <div class="searchResultS">
    ';
    }
    $pageContent .= '
    <div class="searchResult">
    <h2>'.$lstName.'</h2>
        <div class="bizDesc">
            <p>Description: '.$lstDesc.'</p>
        </div>
        <div class="bizAddr">
            <p>Street: '.$lstAddress.'</p>
            <p>Town: '.$lstArea.'</p>
            <p>Province: '.$lstProvince.'</p>
            <p>Post Code: '.$lstPostcode.'</p>
        </div>
        <div class="bizCont">
            <p>Tel: '.$lstTel.'</p>
            <p>Cell: '.$lstCell.'</p>
            <p>Fax: '.$lstFax.'</p>
            <p>eMail: '.$lstEmail.'</p>
        </div>
    </div>
';
  $tempVar ++;
 }

if ($anymatches > 0 and count($tempVar) == count($anymatches)) {
        $pageContent .= '
    <!-- end .searchResultS --></div>
';
}
(大量与此之前的所有内容相关的代码)
$tempVar=0;
而($data=mysql\u fetch\u数组($data)){
$lstId=$data['id'];
$lstName=$data['name'];
$lstDesc=$data['description'];
$lstAddress=$data['address'];
$lstArea=$data['area'];
$lstProvince=$data['province'];
$lstPostcode=$data['postcode'];
$lstTel=$data['tel'];
$lstCell=$data['cell'];
$lstFax=$data['fax'];
$lstEmail=$data['email'];
如果($tempVar==0){
$pageContent.='
';
}
$pageContent.='
“.$lstName。”
说明:'.$lstDesc'

街道:‘.$L地址。’

城镇:“.$LST地区。”

省:‘.$LST省。’

邮政编码:'.$L邮政编码'

电话:‘.$lstTel。’

单元格:'.$lstCell'

传真:'.$lstFax'

电子邮件:'.$lstmail'

'; $tempVar++; } 如果($anymatches>0和count($tempVar)=count($anymatches)){ $pageContent.=' '; }
我遇到的问题是,当用户搜索包含两个或更多可能结果的字符串时。似乎只显示最后一个结果。并不是所有可能的结果,每个结果都在它们自己的包含div中。我知道有更多的结果,因为生成的一行显示了返回的mysql行数

有没有人对这个问题有什么建议或解决办法?我假设有些东西被遗漏了,但经过数小时的反复试验,我没有找到确切的理由来解释为什么目前存在这种行为


任何帮助都将不胜感激,谢谢

看起来您在第一次循环迭代中将结果资源
$data
覆盖到一个数组。在这个迭代之后,
$data
不能再作为结果资源进行访问,因此它将显示为您只有一个结果

while ($data = mysql_fetch_array($data)) {

// Instead use a new variable:
while ($row = mysql_fetch_array($data)) {
//-----^^^^

// Then change all of these
$lstId = $row['id'];
$lstName = $row['name'];
$lstDesc = $row['description'];
...
...
$lstEmail = $row['email'];
您应该在开发中打开
display\u errors
,因为我希望您在第一次循环迭代后收到此警告:

警告:mysql_fetch_array()希望参数1是资源


@Micheal谢谢你的提示,我会应用它,我只是刚刚想出了如何让我的.htaccess覆盖主机默认的php设置