Jump to content

Does this number exist..


cdoggg94

Recommended Posts

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)){

}

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.