Jump to content

[SOLVED] mysql_fetch_assoc not working as desired is this a bug i dont know about?


designknights

Recommended Posts

$contents =  '<h1>DATABASE ERROR!</h1>';
if ($dbConnect = mysql_connect("my_db","my_user","my_pass")){
	mysql_select_db("my_db");
	// setting the vars for the function 
	$numovies=0;
	$i = 0 ;
	//building an array of movies to be checked out by Movie ID 
	foreach ($post as $key => $value){
		if (strlen($key) > {
			$key = str_split($key,;
			if ($key[0] == "checkout"){
				$themids[$numovies] = $value;
				//getting the number of movies selected
				$numovies++;
			}
		}
	}
	// building the query 
	if ($numovies > 1){
		for ($i=0;$i<$numovies;$i++){
			if ($i==0){
				$query = 'SELECT * FROM `movies` WHERE `MID` = \''.$themids[$i].'\' ';
			}
			else if ($i==($numovies-1)){
				$query .= 'AND `MID` = \''.$themids[$i].'\' LIMIT 0,'.$i.';';
			}
			else {
				$query .= 'AND `MID` = \''.$themids[$i].'\' ';
			}
		}
	}
	elseif ($numovies == 1){
		$query = 'SELECT * FROM `movies` WHERE `MID` = \''.$themids[$i].'\' LIMIT 1;';
	}
	else {
		$query = false;
	}
	// query db 
                if ($query != false){ 
		$result = mysql_query($query,$dbConnect) or die ('<br>'.$query.'<br>'.mysql_error());
	}
	else {
		$contents= "DATABASE ERROR!";
	}
	//loop to output results
	while ($row = mysql_fetch_assoc($result)) {
		echo $row["MID"];
		echo $row["title"];
		echo $row["status"];
	}
	mysql_free_result($result);

This function should work. The php manual says so, the mysql manual says so and I have used that while loop many many many times.  Is there a reason it has decided not to work here(IE a bug in php that i cant find)? The problem i am having is that if there is more than one row of results from the SQL query the loop dosent actually put anyhing into $row. However if there is only one row returned from the DB then it works fine. I am stumped, any help would be much appreciated.

Its more likely a problem with you sql statement rather than mysql_fetch_assoc().

Echo the sql statement and check its valid

 

<?php
// query db
if ($query != false){
        echo $query;
$result = mysql_query($query,$dbConnect) or die ('<br>'.$query.'<br>'.mysql_error());
}
?>

 

this is the query being sent to the MySql server. I have tested it in PhpMyAdmin and it pulls the results fine.

$query = SELECT * FROM `movies` WHERE `MID` = '9' AND `MID` = '14' AND `MID` = '18' ;

 

 

will return Zero...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.