1

I'm trying to show icon.envelop.2.gif if there are any messages with message_status = 2. However for some obscure reason this query returns 0. What have I missed here?

The $_SESSION['user_id'] is valid, and so is $access_level. I have checked my table and I have 1 input with the correct message_receive number and I have also tried using message_to_stab = 5 which is the correct number.

This is the information in my database:

  • message_id = 1
  • message_receive = 697
  • message_status = 2
  • message_to_stab = 5 or message_to_stab = NULL

This is my information in session and access_level:

  • access_level = 5
  • $_SESSION['user_id'] = 697

This is my code:

$sql = "SELECT message_id FROM private_messages WHERE message_status = 2 AND (message_receive = ? OR message_to_stab = ?)";
$stmt = $mysqli->prepare($sql) or die ("Database error: <br>" . $sql . "<br><b>Error message:</b> " . $mysqli->error);
$stmt->bind_param("ii", $_SESSION['user_id'], $access_level);
$stmt->execute() or die("Something went wrong");

if($stmt->num_rows != 0){
    $html .= 'icon.envelope.2.gif';
}
else{
    $html .= 'icon.envelope.gif';
}

$stmt->free_result();
$stmt->close();

Here is the problem, no errors, I want BOTH mailing symbols to be like the one under status.

Image showing messaging error

1
  • @Gjert I. Gjersund please try my answer once. and tell if any problem is still there. thanks Commented Jun 10, 2015 at 12:22

1 Answer 1

0

I managed to use $stmt->fetch() to solve the problem. I had to change the query to LIMIT 1 in order to prevent errors when more than 1 unread message available.

if($stmt->fetch()){
    $html .= 'icon.envelope.2.gif';
}
else{
    $html .= 'icon.envelope.gif';
}

PS: Better answers are more than welcome. I still dont know why rowCount() or num_rows didn't work.

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

2 Comments

@anantkumarsingh I haven't been able to test it yet, but does rowCount() not work because there is only 1 column selected?
No that is not the case. i never got this. rowCount() must work either record is 1 or something else.

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.