mysqli - view works fine, but return 0 rows when call in php -


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

select u.login, u.name, u.surname sessions s left join user u  on s.id_user = u.id_user  u.id_user != 0 , unix_timestamp()-s.set_time < 300 order s.set_time desc 

select *from vonline; gives me(i call in phpmyadmin):

login   name    surname admin   chuck   norris user2   john    cena 

i trying same output in php:

if ($stmt = $mysqli->prepare("select * vonline")) {     $stmt->execute();      $result = $stmt->get_result();     echo "online users: $stmt->num_rows"; // shows "0"     while ($row = $result->fetch_array(mysqli_num)) {         echo "login: $row[1] name: $row[1] surname: $row[2]";     }  } else {     echo "select error"; } 

why no results, num_rows returns 0.

the code above works selecting data other tables, not one.

firstly, you're passing code inside string literal, rather executable.

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

that should read as

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

and using concatenation.

example manual:

if ($result = $mysqli->query("select code, name country order 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 available use.

consult q&a on stack:

also make sure you're using same mysql api connect query.

check errors against db , query:


edit:

this not if there errors in query:

else {     echo "select error"; } 

change $stmt->execute(); read as

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

or use

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

in else.

plus, make sure did select right "database".


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -