• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

php/mysql problem...

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

InThrees

Member
Joined
Feb 14, 2003
Location
Southeast US
tearing my hair out over this and is should be SO SIMPLE.

scenario: you have a table with columns that hold strings.

how do you search the table for values (exact entire values, not partial strings) and process a null result? This has to be possible, this forum and every forum uses something like it. If i search the forum subjects for some random hash I won't find it, and the ocforums scripts will tell me it wasn't found.

But i cannot for the life of me replicate it.

- i've tried num_rows = mysql_num_rows($result) figuring that i could then test $num_rows with if. if it's >0, there was a result. If it's <1 or not set, then there was no result. This didn't work. "invalid resource" messages.

- i've tried using a select count statement. This doesn't work either. "invalid resource messages."

- i've tried processing result with many different variations of possible boolean outcomes with many different variations of syntax (TRUE, FALSE, -1, 0, etc) to try to get the script to work, and nothing.

I spent all day yesterday fighting this and a large part of this morning. I joined forums and asked and haven't gotten good responses yet. I've done a ton of googling and can't find it.

mysql 5 and php5 with apache 2.22 is what i'm running, on freebsd.

So please, someone.

Write me some simple code based on the following:

assume schema appleschema and table appleowners

table appleowners -
ownerid int not null auto_increment primary key
ownername varchar(30)

please explain how to search the appleowners table for an already existing owner name, and how to process $result if it's not found.
 
PHP:
<?php

$hostname="localhost";
$username="User";
$password="Pass";
$database="MyDB";
$lookfor="Something that is not there";

mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");


$query = "SELECT client_id FROM `fah_records` WHERE client_id='$lookfor'";
$result = mysql_query($query);

$num=mysql_numrows($result);

echo $num;


mysql_close();


?>

When I run this I get 0 as a result. I had problems with the single quotes around the $lookfor in the query string but once I got those straight it works fine for me. Maybe adapt this for you and try it.
 
and the output of that after i port it to my schema naming is

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in script.php on line 20

[co

GRRRRR

you know what it was? the case of the reserved words in the select statement.

I troubleshoot via commandline and lower case select statements work fine there, and they also work fine for insert statements in php code... and other select statements... wierd.

Regardless, I changed the case of the select statement from

select * from USR_Users where bla = blabla

to

SELECT * FROM and so on... and it works now.

regardless, thanks seadave =D
 
instead of doing mysql_query($query); do mysql_query($query) OR trigger_error('Error in Query: '. $query.' - '.mysql_error,E_USER_ERROR);

This will let you know why your query is failing.

the OR command says "IF this function fails, I need to do X!" where X is what follows the or statement. This way, it will output to the screen the exact mysql error.
 
That is a good idea for troubleshooting Elif, but wouldn't you want to take that out on a production site? At least replace it with a die statement instead of echoing the internals of your site to the world.
 
Well, in those instances, set it to E_NOTICE or something instead of E_USER_ERROR.

I have a custom error handler that I've written that allows for greater flexibility than the regular php error handler (I.E. when an E_USER_ERROR comes up, I give a nice little "Sorry, ineternal script error" and email myself when it's in production, and in testing it merely echoes it to the screen.

I also have other steps in place that I can echo whatever I want, and it will never show up in a production site.
 
Back