-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.

5
  • 1
    And what have you tried to debug the problem? Have you checked whether that query is valid? Whether MySQL reports errors? Commented May 20, 2019 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. Commented May 21, 2019 at 4: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 & link; both give similar levels of detail similar questions similarly phrased, not much debugging info given. They seem to have been warmly received. Commented May 21, 2019 at 4: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. Commented May 21, 2019 at 6:15
  • Does this answer your question? Why shouldn't I use mysql_* functions in PHP? Commented Nov 1, 2019 at 0:41

2 Answers 2

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.

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

1 Comment

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.
-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));

3 Comments

Don't you think that the result would be completely different if mysql* would not be available?
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).
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"

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.