Jump to content

Problems with "Variable passed to each() is not an array or object"


Rohlan

Recommended Posts

Hello everyone  :)

 

I found this very useful set of functions at http://www.spaceblue.com/codedetail.php?CodeID=1 and have been using them for interacting with MySQL.

 

I ran into a problem when trying to use the MySQLSelect() function however.

 

The function code is:

 

	// Build Select query (EXPERIMENTAL)
// $table - Is name of table to select from
// $columnsArray - Is an indexed array of column names to select.  May also be a string if special select from clause or false for all columns
// $whereArray - Is an associative array of names => values, may be false for no where clause, or a string if special where clause
// $link - If false, will return the built query as a string
function MySQLSelect( $table, $columnsArray = false, $whereArray = false, $compareArray = false, $orderByArray = false, $orderAscending = false, $link = false )
{
	// Check that if $CompareArray is an array, it is the same size as $whereArray
	if( is_array($compareArray) )
	{
		if( !is_array($whereArray) )
			return MySQLDie("SELECT ERROR: whereArray must be an array since compareArray is an array.");
		if( count($compareArray) != count($whereArray) )
			return MySQLDie("SELECT ERROR: compareArray and whereArray must have same mumber of items.");
	}

	MySQLAddSlashes( $whereArray ); // Add slashes to array values
	$query = "SELECT ";

	// Specify columns to select
	if( is_array($columnsArray) )
	{
		$bIncludeComma = false;
		while(list(,$column) = each($ColumnsArray) )
		{
			if( $bIncludeComma )
				$query .= ", ";
			$query .= "`$column`";
			$bIncludeComma = true;
		}
		$query .= " ";
	}
	else if( $columnsArray )
	{
		$query .= "$columnsArray ";
	}
	else
	{
		$query .= "* ";
	}

	$query .= "FROM `$table`";

	// specify where clause
	if( is_array($whereArray) )
	{
		$count = 0;
		$query .= " WHERE";
		while( list($column, $value) = each($whereArray) )
		{
			if( $count > 0 )
				$query .= " AND";  // If you want more than this, you really should make the query yourself

			$query .= "`$column` ";
			if( is_array($compareArray) )
				$query .= $compareArray[$count];
			else if( $compareArray )
				$query .= "$compareArray";
			else
				$query .= "LIKE";

			if( $value === null )
				$query .= " NULL";
			else
				$query .= " '$value'";

			$count++;
		}
	}
	else if( $whereArray )
	{
		$query .= " WHERE $whereArray";
	}

	// specify order by clause
	if( is_array($orderByArray) )
	{
		$bIncludeComma = false;
		while(list(,$column) = each($orderByArray) )
		{
			if( $bIncludeComma )
				$query .= ", ";
			$query .= "`$column`";
			$bIncludeComma = true;
		}
		if( $orderAscending )
			$query .= " ASC";
		else
			$query .= " DESC";
	}
	else if( $orderByArray )
	{
		$query .= " ORDER BY $orderByArray";
	}

	if( $link )
		return mysql_query($query, $link ) or die("SELECT ERROR ($query): " . mysql_error() );

	return $query;
}

 

I am calling the function using:

(right now just trying to echo the query)

		
$table = "users";
$fields = array("user_ID", "username");
		$query = MySQLSelect($table, $fields);
		echo $query;

 

An error saying "Variable passed to each() is not an array or object" pops up when I run my code.

 

I am fairly certain I am using an array to indicate the fields, but I don't quite understand what I'm doing wrong.

 

The function allows $fields to be a string, so something like $fields = "user_ID, username" will work just fine, but I wanted it to work as an array as the code is much more readable to me atleast.

 

The comments on the function suggest using an array too... I'm just not seeing where I went wrong, I must confess I don't quite grasp why they use the each() in there either, but that's beside the point.

 

Can anyone help?

Link to comment
Share on other sites

You haven't specified what the values for the columns need to be, so you need to create another array with the WHERE values.

$table = "users";
$fields = array("user_ID", "username");
$where = array("1", "Rohlan");
$query = MySQLSelect($table, $fields,$where);
echo $query;

 

This should then assign the fields with their value.

 

If you don't want to specify a WHERE clause, try the following.

 

Replace:

MySQLAddSlashes( $whereArray ); // Add slashes to array values

 

With:

if ($whereArray)
     MySQLAddSlashes( $whereArray ); // Add slashes to array values

 

So if it doesn't equal false, it will then add slashes.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.