Jump to content

Setting variable from database results


genista

Recommended Posts

Hi all,

 

I have a most strange problem, and I am not sure if it is because of a long overdue move to mysqli from mysql or something else that is ludicrously simply wrong with what I am doing. I am doing a select on name from users where the userid = the sessions userid.

 

From there I want to set the $personsname variable from the name column - simple.

 

The query below runs fine without error and I can print out the name, but what I cannot do is set the variable up:

 

$query1 = "SELECT name FROM `users` WHERE `userid`=$userid";
$result = $mysqli->query($query1) or die($mysqli->error.__LINE__);
// GOING THROUGH THE DATA
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo stripslashes($row['name']);
$personsname=stripslashes ($row['$name']); = FRED BLOGS - correct
echo "<p>name=$personsname</p>"; = EMPTY?!?!?!?!

 

I am sure the answer is embarrassingly simple, but I just cant get my head around this..

 

Thanks,

 

G

Link to comment
https://forums.phpfreaks.com/topic/274495-setting-variable-from-database-results/
Share on other sites

$personsname=stripslashes ($row['$name'])

 

if only one row why use a while loop?

 

edit:

If you have to use stripslashes then your data handling is faulty

 

See http://forums.phpfreaks.com/topic/273999-importing-a-quoted-db-into-a-non-quoted-db/?do=findComment&comment=1409998

Change this:

$personsname=stripslashes ($row['$name']); = FRED BLOGS - correct

 

To this:

$personsname=stripslashes ($row['name']); = FRED BLOGS - correct

 

*Edit: Barand beat me to it...

 

*Edit 2: As Barand said, you don't need WHILE() for a single returned row.

<?PHP
 if($result->num_rows > 0) {
   $row = $result->fetch_assoc();

   echo stripslashes($row['name']);
   $personsname = stripslashes($row['name']); //### FRED BLOGS - correct
   echo "<p>name=$personsname</p>"; //### Empty?
 }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.