0

我有一个网页,它向我的 MySQL 数据库发送查询以检索有关我公司供应商的信息并回显结果(到目前为止一切都很好)。然后它会进行第二次查询以检查是否有任何已知联系人与该供应商相关联:

while ($row = mysqli_fetch_array($result)) {
    $contact = $row["contact"];
    echo $row["name"] . "<br>" . $row["category"] . "<br>" . "Telephone: " .
        $row["telephone"] . "<br>" . "Fax: " . $row["fax"] . "<br>" .
        $row["email"] . "<br>" . $row["address"] . "<br>" .
        $row["postcode"] . "<br><br>" . "Contact: " .
        $contact . "<br>";
    $resultContact = mysqli_query($con,"SELECT * FROM contacts WHERE name =
        '$contact'");
    while($rowContact = mysqli_fetch_array($resultContact)) {
        echo $rowContact["telephone"] . "<br>" . $rowContact["email"];
        echo "<br>";
    }
}

$row来自第一个查询并且有效。)但是,echo $rowContact["telephone"] . "<br>" . $rowContact["email"];只打印空格。

谢谢

编辑:如果我在 Where 子句中使用字符串文字,它可以正常工作。但是,我使用变量的方式与我现在在第一个查询中的方式完全相同,并且有效!我很困惑 :(

编辑2:第一个查询:$result = mysqli_query($con,"SELECT * FROM suppliers WHERE LOWER(name) = LOWER('$searchText')");

编辑 3:执行的结果var_dump($resultContact);object(mysqli_result)#3 (5) { ["current_field"]=> int(0) ["field_count"]=> int(4) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) }我猜这意味着查询返回的表正常,但是 Where 子句没有按照我的意愿比较联系人姓名......

4

1 回答 1

0

您的查询必须是 mysql_query,而不是 mysqli_query。这是正确的代码:

//Your db connection
mysql_connect('localhost','root','');
mysql_select_db('database');

//queries and data
$result = mysql_query("SELECT * FROM suppliers WHERE LOWER(name) = LOWER('$searchText')");
while ($row = mysql_fetch_array($result)) {
    $contact = $row["contact"];
    echo $row["name"] . "<br>" . $row["category"] . "<br>" . "Telephone: " .
        $row["telephone"] . "<br>" . "Fax: " . $row["fax"] . "<br>" .
        $row["email"] . "<br>" . $row["address"] . "<br>" .
        $row["postcode"] . "<br><br>" . "Contact: " .
        $contact . "<br>";
    $resultContact = mysql_query("SELECT * FROM contacts WHERE name =
        '$contact'");
    while($rowContact = mysql_fetch_array($resultContact)) {
        echo $rowContact["telephone"] . "<br>" . $rowContact["email"];
        echo "<br>";
    }
}
于 2013-09-05T12:59:55.260 回答