0

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

3
  • Use this mysqli_num_rows() Commented 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. Commented Oct 2, 2016 at 9:15
  • Chk $con are u getting connection resource? Commented Oct 2, 2016 at 9:20

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.