0

I am troubleshooting this query that I was previously trying to use to count rows but now I have discovered that 0 rows are being found in the database even though they exist. I have checked previous queries I have used and they work and look the same. The $db_conx definitely works to connect to the database as I am using it on other web pages. Does any one have any suggestions? Thanks

$result =("SELECT * FROM Request WHERE user2='joey' AND accepted='0'");
$query = mysqli_query($db_conx, $result);
$rows = mysqli_num_rows($query);
if (rows < 1) {
    echo '<strong style="color:#F00;">No rows</strong>';
    exit();
} else {
    echo '<strong style="color:#F00;">Rows exist</strong>';
    exit();
}
4
  • And rows not equal $rows. Commented Oct 23, 2015 at 17:00
  • @AbraCadaver Sorry I typed rows wrong on here and it does say rows in my code I am compiling. I have tried SELECT * still nothing Commented Oct 23, 2015 at 17:01
  • @AbraCadaver I had an answer ready for this, but I'm hesitant to click on "Post Your Answer". Edit: good thing I didn't. Commented Oct 23, 2015 at 17:01
  • actually, I decided to. another error. Commented Oct 23, 2015 at 17:03

2 Answers 2

2

You are missing an important step to select something from the database and that is what are you trying to select. And, you are also missing the $ in the rows.

$result =("SELECT something FROM Request WHERE user2='joey' AND  accepted='0'");
$query = mysqli_query($db_conx, $result);
$rows = mysqli_num_rows($query);
if ($rows < 1) {
   echo '<strong style="color:#F00;">No rows</strong>';
   exit();
} else {
echo '<strong style="color:#F00;">Rows exist</strong>';
exit();
}
Sign up to request clarification or add additional context in comments.

8 Comments

so, where's the explanation here?
@Fred-ii- you edited your answer so many times, that when I came up with the $rows you forgot about that part ;)
my original post stackoverflow.com/revisions/33307825/1 contained the fact that the OP's rows was treated as a constant. The other edits were "gravy" ;-)
@Fred-ii- Oh I see, maybe someone just had to copy and paste it to test it, and they might like that. At least your answer was the right one ;). Good luck...
haha maybe ;-) As I mentioned in comments under the OP's question; I was already writing up my answer but didn't press on the "Post Your Answer" button. Your code is correct, it's just that it would be beneficial for all future readers to the question, including the OP when an explanation is given as to why their code failed. It's a "win-win" ;-) Cheers
|
0

You edited your original post https://stackoverflow.com/revisions/33307758/1

being SELECT FROM Request

You have selected "nothing" from your query

SELECT FROM ...

Read the manual on SELECT:

and this if (rows < 1) { - rows is treated as a constant instead of a variable.

Error reporting would have told you that if it was setup to catch and display on your system.

if ($rows < 1) {

and make sure you are successfully connected to your db using the same MySQL API as your query.

Check for errors on your query

Look into using a prepared statement also.


Nota:

You will also need to escape your data should it be coming from user input at one point.

I.e.:

$var = $_POST['var'];

and if $_POST['var'] is equivalent to joey's bistro.

MySQL will see that as WHERE user2='joey's bistro' if it were passed as a variable in the query WHERE user2='$var' resulting in a syntax error.

Escaping it will render it as joey\'s bistro being valid syntax.

$var = mysqli_real_escape_string($db_conx,$_POST['var']);

This would be beneficial for just that as well as helping to protect against an SQL injection.

2 Comments

Thanks Fred. I changed this code so many times today to troubleshoot it I forgot to put $rows. Thanks again
@StrayGraduate You're welcome. If it's solved, great and glad to hear it. Cheers

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.