Rohlan Posted December 22, 2008 Share Posted December 22, 2008 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? Quote Link to comment Share on other sites More sharing options...
Mikedean Posted December 24, 2008 Share Posted December 24, 2008 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. Quote Link to comment Share on other sites More sharing options...
Rohlan Posted December 30, 2008 Author Share Posted December 30, 2008 Thanks for the help I'm no longer having any errors, I got it fixed before I saw your message. Thanks a lot though, I appreciate it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.