Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check that an existing value = $input (mysql)

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Ok i have 3 tables that i'm querying from.

All three tables have a common column, RTA. Initially i was going to use this as referential integrity, but since mysql doesn't handle referential integrity (least I can't figure out how to do it if it does) I'm attempting to use php to help me out. This is a small application and more for test purposes than anything else. Ok enough background, here is the question.

User enters information from a page into a text field named "rga".

I want to check that this number ($rga) = an RTA number in the main table BEFORE inserting the rest of the information from the page. If there is not an RTA number that equals $rga then I want it to die.


I have this in my page...

$res = mysql_query("SELECT RTA FROM maindcp WHERE RTA = '$rga'")
or die("RTA doesn't exist");

It continues the process regardless if the RMA does or doesn't exist. It either returns a value or returns an empty set, and if i'm correct, an empty set is still "true" so it continues on with the rest of the script.

I then put this after the query but it still continues on-- i don't know if this is even remotely close to what it should be:

$res = mysql_query("SELECT RTA FROM maindcp WHERE RTA = '$rga'")
or die("RTA doesn't exist");
if ($res = $rga) {
} else {print "rta doesn't exist";}

and again it just continues on thru the script and doesn't even blink.

What I want is for it to stop if the number in $rga is not in the maindcp table's RTA column. How could I accomplish this? thanx!

Nico
 
You're not far enough into PHP's database functions.

The mysql_query() command returns a resource identifer or FALSE when you attempt to execute a SELECT statement ( It doesn't care whether the query returned any rows or not -- just that the query executed successfully. And a return of no rows is a successful execution of the query. SQL syntax errors, for example, will cause mysql_query() to return false.

To get a count of the number of rows returned by your query, check out mysql_num_rows() ( ______________________________________________________________________
TANSTAAFL!
 
OK i cruised and read that link you put up there. I tried a few different this and either i'm not doing it correctly or i'm putting in the wrong things.

How would I write a check for if a row was returned based on that query, then continue else if a row was NOT returned die?

Thanx again

Nico
 
Ok I got it if anyone else is looking for the answer

$res = mysql_query("SELECT RTA FROM maindcp WHERE RTA = '$rga'");
$rep = mysql_num_rows($res);
if ($rep > 0) {
} else {die("There is no such RMA number");}

thanx for steering me in the right direction sleipnir214

Nico
 
Or you could just do this:
Code:
$res = mysql_query("SELECT RTA FROM maindcp WHERE RTA = '$rga'"); 
$rep = mysql_num_rows($res) || die("There is no such RMA number");
//Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top