/    Sign up×
Community /Pin to ProfileBookmark

Query not returning true…but why?

Hello,

I have the following:

[code=php]

$q=”SELECT * FROM `bigTable`”; //get loads of rows
echo $q.”<br>”; //display that $q is as expected
$r=mysql_query($q); //run the query
if ($r){echo “yay!”;} //result found. display yay!

[/code]

This works on its own in a test page. But if I copy and paste it into a larger file, the $q is echoed but “yay!” is not.

For troubleshooting, if I copy the echoed $q value into phpmyadmin and run it there, I get the rows as expected.

Any ideas why this is happening? (or not happening?)

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 13.2010 — Try asking PHP what is wrong:
[code=php]
if ($r){
echo "yay!";
} else {
echo "Boo! " . mysql_error();
}
[/code]
Copy linkTweet thisAlerts:
@speghettiCodeauthorOct 13.2010 — Hello NogDog,

Thanks for the suggestion. The output was just "Boo!"...no error (all of my code is flawless ? ).

I figured out the issue, though. I had locked a different table above on the same long page but had never unlocked it. That's why the query above executed and this one failed. It also explains why copying and pasting the query string into phpmyadmin worked and why the script works on the test page.

Thank you for your help.
Copy linkTweet thisAlerts:
@zakkwOct 14.2010 — if i remember correctly you can't just display the results of the query directly, try this:


[code=php]

$q="SELECT * FROM bigTable"; //get loads of rows

echo $q."<br>"; //display that $q is as expected

$r=mysql_query($q); //run the query

if ($rowsFound = mysql_count_rows($r) == 0) {

echo "oh noes, my SQL query didn't work! <br />" . mysql_error();
} else {echo $rowsFound;}

while ($results = mysql_fetch_array($r)) { //result found. display yay!

$i = 1;
echo "row " . $i . " found, yay!";

echo $results['TABLEFIELDNAME'];

}
[/code]


What it does:

the mysql_num_rows() function is assigned to the $rowsFound variable, this returns how many numbers of rows there are as a result of the query, if there are 0 rows, there's no data in your db, or your SQL is incorrect.

The query doesn't return true or false dierectly, however if data is found with the query, the mysql_fetch_array() function looks at the first row and if there is data present returns true, when ran again in the loop, it tests the second row, and so on, so forth. The mysql_fetch_array() function returns false when no data is found.

You can then go further and assign the function to a variable so you can access the fields with $results['fieldname']. Hope this helps.

Let me know if im on the right track, or off some random tangent ?
Copy linkTweet thisAlerts:
@speghettiCodeauthorOct 14.2010 — hello zakkw,

Thank you for your feedback.

The issue I was having was definitely due to the query being run before the table used in a previous query was "unlocked". But your suggestions are definitely a good contribution to the discussion here:

http://www.webdeveloper.com/forum/showthread.php?t=236912
×

Success!

Help @speghettiCode spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...