problem returning required output from sql statement
Posted: Mon Feb 26, 2007 8:40 pm
i have the following php code
firstly the code gets the value in the ingredients_added text field which will be in the form of:
value1 \n
value2 \n
\n
then it adds each line to an array element, strips off the \n and deletes the last element in the array which is empty. this works fine but the next bit is the trouble.
basically i now have an array of strings and i want to get the ids that correspond to these strings from the database. i have created an idarray which is an array that will hold the ids for the values.
the condition while($iterationno <= ($elemsinarray - 1)) means get the ids for the number of elements in the array
i think the problem i m having is here some where i get the error if i change the code to
the array contains the corrrect number of elements but they are all null. The SQL statement doesnt fail so that is not the problem can anyone help??
Code: Select all
if(isset($_POST['ingredients_added']))
{
//adds list of ingredients to a variable
$allingredients = $_POST['ingredients_added'];
//explode adds a new element to the array every time \n is found
$ingredientsarray = explode("\n", $allingredients );
//funtion to strip \n off the end of each line
function trim_value(&$value)
{
$value = trim($value);
}
//apply trim_value function to every element in array
array_walk($ingredientsarray, 'trim_value');
//by this point the array has each line in an element in the correct format but has an unwanted element at the end
//gets rid of the unwanted empty element
$foo = array_pop($ingredientsarray);
//need to get ids of names of recipes in $ingredients array
//add numbers to id table
//add id of recipe to id table
$elemsinarray = count($ingredientsarray);
echo "no of elements in array : $elemsinarray";
$idarray = array();
$iterationno = 0;
while($iterationno <= ($elemsinarray - 1))
{
$getids = @mysql_query("SELECT id FROM ingredients WHERE name = '$ingredsinarray[$iterationno]'");
if (!getids)
{
echo "fail ' . mysql_error() . '" ;
}
while ($row = mysql_fetch_array($getids,MYSQL_ASSOC))
{
$value = $row['id'];
echo "$value";
$idarray[$iterationno] = $value;
}
$iterationno++;
}
var_dump($idarray);
}value1 \n
value2 \n
\n
then it adds each line to an array element, strips off the \n and deletes the last element in the array which is empty. this works fine but the next bit is the trouble.
basically i now have an array of strings and i want to get the ids that correspond to these strings from the database. i have created an idarray which is an array that will hold the ids for the values.
the condition while($iterationno <= ($elemsinarray - 1)) means get the ids for the number of elements in the array
i think the problem i m having is here some where
Code: Select all
while ($row = mysql_fetch_array($getids,MYSQL_ASSOC))
{
$value = $row['id'];
echo "$value";
$idarray[$iterationno] = $value;
}
Code: Select all
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /customersites/9/home/httpd/vhosts/stear-uk.co.uk/httpdocs/menu_manager/add_pages/addrecipe.php on line 133Code: Select all
$value = $row['id'];
echo "$value";
$idarray[$iterationno] = $value;