Avatar of TonyReba
TonyReba
Flag for United States of America asked on

reading from a mysql table and use that as a parameter second query

I am trying to pull Descr from a a table called jos_dept_codes , which would look for a result  from a 1st querie  , I made a variable called deptcode for this but is not pulling anything,. no error but just not output.
$database =& JFactory::getDBO(); 

// to do GET QUERY STRING OR POST DATA....... 
$database->setQuery('SELECT department FROM jos_dhruprofile WHERE uid = 325 ') ;
//echo $database->loadResult();
 
if (!$database->query()) { //write data and if error occurs alert
    echo "<script> alert('".$database->getErrorMsg()."'); </script>";
}
//var_dump($database);
//$row = $database->loadRowList();
// Set New Var

$deptcode =  $database->loadResult();

 $db2 =& JFactory::getDBO(); 
 $db2->setQuery('SELECT Descr FROM jos_dept_codes WHERE code = $deptcode  ') ;
 
 
      ?>
      <input class="label" type="text" name="email" id="email" size="40" value="<?php echo $db2->loadResult(); ?>" />
     </td>
    </tr>

Open in new window

PHPJoomlaSQL

Avatar of undefined
Last Comment
TonyReba

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Marco Gasi

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
TonyReba

ASKER


thanks that worked, I am still kind of getting familiar with php syntax,
Marco Gasi

Hi, TonyReba. Thanks for points. Keep in mind that if you use in your WHERE clause a php variable which holds a numeric value you have to extract it from quotes, otherwise it will be treated as a string. So you can write

$query = "SELECT field  FROM table WHERE username = $username";

but if you use something as a code or a numeric id then you must write

$query = "SELECT field  FROM table WHERE user_id = " . $user_id;

Cheers
TonyReba

ASKER
thanks for the info
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy