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

Edited by Barand
Link to comment
Share on other sites

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?
 }
?>

Edited by PaulRyan
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.