Portal Home > Knowledgebase > Articles Database > (PHP) Script won't display any data


(PHP) Script won't display any data




Posted by calidude, 08-07-2008, 12:32 AM
I have a script that I coded to search a database for data and display it on a new page. The script refuses to display the data no matter what I do. Here is the code: Any ideas on how I can get it to display the appropriate results? Last edited by calidude; 08-07-2008 at 12:33 AM. Reason: excess page stretching

Posted by qbert220, 08-07-2008, 04:26 AM
Where have you got with debugging this yourself? My initial suggestion would be to echo your mysql_query string to make sure it looks like you expect.

Posted by calidude, 08-07-2008, 04:37 AM
The query itself looks ok, I just tested it. I think the problem is somewhere in the non-query part of the code (eg, the table section). It printed out the following:

Posted by Master Bo, 08-07-2008, 05:44 AM
Suggestions: - make sure that you try to run the SQL statement in MySQL client and it it returns results - redirect all your errors to a log file and study it every time the page loads Also: - use tools like xDebug to make the debugging easy - make sure that the final code is less open to possible SQL injections Does the SQL statement actually return anything?

Posted by calidude, 08-07-2008, 05:58 AM
I found the problem, the AND statements needed to be turned into OR statements. But now I have a new problem. I have two entries with my name in the database, but when I go to search by my name only one entry (the first one) shows up. How can I fix this?

Posted by Master Bo, 08-07-2008, 06:04 AM
Select the entries by author name only and see where they do differ (so that the SQL statement doesn't take all of them).

Posted by calidude, 08-07-2008, 06:22 AM
When I search the table by author name, both entries come up just as expected. Weird. Same applies for the other methods of searching as well. (both manual query and posting into the form)

Posted by Master Bo, 08-07-2008, 06:35 AM
It means the other SQL conditions (in AND cluases) phase out the second entry. Select all the fields that are checked in the original SQL statement, to see what is filtered out.

Posted by calidude, 08-07-2008, 06:45 AM
I switched them to OR statements, but it's still not working. Is there any other operators that I could possibly use?

Posted by Master Bo, 08-07-2008, 09:26 AM
I am out of ideas at this stage - without mentioned data returned from shorter SQL statement, I can't guess what's wrong.

Posted by P-nut, 08-07-2008, 12:30 PM
Not sure if this will help but I noticed you were using a lot of strpos !== FALSE in your script. I've found that I usually have to use if strpos === FALSE with an else statement to get a result. Like this: Alternatively (and I've not yet tested this, so YMMV), you could try using something like this: I guess what I'm trying to say in my roundabout way is that I've personally never been able to get something to return with strpos !== FALSE.

Posted by Adam-AEC, 08-07-2008, 01:07 PM
Are you passing all the search parameters, or are some of them optional? I mean, when you are using the AND operative, it is trying to match EVERY condition you are providing. OR won't work the way you want, since all the conditions should work together. If I am understanding what you are trying to achieve, you should be building your WHERE statement dynamically, depending on what POST variables have data. Untested, but might give you a starting point.

Posted by calidude, 08-07-2008, 08:15 PM
So now the PHP part looks like this: but it's still only printing one result when there should be two. Wtf.

Posted by Adam-AEC, 08-07-2008, 08:17 PM
Output the generated query here and we'll take a look at it.

Posted by calidude, 08-07-2008, 08:43 PM
It comes up with this: SELECT * FROM `owsblogs` WHERE entry_month='8' Which looks right, but the thing is there are two entries with a month of 8; yet only one is showing up on the display page. And that is the first one. So the second one is getting canceled out somehow.

Posted by Adam-AEC, 08-07-2008, 08:46 PM
Strange. You try putting that exact query in phpMyAdmin and see what the results are? What is the column type of entry_month? Your loop looks correct but just for the sake of thoroughness:

Posted by calidude, 08-07-2008, 09:14 PM
It changed to this: SELECT * FROM `owsblogs` WHERE entry_month='8'Thank you for submitting your search query. Please wait a moment while your requested entry is found.2 5 8 2008 Joshua Young Test So it knows that two results are there. Yet only one shows up. How weird is that. edit - All the fields are text, maybe I should change the day and month ones to varchar? *Pause* Nope, changed the date ones to varchar and still no luck.

Posted by foobic, 08-07-2008, 09:43 PM
Are you just viewing the web page or the source too? Any HTML tags in the data? Try using print_r instead of echo to display results.

Posted by calidude, 08-07-2008, 09:53 PM
Do I leave all of the other code alone? And where in the php does that go? I put it in below the actual query area but it didn't work.

Posted by foobic, 08-07-2008, 10:18 PM
It's just another way to display the data, replacing one line in your code (the one shown commented out). But if there's any chance of HTML in the data you'd probably be better with this instead (try either, or both):

Posted by calidude, 08-07-2008, 10:27 PM
I am still getting this: SELECT * FROM `owsblogs` WHERE entry_month='8'Thank you for submitting your search query. Please wait a moment while your requested entry is found. 5 8 2008 Joshua Young Test It should also have an 8/7/2008 entry. What the hell is going on!

Posted by Master Bo, 08-08-2008, 12:50 AM
Could you run the query, with the condition matching two rows, in a mysql client and print the result here?



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
Help with rsync (Views: 566)
Arbor System (Views: 595)