Liam
Liam

Reputation: 9855

Define a variable from two other variables

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

}

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.

Upvotes: 0

Views: 649

Answers (2)

Teena Thomas
Teena Thomas

Reputation: 5239

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

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.

Upvotes: 2

prodigitalson
prodigitalson

Reputation: 60413

var is a keyword only used to declare variables in a class. Thats probably the problem it would be:

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

or

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

Additionally youre using preprared statmemnts wrong. Part of the benefit of them is getting your parameters quoted automatically. It should look something like one of the following:

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

or

// names palceholders
$sth = $conn->prepare("SELECT * FROM directory WHERE user_active != '' AND ID = :uid");
$sth->execute(array(':uid' => $UID));

Upvotes: 0

Related Questions