can anyone offer some help here
i have a string that contains course codes. i separate the string into the individual course codes using the explode function. however i want to run a query on each of the course codes that was present in the string and store the result in an array that i have created this is the code that i have thus far

<?php


$hostname = "localhost";
$username = "root";
$password = "hayden";
$database = "ecng3020";



$con = mysql_connect("$hostname","$username","$password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("$database", $con);
  
  $q="SELECT * FROM rules Where type ='course_option'  ";
  if ($result_2=mysql_query($q))
  {
  	echo"nice";
  }
  else
  {
  	echo"shit";
  }
											
	while($row = mysql_fetch_assoc($result_2))
	{
			extract($row);
			echo "<p>$parameters</p>";
			$e=explode(",",$parameters);
			print_r($e);
			
			$w="SELECT * FROM student_courses WHERE course_status='pass' AND course_code='$e' AND id_no=806000169";
			
			if ($result1=mysql_query($w))
			  {
				echo"nice";
			  }
			  else
			  {
				echo"shit";
			  }
			  while($row1 = mysql_fetch_assoc($result1))
			  {
			  	extract($row);
				echo "<p>$course_status</p>";
			  }
		
	}
	
?>

as you can see i have not created the array to store the results of the query cause i am experiencing some problems with the final query for some reason it is not working
can some one offer some assistance please
thanks

Recommended Answers

All 10 Replies

on line 47 change to

extract($row1);

That will solve your first problem and could you mention the value you would like to store in the array (eg. $course_status).

i have picked up that error and made the change and the values that i would like to store in the array will be the course codes of the courses that have their course-status as pass. i have been working on it and i have got the second query to work but now for some reason i am not getting to query to loop through all the course codes that were stored in the $e array

Perhaps the following?

<?php


$hostname = "localhost";
$username = "root";
$password = "hayden";
$database = "ecng3020";



$con = mysql_connect("$hostname","$username","$password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("$database", $con);
  
  $q="SELECT * FROM rules Where type ='course_option'  ";
  if ($result_2=mysql_query($q))
  {
  	echo"nice";
  }
  else
  {
  	echo"shit";
  }
											
	while($row = mysql_fetch_assoc($result_2))
	{
			extract($row);
			echo "<p>$parameters</p>";
			$e=explode(",",$parameters);
			print_r($e);
			foreach ($e AS $val) {
			$w="SELECT * FROM student_courses WHERE course_status='pass' AND course_code='".mysql_real_escape_string($val)."' AND id_no=806000169";
			
			if ($result1=mysql_query($w))
			  {
				echo "nice";
			  }
			  else
			  {
				echo "shit";
			  }
			  while($row1 = mysql_fetch_assoc($result1))
			  {
			  	extract($row1);
				echo "<p>$course_status</p>";
			  }
            }
		
	}
	
?>

thanks alot for the code. i have tried it and it works. i have moved ahead to storing the values in an array however for some reason it appearing empty
this is what i have done for the array

while($row1 = mysql_fetch_assoc($result1))
								{
									  extract($row1);
										echo "<p>$course_status</p>";
										echo "<p>$course_code</p>";
										$first=array();
										$first=$course_code;
											echo "<p>hi</p>";
											print_r($first);
}
<?php


$hostname = "localhost";
$username = "root";
$password = "hayden";
$database = "ecng3020";



$con = mysql_connect("$hostname","$username","$password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("$database", $con);
  
  $q="SELECT * FROM rules Where type ='course_option'  ";
  if ($result_2=mysql_query($q))
  {
  	echo"nice";
  }
  else
  {
  	echo"shit";
  }
  $first=array();
	while($row = mysql_fetch_assoc($result_2))
	{
			extract($row);
			echo "<p>$parameters</p>";
			$e=explode(",",$parameters);
			print_r($e);
			foreach ($e AS $val) {
			$w="SELECT * FROM student_courses WHERE course_status='pass' AND course_code='".mysql_real_escape_string($val)."' AND id_no=806000169";
			
			if ($result1=mysql_query($w))
			  {
				echo "nice";
			  }
			  else
			  {
				echo "shit";
			  }
			  while($row1 = mysql_fetch_assoc($result1))
			  {
			  	extract($row1);
                $first[]=$course_code;
				echo "<p>$course_status</p>";
			  }
            }
		
	}
    print_r($first);
	
?>

I find this a strange construction:

if ($result1=mysql_query($w))
{
  echo "nice";
}
else
{
  echo "shit";
}
while($row1 = mysql_fetch_assoc($result1))
{
  extract($row1);
  $first[]=$course_code;
  echo "<p>$course_status</p>";
}

First you determine if the query has failed or not. Then you execute the while, not taking into account the return value. If the query has failed, the mysql_fetch_assoc will throw a warning. I'd recommend this:

if ($result1=mysql_query($w))
{
  echo "nice";
  while($row1 = mysql_fetch_assoc($result1))
  {
    extract($row1);
    $first[]=$course_code;
    echo "<p>$course_status</p>";
  }
}
else
{
  echo "shit";
}

My $0.02

i have initially tried that but the problem that i am having with that is that is is only storing the current value. it is not storing the other values when the loop runs another time. ie. when i get a result in $row1 in the first loop it is stored in the array however in the next time it loops the array only contain one value and that value will now be the second course

i have initially tried that but the problem that i am having with that is that is is only storing the current value. it is not storing the other values when the loop runs another time. ie. when i get a result in $row1 in the first loop it is stored in the array however in the next time it loops the array only contain one value and that value will now be the second course

Try to copy and past my code because I used the [] operator which makes a big difference. That is where your problem would have been.

thanks alot that worked the problem was that i had this line just above
$first=array();
when i removed it and put in the [] operator it worked
just for future reference what is the function of the [] operator??

I want to point out the mysql find_in_set command, where you can do something like this:

select * from student_courses 
where course_status='pass' 
and id_no=806000169
and  find_in_set(course_code, $parameters)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.