Page 1 of 1

problem returning required output from sql statement

Posted: Mon Feb 26, 2007 8:40 pm
by slash_gnr3k
i have the following php code

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);
				}
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

Code: Select all

                                                        while ($row = mysql_fetch_array($getids,MYSQL_ASSOC))
                                                        {
                                                                $value = $row['id'];
                                                                echo "$value";
                                                                $idarray[$iterationno] = $value;

                                                        }
i get the error

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 133
if i change the code to

Code: Select all

                                                       
                                                                $value = $row['id'];
                                                                echo "$value";
                                                                $idarray[$iterationno] = $value;

                                                        
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??

Posted: Mon Feb 26, 2007 8:54 pm
by volka
slash_gnr3k wrote:if (!getids)
{
echo "fail ' . mysql_error() . '" ;

}
missing $ between ! and getids. mysql_error() is never called because it's inside a double-quoted string.

try

Code: Select all

while($iterationno <= ($elemsinarray - 1)) {
	$query = "SELECT id FROM ingredients WHERE name = '$ingredsinarray[$iterationno]'";
	$getids = @mysql_query($query);

	if (false===$getids) {
		die(mysql_error() . ': ' . $query);
	}

Posted: Mon Feb 26, 2007 9:04 pm
by slash_gnr3k
aha thats sorted the error. my field was inig not id. now though. theres nothing in the array $idarray and i cant tell why. can anyone see?