douchi0638 2014-03-18 22:33
浏览 32
已采纳

PHP循环不返回所有可能的MySQL查询结果

I am new to php & MySQL. I have a db that contains two tables called person (property owner) and property. There is a one-to-many relationship between the tables. For instance, a person can own many properties.

I am using php to retrieve data from the db (I have already established a connection with the db).

What I am trying to achieve is this: if a query is carried out for a person's name - (whether it is fname or lname) - then a list of all the properties owned by this person will be displayed in my search results form. The output will be shown as lname, fname, property number, property road, property zipcode.

However, with this code I am only getting two results output per property owner name. :

$request = mysql_query("SELECT person.fname, person.lname, property.number, property.road, property.zipcode, 
    FROM person p
        INNER JOIN property py 
            ON p.id = py.p_id
    WHERE p.fname LIKE  '%$search%' 
            OR p.lname LIKE  '%$search%' 
            ORDER BY p.lname");

$number = mysql_num_rows($request);
    if ($number == 0){
        $result .= 'No results'.'  '.$search;
    } else { 

    $propertyinfo= array();
    $count = 0;

    $real = false;
    $real_within_array = 0;


    while ($nrow = mysql_fetch_array($query)){
        $lname = $nrow['lname'];
        $real = false;
        for ($l = 0; $l < count($propertyinfo); $l++)
        {
            if ($propertyinfo[$l][0] == $lname)
            {
                $real = true;
                $real_within_array = $l;
                break;
            }
        }

        if ($real)
        {
            $fname = $nrow['fname'];
            $lname = $nrow['lname'];
            $number = $nrow['number'];
            $road = $nrow['road'];
            $zipcode = $nrow['zipcode'];



            $propertyinfo[$real_within_array][1][1] = $fname;
            $propertyinfo[$real_within_array][2][1] = $lname;
            $propertyinfo[$real_within_array][3][1] = $number;
            $propertyinfo[$real_within_array][4][1] = $road;
            $propertyinfo[$real_within_array][5][1] = $zipcode;

        }
        else
        {
            // Get all the data from the row.
            $fname = $nrow['fname'];
            $lname = $nrow['lname'];
            $number = $nrow['number'];
            $road = $nrow['road'];
            $zipcode = $nrow['zipcode'];

            $propertyinfo[$real_within_array][1] = $fname;
            $propertyinfo[$real_within_array][2]= $lname;
            $propertyinfo[$real_within_array][3][0] = $number;
            $propertyinfo[$real_within_array][4][0] = $road;
            $propertyinfo[$real_within_array][5][0] = $zipcode;
            $count++;
        }
    }

Example: Adam Smith owns 4 properties, but I will only get a result of two of his properties instead of all 4 in a list.

Please help. I have tried my very best to sort this out but I seem to have stumbled upon a difficult problem.

Your help is much appreciated. Thank you in advance.

  • 写回答

3条回答 默认 最新

  • dongsu1951 2014-03-18 23:15
    关注

    I think it's a logical error; when you defined $propertyinfo=array(); it's size is 0, therefore the code withing the block of for ($l =0; $l< count($propertyinfo); $l) will only start running after some values has been assigned to $propertyinfo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法