0

I must be missing something very obvious here, but I don't understand what I am doing wrong here. For some reason, $row is coming out empty, which should not happen. I am getting no error though.

$uname=mysqli_real_escape_string($conn,$_SESSION["username"]);
$query1="SELECT user_id FROM users WHERE username='$uname'";

$results=$conn->query($query1);
if($results->num_rows>0){
    while($row=$results->fetch_assoc()){
        $uid=mysqli_real_escape_string($row["user_id"]);
    }
}

I am sorry if this is a bad question, I will delete it right after, but I would appreciate any help.

7
  • You're not actually checking for errors, so how do you know? Commented Jun 22, 2017 at 19:27
  • echo your query try to run in PHPMyAdmin. is it returning some data? Commented Jun 22, 2017 at 19:29
  • I would try $rowcount=mysqli_num_rows($result) Commented Jun 22, 2017 at 19:32
  • You are wide open for SQL injection. Since you're using mysqli, take advantage of prepared statements and bind_param. And you'll never have to worry about pesky quoting issues again. Commented Jun 22, 2017 at 19:42
  • Actually, no error is being displayed in the log, so I thought I might not have errors. I am still learning. Thank you for the links you provided. @aynber Commented Jun 22, 2017 at 19:53

2 Answers 2

2

No question is a bad question imo..we are all here to learn isnt it?I'd rather construct that code this way:

$uname=mysqli_real_escape_string($conn,$_SESSION["username"]);
$query1=mysqli_query($conn, "SELECT user_id FROM users WHERE username='$uname'");

if ($query1){
   while ($row = mysqli_fetch_assoc($query1){
      $user_id = $row['user_id'];
      echo $user_id;
   }
}
Sign up to request clarification or add additional context in comments.

5 Comments

This is working. I did not know we could do it like this. Thank you! I need to read up more.
And about the bad question thing, it happened to me once before, it was a question like this and I had to delete it without ever getting an answer.
It happened to me too severally, but the key is never to get deterred.
I try not to. Btw, I did understand what you did, but could you tell me what I was doing wrong?
This line $uid=mysqli_real_escape_string($row["user_id"]); was unneccessary. I guess it broke ur code. Try with $uid = ($row['user_id']);
1

Try this

    $uname=mysqli_real_escape_string($conn,$_SESSION["username"]);
$query1="SELECT user_id FROM users WHERE username=
'$uname'";

$results=mysqli_query($conn, $query1);
if(mysqli_num_rows($results) >0){
    $row=$results->fetch_assoc();
    $uid=$row["user_id"];
}

4 Comments

Bad concatenation there. You don't need the periods.
Echo ur query and check
@aynber try it now.
I tried this, but this is not working for some reason. Can you please explain what you have done?

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.