cdoggg94 Posted December 6, 2011 Share Posted December 6, 2011 Basically this gets a number from a table on my DB, and displays the content relevent to that number...the second part checks for that number and gets other information according to that number... what i want to do is make sure that the number from the first query exists in the table from the query, and if it doesn't print out and error saying "there is no information" or whatever.. anyone have an idea? hopefully this makes sense <?php require_once('Connections/myConnect.php'); $detnum = $_GET['Details']; $detinfo = mysql_query("SELECT* FROM `Agency Stores - Table 1` WHERE F11=".$detnum); $det = mysql_fetch_array($detinfo); $salesnum = $det['F3']; $salesinfo = mysql_query("SELECT* FROM `By Agency Store _` WHERE store_store=".$salesnum); $sales = mysql_fetch_array($salesinfo); if(mysql_query("SELECT* FROM `By Agency Store _` WHERE store_store=".$salesnum)){ } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252591-does-this-number-exist/ Share on other sites More sharing options...
joe92 Posted December 6, 2011 Share Posted December 6, 2011 Add between the two queries: <?php require_once('Connections/myConnect.php'); $detnum = $_GET['Details']; $detinfo = mysql_query("SELECT* FROM `Agency Stores - Table 1` WHERE F11=".$detnum); $det = mysql_fetch_array($detinfo); if(mysql_num_rows($detinfo)) { $salesnum = $det['F3']; $salesinfo = mysql_query("SELECT* FROM `By Agency Store _` WHERE store_store=".$salesnum); $sales = mysql_fetch_array($salesinfo); if(mysql_query("SELECT* FROM `By Agency Store _` WHERE store_store=".$salesnum)) { //do whatevers going in here } } else{ echo 'Row does not exist'; } ?> That is saying, if rows are returned, carry on with the script. Else echo 'Row does not exist'. Info on mysql_num_rows Quote Link to comment https://forums.phpfreaks.com/topic/252591-does-this-number-exist/#findComment-1294973 Share on other sites More sharing options...
cdoggg94 Posted December 6, 2011 Author Share Posted December 6, 2011 would it be the same if in this script: <?php require_once('Connections/myConnect.php'); $detnum = $_GET['Details']; $detinfo = mysql_query("SELECT* FROM `Agency Stores - Table 1` WHERE F11=".$detnum); $det = mysql_fetch_array($detinfo); the number always exists and i want to check if it also exists in this script: $salesnum = $det['F3']; $salesinfo = mysql_query("SELECT* FROM `By Agency Store _` WHERE store_store=".$salesnum); $sales = mysql_fetch_array($salesinfo); im just a little confused and i think i made it worse but adding the IF statement in it at the end. Quote Link to comment https://forums.phpfreaks.com/topic/252591-does-this-number-exist/#findComment-1294980 Share on other sites More sharing options...
Psycho Posted December 6, 2011 Share Posted December 6, 2011 1. Do sanitize user input as appropriate before using in a Query 2. Do NOT use '*' in your SELECT queries unless you really need all the fields. 3. Do NOT use multiple queries when you can achieve the same thing with one. I.e. learn to do JOINs 4. DO create your queries as string variables so you can echo the query to the page for debugging purposes I would also advise not using spaces in table or field names. require_once('Connections/myConnect.php'); $detnum = mysql_real_escape_string(trim($_GET['Details'])); $query = "SELECT s1.* FROM `By Agency Store _` as s1 LEFT JOIN `Agency Stores - Table 1` as s2 ON s1.store_store = s2.F3 WHERE s2.F11 = {$detnum}"; $salesinfo = mysql_query($query); if(mysql_num_rows($salesinfo)) { //No matching records } else { //Do somethign with the results } Quote Link to comment https://forums.phpfreaks.com/topic/252591-does-this-number-exist/#findComment-1294991 Share on other sites More sharing options...
cdoggg94 Posted December 6, 2011 Author Share Posted December 6, 2011 What a difference...thanks a ton! i appreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/252591-does-this-number-exist/#findComment-1294995 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.