Mysql select to an array

Mitch Anderson mitch at metauser.net
Wed Aug 27 09:13:36 MDT 2003


Alright... I was banging my head on my desk all day yesterday because I
couldn't figure this out...

I'm having a problem saving a select from a mysql database to an array
in PHP.  

My code is as fallows:

function get_scheduleid($class_id){
	$sched_sql = "SELECT scheduleid FROM nbschedules WHERE classid =
'$class_id'";
	
	$myconn = db_connect(); // basic function to connect to DB.
	
	$sched_result = mysql_query($sched_sql, $myconn);
	
	$sched_num = mysql_num_rows($sched_result);
	
	
	// This is where the problem lies... Not sure if I need the for 	//
loop or not...

	for($i=0; $i<$sched_num; $i++){
		$sched_array[$i] = mysql_fetch_row($sched_result);
	}
	
	mysql_close($myconn);
	
	//print_r($sched_array);
	
	return $sched_array;
}

The Results of the select will contain atleast 2 values... and sometimes
as many as 8... so it needs to be dynamic..  but they will look
something like this:

mysql> SELECT scheduleid FROM nbschedules WHERE classid = 1385;
+------------+
| scheduleid |
+------------+
|          1 |
|          2 |
+------------+
2 rows in set (0.00 sec)

I want the function to return an array of the values (1,2,etc...) but
with that for loop... it creates an array that contains two arrays, with
those two values.  If that makes sense... If I drop the for loop... it
replaces the value of the array with the new value (ie: $sched_array[0]
= 2 at the end of the loop, but after the first time through
$sched_array[0] = 1)

Any Thoughts on what I'm doing wrong?

Mitch





More information about the UPHPU mailing list