添加到链接的变量为空

时间:2014-10-03 15:51:10

标签: php mysql mysqli

我从数据库中获取一个值,并使用PHP将其添加到<a>标记中。当我点击这样的链接时,我可以清楚地看到,href属性中只有一个静态部分,并且没有添加变量。

例如,我想要www.domain.com/client?client=SLCH12345678网址,其中SLCH12345678取自数据库(变量值),但我得到的只是www.domain.com/client?client=

这是我的代码:

$query = "SELECT customer_ref, f_name, l_name FROM client_details WHERE ".$searchtype." LIKE '%".$searchterm."%'";
$result = $db->query($query);

$customer_ref = $query['customer_ref'];
$num_results = $result->num_rows;

echo "<p>Number of clients found: ".$num_results."</p>";

for ($i=0; $i <$num_results; $i++)
{
    $row = $result->fetch_assoc();
    echo "<p><strong>".($i+1).". Name: ";
    echo "<a href='client.php?client=$customer_ref'>";
    echo htmlspecialchars(stripslashes($row['f_name']));
    echo "&nbsp;";
    echo htmlspecialchars(stripslashes($row['l_name']));
    echo "</a></strong>";
    echo "<br/>Address: ";
} 

我做错了什么?

1 个答案:

答案 0 :(得分:0)

你没有取任何东西,你只是在询问。您已标记mysqli,因此请使用mysqli

$result = $db->query($query);
$query = $result->fetch_assoc();

http://php.net/manual/en/mysqli-result.fetch-assoc.php

然后,您将遍历每个结果集。

$query = "SELECT customer_ref, f_name, l_name FROM client_details WHERE ".$searchtype." LIKE '%".$searchterm."%'";
$result = $db->query($query);

echo "<p>Number of clients found: ". $result->num_rows ."</p>";
$i = 1;
while($row = $result->fetch_assoc()) {
  echo "<p><strong>".($i).". Name: ";
  echo "<a href='client.php?client=". $row['customer_ref'] ."'>";
  echo htmlspecialchars(stripslashes($row['f_name']));
  echo "&nbsp;";
  echo htmlspecialchars(stripslashes($row['l_name']));
  echo "</a></strong>";
  echo "<br/>Address: ";
  $i++;
}