I have this function
function getNick($uid)
{
$sqli = "SELECT nick FROM users WHERE userid='".$uid."'";
mysqli_real_escape_string($con,$sqli);
$resulti = mysqli_query($con,$sqli);
$rowi = mysqli_fetch_assoc($resulti);
if($resulti->num_rows > 0) return $rowi["nick"];
else return "(none)";
}
Basically it should return me nick based on user's id. Problem is that I only keep getting '(none)'. What is interesting I printed actual $sqli and copied it into phpMyAdmin and it worked as expected. I even tried to just print nick without IFs but I ended up with empty string. What might be the issue? Am I overlooking something? Thanks
-
Use this mysqli_num_rows()devpro– devpro2016年10月02日 09:13:46 +00:00Commented Oct 2, 2016 at 9:13
-
It didn't work and problem has to be somewhere else as I had echo under mysqli_fetch_assoc and it was empty string.Martin– Martin2016年10月02日 09:15:49 +00:00Commented Oct 2, 2016 at 9:15
-
Chk $con are u getting connection resource?devpro– devpro2016年10月02日 09:20:56 +00:00Commented Oct 2, 2016 at 9:20
1 Answer 1
<?php
$con = mysqli_connect("localhost","root","","test");
function getNick($uid,$con)
{
$sqli = "SELECT nick FROM users WHERE userid='".$uid."'";
mysqli_real_escape_string($con,$sqli);
$resulti = mysqli_query($con,$sqli);
$rowi = mysqli_fetch_assoc($resulti);
if($resulti->num_rows > 0) return $rowi["nick"];
else return "(none)";
}
echo getNick(1,$con);
?>
it works
variable scope problem
use above method to pass connection in method or
use $GLOBALS['con'] to access connection in method getNick
answered Oct 2, 2016 at 9:24
asissuthar
2,2561 gold badge18 silver badges30 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default