PHP C或在数组中存储sql查询

时间:2013-06-09 01:13:58

标签: php sql

我有一个查询从我的数据库中获取了许多cols

$searchResults = mysql_query("SELECT customerRefId, seekingAddressSuburb, 
  seekingAddressPostcode, seekingAddressState FROM customer_seeking");

然后我使用数据显示HTML表格

?>
<table border="1">
<th>Customer Ref ID</th>
<th>Suburb</th>
<th>State</th>
<th>Postcode</th>
<?php
while($row=mysql_fetch_array($searchResults))
{
echo"<tr><td>$row[customerRefId]</td><td>$row[seekingAddressSuburb]</td><td>$row[seekingAddressPostcode]</td><td>$row[seekingAddressState]</td></tr>";
}
?>
</table>

我接下来需要做的是如下所示另一个查询,但不是硬编码9,10我想将$row[customerRefId]用于IN ()

我应该使用mysql_data_seek($searchResults,0);返回到sql结果的顶部还是应该构建一个arrary?

$searchResults2 = mysql_query("SELECT customerRefId, firstName, lastName, email, phone, mobile FROM customer_contact WHERE customerRefId IN (9,10)");

1 个答案:

答案 0 :(得分:1)

首先,每个人都在评论中说mysql_ *函数已被弃用,你需要切换到mysqli。

要解决您的问题,您需要的是JOIN。使用JOIN,您可以从两个表中合并数据。这是一个例子:

SELECT 
    customerRefId,
    seekingAddressSuburb, 
    seekingAddressPostcode, 
    seekingAddressState,
    firstName,
    lastName,
    email,
    phone,
    mobile
FROM 
    customer_seeking cs
LEFT JOIN
    customer_contact cc ON cc.customerRefId = cs.customerRefId

每次循环一行时,您将立即获得所有数据!