[英]PHP loop not returning all possible MySQL query results
I am new to php & MySQL. 我是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). 我正在使用php从数据库中检索数据(我已经与数据库建立了连接)。
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. 我要达到的目的是:如果查询一个人的名字(无论是fname还是lname),那么该人拥有的所有属性的列表将显示在我的搜索结果表单中。 The output will be shown as lname, fname, property number, property road, property zipcode.
输出将显示为lname,fname,属性编号,属性道路,属性邮政编码。
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. 示例:Adam Smith拥有4个属性,但是我只会得到他的两个属性的结果,而不是列表中的所有4个结果。
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.
先感谢您。
What is $uqery
? 什么是
$uqery
? Maybe: 也许:
while ($nrow = mysql_fetch_array($request)){
You would see this with: 您将看到以下内容:
error_reporting(E_ALL);
ini_set('display_errors', '1');
As for all the other stuff it's impossible to tell without knowing what $propertyinfo
looks like. 至于所有其他东西,要知道
$propertyinfo
是什么样子是不可能的。 Very convoluted looking so I don't follow. 非常令人费解的外观,所以我不关注。
I think it's a logical error; 我认为这是一个逻辑错误; when you defined $propertyinfo=array();
当您定义$ 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.
它的大小为0,因此,带有for($ l = 0; $ l <count($ propertyinfo); $ l)块的代码仅在将某些值分配给$ propertyinfo后才开始运行。
尝试使用while(($nrow=mysql_fetch_assoc($query))!=null)
循环查询结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.