genista Posted February 14, 2013 Share Posted February 14, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/274495-setting-variable-from-database-results/ Share on other sites More sharing options...
Barand Posted February 14, 2013 Share Posted February 14, 2013 (edited) $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 February 14, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274495-setting-variable-from-database-results/#findComment-1412504 Share on other sites More sharing options...
PaulRyan Posted February 14, 2013 Share Posted February 14, 2013 (edited) 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 February 14, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/274495-setting-variable-from-database-results/#findComment-1412506 Share on other sites More sharing options...
genista Posted February 14, 2013 Author Share Posted February 14, 2013 Thanks guys, spot on - I have taken note of your comments on the for each and changed accordingly. G Quote Link to comment https://forums.phpfreaks.com/topic/274495-setting-variable-from-database-results/#findComment-1412515 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.