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

TonyReba
TonyReba used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Freelancer
Top Expert 2010
Commented:
Try this:

$db2->setQuery('SELECT Descr FROM jos_dept_codes WHERE code =' . $deptcode ) ;

Author

Commented:


thanks that worked, I am still kind of getting familiar with php syntax,
Marco GasiFreelancer
Top Expert 2010

Commented:
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

Author

Commented:
thanks for the info

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial