我是 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"] . '&zoom=1
4&size=200x200&maptype=roadmap&markers=color:ORANGE|label:A|' . $row["Location_Postcode_Last_Seen"] . '&sensor=true">';
echo $row["Nicknames"];
echo $row["Age"];
}
如果我尝试添加:
var $name = echo $row['First_Name'] . ' ' . $row['Surname'];
在我的 while
循环中,代码无法向我的浏览器输出任何内容。
最佳答案
分配一个变量是这样完成的,
$name = $row['First_Name'] . ' ' . $row['Surname']; //defining variable $name
并回显/显示它,应该这样做,
echo $name; //this will display whatever is in $name
问题中给出的 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"] . '&zoom=1
4&size=200x200&maptype=roadmap&markers=color:ORANGE|label:A|' . $row["Location_Postcode_Last_Seen"] . '&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"] . '&zoom=1 4&size=200x200&maptype=roadmap&markers=color:ORANGE|label:A|' . $row["Location_Postcode_Last_Seen"] . '&sensor=true">';
echo $map; //display $map
注意:只有在需要多次访问变量时才需要对变量进行赋值。
关于php - 从另外两个变量定义一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13018949/