douleng0794 2012-10-22 20:04 采纳率: 0%
浏览 18
已采纳

从另外两个变量定义变量

I'm new to PDO and PHP, I'm wondering how I would go about defining vars from the information being pulled from my table.

I have the following:

$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'] . "
";
    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"];

}

If I try adding:

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

within my while loop the code fails to output anything to my browser.

  • 写回答

2条回答 默认 最新

  • douruye5092 2012-10-22 20:05
    关注

    assigning a variable is done like this,

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

    and echo/displaying it, should be done like,

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

    the while loop, as given in the question should work fine, displaying all that you are trying to echo out, as long as those keys exist in the resultset.

     while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
    echo $row['First_Name'] . ' ' . $row['Surname'] . "
    ";
    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"];
    }
    

    Edit: (as per comment)

    //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
    

    Note: Variables need to be assigned only if they need to be accessed multiple times.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启