-2

I have just used a select with a select for the first time. It seems to work and I am getting the desired result when I run it in phpMyAdmin. However, when I then use $variable = mysql_num_rows($queryresult); I get nothing. I guess it is null or something as it won't echo. This is the query:

$resultxl = mysql_query(select * from (Select * from mon_content_lid where mon_date_last!='0000-00-00' ORDER BY lid, mon_date_last desc) as x group by `lid`);
$numx1 = mysql_num_rows($resultxl);
echo $numx1;

No result.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Donquick
  • 1
  • 1
  • 1
    And what have you tried to debug the problem? Have you checked whether that query is valid? Whether MySQL reports errors? – Nico Haase May 20 '19 at 12:35
  • Hm, as I say, the query gave the correct result in phpMyAdmin without errors , so I think that it is working. I see from the answer below that I am using a 'cursor'. I think perhaps that is the issue here? Perhaps this doesn't work with mysql_num_rows. I will investigate this. – Donquick May 21 '19 at 04:42
  • Not sure what I have done to get downvoted. As i am new to this, I based my question on these two posts: [link](https://stackoverflow.com/questions/23741534/sqlsrv-num-rows-not-returning-any-value) & [link](https://stackoverflow.com/questions/20300582/display-sql-query-results-in-php); both give similar levels of detail similar questions similarly phrased, not much debugging info given. They seem to have been warmly received. – Donquick May 21 '19 at 04:50
  • Two hints about your code: a) please share code without syntax errors - the current code is not well formed, as it won't do anything. Really, nothing, it won't compile. b) Check whether `mysql_error` provides an error message. Don't rely on whatever phpMyAdmin tells you - check for errors in your code. – Nico Haase May 21 '19 at 06:15
  • Does this answer your question? [Why shouldn't I use mysql\_\* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Dharman Nov 01 '19 at 00:41

2 Answers2

0

If I see it right (didn't tried it out), you do a SELECT an put the result in a virtual column x, so in my opinion, x should be the only delivered column. This one you group by 'lid'.

select * from (...) as x group by `lid`

So 'lid' is, if it is quoted, a text, not a column-name. Try the query qithout the "group" and dump the cursor, maybe you see some results.

  • Thank you - i didn't realise i was creating a cursor: i just copied the select within a select from elsewhere as the result i wanted was similar to the example. I will investigate the cursor. – Donquick May 21 '19 at 04:51
-1

Don't use mysql, use the mysqli functions. If i'm right you can't use the mysql functions in php7 anymore

$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$sql = "Here your sql query";
$query_result = mysqli_query($db, $sql);
$number = mysqli_num_rows($query_result);
echo $number;

I didn't test is, but i think this should work. If it still don't work try to see if there are any errors.

die(mysqli_error($db));
Baracuda078
  • 677
  • 1
  • 5
  • 10
  • Don't you think that the result would be completely different if `mysql*` would not be available? – Nico Haase May 20 '19 at 12:36
  • Thanks for answering. No, this is not the issue. I have many instances of mysql which work fine. (We have not updated the site yet). – Donquick May 21 '19 at 04:57
  • And what if you write the query just like this "select * from mon_content_lid where mon_date_last != '0000-00-00' group by lid order by lid, mon_date_last desc" – Baracuda078 May 21 '19 at 08:49