0

我是 PDO 和 PHP 的新手,我想知道如何从表中提取的信息中定义变量。

我有以下内容:

$UID = $_GET['id'];
$sth = $conn->prepare("SELECT * FROM directory WHERE user_active != '' AND ID = '$UID'");
$sth->execute();

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
    echo $row['First_Name'] . ' ' . $row['Surname'] . "\n";
    echo '<img src="http://maps.google.com/maps/api/staticmap?center=' . $row["Location_Postcode_Last_Seen"] . '&amp;zoom=1
    4&amp;size=200x200&amp;maptype=roadmap&amp;markers=color:ORANGE|label:A|' . $row["Location_Postcode_Last_Seen"] . '&amp;sensor=true">';
    echo $row["Nicknames"];
    echo $row["Age"];

}

如果我尝试添加:

var $name = echo $row['First_Name'] . ' ' . $row['Surname'];

在我的while循环中,代码无法向我的浏览器输出任何内容。

4

2 回答 2

2

分配一个变量是这样完成的,

$name = $row['First_Name'] . ' ' . $row['Surname']; //defining variable $name

并回显/显示它,应该这样做,

echo $name; //this will display whatever is in $name

echo问题中给出的while循环应该可以正常工作,只要结果集中存在这些键,就会显示您尝试输出的所有内容。

 while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo $row['First_Name'] . ' ' . $row['Surname'] . "\n";
echo '<img src="http://maps.google.com/maps/api/staticmap?center=' . $row["Location_Postcode_Last_Seen"] . '&amp;zoom=1
4&amp;size=200x200&amp;maptype=roadmap&amp;markers=color:ORANGE|label:A|' . $row["Location_Postcode_Last_Seen"] . '&amp;sensor=true">';
echo $row["Nicknames"];
echo $row["Age"];
}

编辑:(根据评论)

//assign all that you want to display to a variable $map, the equality operator '=' is used for assigning the right-hand side value to the left-hand side variable.
$map = '<img src="maps.google.com/maps/api/staticmap?center=' . $row["Location_Postcode_Last_Seen"] . '&amp;zoom=1 4&amp;size=200x200&amp;maptype=roadmap&amp;markers=color:ORANGE|label:A|' . $row["Location_Postcode_Last_Seen"] . '&amp;sensor=true">'; 

echo $map; //display $map

注意:变量需要被多次访问时才需要赋值。

于 2012-10-22T20:05:57.567 回答
0

var是仅用于在类中声明变量的关键字。那可能是它的问题:

echo $row['First_Name'] . ' ' . $row['Surname'];

或者

$name = $row['First_Name'] . ' ' . $row['Surname'];
echo $name;

此外,您使用的 preprared statmemnts 错误。它们的部分好处是自动引用您的参数。它应该类似于以下内容之一:

// position placeholders
$sth = $conn->prepare("SELECT * FROM directory WHERE user_active != '' AND ID = ?");
$sth->execute(array($UID));

或者

// names palceholders
$sth = $conn->prepare("SELECT * FROM directory WHERE user_active != '' AND ID = :uid");
$sth->execute(array(':uid' => $UID));
于 2012-10-22T20:08:22.040 回答