Notices

Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

php/mysql problem...

Post Reply New Thread Subscribe Search this Thread
 
 
Thread Tools
Old 03-24-07, 03:13 PM Thread Starter   #1
InThrees
Member

 
InThrees's Avatar 

Join Date: Feb 2003
Location: Southeast US

 
php/mysql problem...


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.
InThrees is offline   QUOTE Thanks
Old 03-24-07, 05:07 PM   #2
seadave77

 
seadave77's Avatar 

Join Date: Jul 2002
Location: Newnan, GA

 
PHP Code:
<?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.
seadave77 is offline   QUOTE Thanks
Old 03-24-07, 06:21 PM Thread Starter   #3
InThrees
Member

 
InThrees's Avatar 

Join Date: Feb 2003
Location: Southeast US

 
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
InThrees is offline   QUOTE Thanks
Old 03-26-07, 10:41 AM   #4
Elif Tymes
Member

 
Elif Tymes's Avatar 

Join Date: Dec 2004

 
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.

__________________
Heatware:
Trolls: You mess with one of us, you mess with SSS
Gamertag: Elif Tymes
Elif Tymes is offline   QUOTE Thanks
Old 03-26-07, 10:59 AM   #5
seadave77

 
seadave77's Avatar 

Join Date: Jul 2002
Location: Newnan, GA

 
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.
seadave77 is offline   QUOTE Thanks
Old 03-26-07, 11:13 AM   #6
Elif Tymes
Member

 
Elif Tymes's Avatar 

Join Date: Dec 2004

 
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.

__________________
Heatware:
Trolls: You mess with one of us, you mess with SSS
Gamertag: Elif Tymes
Elif Tymes is offline   QUOTE Thanks

Post Reply New Thread Subscribe


Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Mobile Skin
All times are GMT -5. The time now is 05:26 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
You can add these icons by updating your profile information to include your Heatware ID, Benching Profile ID or your Folding/SETI profile ID. Edit your profile!
X

Welcome to Overclockers.com

Create your username to jump into the discussion!

New members like you have made this the best community on the Internet since 1998!


(4 digit year)

Why Join Us?

  • Share experience
  • Max out your hardware
  • Best forum members anywhere
  • Customized forum experience

Already a member?