MySQL & PHP error for multiple tables

software development

#1

Hi all,
I am a newbie creating a mysql database and using php scripts to recall user choice of drugs and extract data out from multiple tables from the database. Would appreciate some help on debugging a script which gives me the following error:

"Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [URL where script is embedded] on line 165

Warning: Invalid argument supplied for foreach() in [URL where script is embedded] on line 165"

My php & MySQL scripts for creating variables depends on the user choice of a pull-down menu with name=“psy_drug[]” and id=“psy_drug”:

//create variables for psy_drugs
foreach ($_GET[‘psy_drug’] as $psy_drug)
{$PSY = $psy_drug;
//echo $PSY;
}

//Query for drug name
$psyname = ‘SELECT psyd FROM psy_pk WHERE acronym = "’.$PSY.’"’;
$psyname_result = mysql_query($psyname);
foreach (mysql_fetch_row($psyname_result) as $psyname_output)
{ //echo $psyname_output;
}

//The following lines are where the errors occur:

// Create query for selecting $psyname_output from multiple tables where the “acd_name = user choice of $acdname_output”

$drugint = ‘SELECT ‘.$psyname_output.’ FROM ddi1, ddi2, ddi3, ddi4, ddi5, ddi6, ddi7, ddi8 WHERE acd_name = "’.$acdname_output.’"’;

$drugint_result = mysql_query($drugint);
foreach (mysql_fetch_array($drugint_result) as $drugint_output)
{ //echo $drugint_output;
}


#2

[quote]foreach (mysql_fetch_row($psyname_result) as $psyname_output)
{ //echo $psyname_output;
}[/quote]
The variable $psyname_output will be destroyed after the for loop. You will need to create another variable outside the for loop and store the value.

[quote]foreach (mysql_fetch_array($drugint_result) as $drugint_output)
{ //echo $drugint_output;
}
[/quote]
Since $psyname_output is not valid, it returns empty value. You can try to echo $drugint to double check the sql query.

While for the error message, it is because empty value is passed to mysql_fetch_array and foreach. To have a good habit in programming, you should always check whether the value of variable exists or not. You can use function empty($yourvairable) to check.

$50 off and 3 free domains with code: [color=#CC0000]DH3[/color] Sign Up NOW or More Codes Here