-1

I created folowing view (select list of last active users):

SELECT U.login, U.name, U.surname
FROM sessions S LEFT JOIN user U 
ON S.id_user = U.id_user 
WHERE U.id_user != 0 AND UNIX_TIMESTAMP()-S.set_time < 300
ORDER BY S.set_time DESC

SELECT *FROM vonline; gives me(i call it in phpmyadmin):

login   name    surname
admin   Chuck   Norris
user2   John    Cena

I am trying to get same output in php:

if ($stmt = $mysqli->prepare("SELECT * FROM vonline")) {
    $stmt->execute();

    $result = $stmt->get_result();
    echo "Online users: $stmt->num_rows"; // This shows "0"
    while ($row = $result->fetch_array(MYSQLI_NUM)) {
        echo "Login: $row[1] Name: $row[1] Surname: $row[2]";
    }

} else {
    echo "Select Error";
}

Why get no results, num_rows returns 0.

The code above works perfectly for selecting data from other tables, but not for this one.

user3464829
  • 69
  • 1
  • 9
  • *"I created folowing view (select list of last active users):"* - what does this have to do with your querying the `vonline` table? and try getting rid of `MYSQLI_NUM` and which MySQL API are you using to connect with? `mysqli_` I hope. – Funk Forty Niner Nov 01 '15 at 15:21

1 Answers1

1

Firstly, you're passing your code inside a string literal, rather than an executable.

echo "Online users: $stmt->num_rows"; // This shows "0"

That should read as

echo "Online users: " . $stmt->num_rows;

and using concatenation.

Example from the manual:

if ($result = $mysqli->query("SELECT Code, Name FROM Country ORDER BY Name")) {

    /* determine number of rows result set */
    $row_cnt = $result->num_rows;

    printf("Result set has %d rows.\n", $row_cnt);

    /* close result set */
    $result->close();
}

However, get_result() may not be available for you to use.

Consult this Q&A on Stack:

Also make sure you're using the same MySQL API to connect with as your query.

Check for errors also against your db and query:


Edit:

This does not help you if there are errors in your query:

else {
    echo "Select Error";
}

Change $stmt->execute(); to read as

if(!$stmt->execute()){
    trigger_error("there was an error....".$mysqli->error, E_USER_WARNING);
    }

Or use

echo("Error description: " . mysqli_error($mysqli)); 

in your else.

Plus, make sure you did select the right "database".

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • I am using " not ' in echo, so i can use variables inside "", concatenation or my way does not make difference. I am using same MySQL API to connect and query, i have already a lot of queries in my project, every works fine. Also $mysqli->error; is not set because i get no errors, just empty result of query. – user3464829 Nov 01 '15 at 14:57
  • @user3464829 I made an edit to my answer. Reload it and see under **Edit:**. – Funk Forty Niner Nov 01 '15 at 15:05
  • @user3464829 or change `$stmt->num_rows` to `$result->num_rows` pretty that's what the issue is. checking for errors would have triggered something. – Funk Forty Niner Nov 01 '15 at 15:06
  • @user3464829 I found some more information that may be relevant to the problem. Do reload my answer again and read it in its entirety. – Funk Forty Niner Nov 01 '15 at 15:15
  • `$stmt->execute();` works with no errors. You are right about `num_rows`, i used wrong variable, but i used $result for `num_rows` before and had 0 result too. Also now checking for errors everywhere. Still 0 errors and 0 result. I am sure that i am using good database, also this code works well, (`SELECT *FROM users` works but `SELECT *FROM vonline;` not). – user3464829 Nov 01 '15 at 15:56
  • @user3464829 if `SELECT *FROM vonline;` is what you're really using, remove the space from in there `SELECT * FROM vonline;` and make sure you have data in that table. – Funk Forty Niner Nov 01 '15 at 16:04
  • I have space in my code, and i am 100% sure that data is in table. I call `SELECT * FROM vonline;` in phpmyadmin and navicat, both give me 2 rows. Thats really strange, looks like i have well working php code and well working view. I tried also put `vonline` code into php query, doesn't work too... – user3464829 Nov 01 '15 at 16:10
  • When i remove `AND UNIX_TIMESTAMP()-S.set_time < 300` from `vonline` code i get one of expected rows... – user3464829 Nov 01 '15 at 16:13
  • @user3464829 and you are using `mysqli_` to connect with, correct? I can't see this from failing. Plus, as I stated in a comment under your question, you mention about querying another table. If you're not showing us something related to multiple table query, then that could be a contributing issue here. I'm sorry, but at this point there isn't anything else I can think of that will cause your query to fail. – Funk Forty Niner Nov 01 '15 at 16:14
  • @user3464829 ok well you didn't put your real query in your 2nd body of code. You only put `"SELECT * FROM vonline"` and nothing else related to the joined table(s). You will need to figure those out yourself and as to which query should be used in conjunction with the `vonline` table. I won't be able to offer further help on this, sorry. – Funk Forty Niner Nov 01 '15 at 16:16
  • This one script have only `vonline` query, i am just saying that if i replace `vonline` to table name then everything works fine. – user3464829 Nov 01 '15 at 16:17
  • @user3464829 what do you mean by *" if i replace vonline to table name"*? I don't get that. – Funk Forty Niner Nov 01 '15 at 16:18
  • If i change to `if ($stmt = $mysqli->prepare("SELECT * FROM vonline")) {` to `if ($stmt = $mysqli->prepare("SELECT * FROM user")) {` i mean. – user3464829 Nov 01 '15 at 16:25
  • @user3464829 well then, your query outright failed. Sorry, but I have to move on now. There isn't anything else I can do for you here. I did my best to help. Good luck and I sincerely wish you well. *Cheers* – Funk Forty Niner Nov 01 '15 at 16:26
  • Thank you very much for help. I invited a friend and find out now that this view returns all users except me, problem is that when page reload my session class delete row with my session for a moment. – user3464829 Nov 01 '15 at 16:41
  • @user3464829 You're welcome and I'm happy to hear that this was finally solved. So, it had something to do with sessions then. – Funk Forty Niner Nov 01 '15 at 16:42