Re: Array driving me nuts

From: Date: Wed, 29 Nov 2000 13:38:11 +0000
Subject: Re: Array driving me nuts
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-27810@lists.php.net to get a copy of this message
Why not just use the array returned from the result set? Mysql_fetch_array returns an associative array with key, value pairs, so you can fetch elements using the name of your key, in this case the field name. // all your preliminary code // get the number of rows here for display if you wish // then use the search_result handle. while ($results_returned = mysql_fetch_array( $search_result )) { //table formating stuff as appropriate echo "$results_returned['_fname'] : $results_returned['_lname'] ... <br>"; //increment your row counter if you want to use it for display //table formatting stuff } You may want to shorten some variable names to help readability, but that's another discussion. Also keep in mind, while creating your "WHERE" clause, is that if the variable is empty it doesn't exist. Regards - Miles Thompson Jon Rosenberg wrote: > I'm using the code below to search for results in a mysql db, I'm trying to > loop throught the returned array(multi-dimensional), but I'm not getting > anything I can use. I can' t figure this one out. > > to see the results, go to > http://www.politics2004.com/personals/search.html > ...don't fill in anything, so that it returns all the results in the db (3 > right now) > Thanks for any help. btw, There are other problems in the code...for > example, the search method is still buggy, but works for where I am in the > project right now. > > Jon > > here is the code that does the work: > > function search_db ( $search_sex, $search_city, $search_state, > $search_orientation, $search_status, $search_race, $search_option ) > { > print "Choices are: $search_sex, $search_city, $search_state, > $search_orientation, $search_status, $search_race, $search_option"; > $user = ""; > $pass = ""; > $db = "personals"; > $db_connect = mysql_connect( "localhost", $user , $pass ); > if ( ! $db_connect ) > { > $dberror = "Couldn't connect to update."; > return false; > } > if ( ! mysql_select_db( $db, $db_connect) ) > { > $dberror = mysql_error(); > return false; > } > > if ( $search_sex=="" && $search_city=="" && > $search_state=="" && > $search_orientation=="" && $search_status=="" && > $search_race=="" ) > { > $db_query = "SELECT * FROM gay ORDER BY _date"; > } > > if ( !$search_sex=="" || !$search_city=="" || !$search_state=="" > || > !$search_orientation=="" || !$search_status=="" || > !$search_race=="" ) > { > $db_query = "SELECT * FROM gay WHERE "; > > if ( ! $search_sex=="" && ! $search_sex=="Any" ) > $db_query .= "$search_option _sex='$search_sex' "; > > if ( ! $search_city=="" ) > $db_query .= "$search_option _city='$search_city' "; > > if ( ! $search_state=="" && ! $search_state=="Any" ) > $db_query .= "$search_option _state='$search_state' "; > > if ( ! $search_orientation=="" && ! $search_orientation=="Any" ) > $db_query .= "$search_option _orientation='$search_orientation' "; > > if ( ! $search_status=="" && ! $search_status=="Any" ) > $db_query .= "$search_option _status='$search_status' "; > > if ( ! $search_race=="" && ! $search_race=="Any" ) > $db_query .= "$search_option _race='$search_race' "; > > $db_query .= "ORDER BY _date"; > } > > print "Query is: $db_query, that's all<BR><BR>"; > > $search_result = mysql_query( $db_query, $db_connect ); > $num_rows_returned = mysql_num_rows( $search_result ); > if ( ! $search_result ) > die ( "Error searching the personals database: ".mysql_error() ); > > $results_returned = array(); > while ( $a_row = mysql_fetch_array( $search_result )); > array_push( $results_returned, $a_row ); > > print "Number of rows returned is: $num_rows_returned<BR>"; > > $num_returned = 0; > while ( $num_returned < $num_rows_returned ) > { > print "Number returned is: $num_returned <BR>"; > $_fname = $results_returned[$num_returned][_fname]; > $_lname = $results_returned[$num_returned][_lname]; > $_email = $results_returned[$num_returned][_email]; > $_uname = $results_returned[$num_returned][_uname]; > $_pword = $results_returned[$num_returned][_pword]; > $_sex = $results_returned[$num_returned][_sex]; > $_age = $results_returned[$num_returned][_age]; > $_city = $results_returned[$num_returned][_city]; > $_state = $results_returned[$num_returned][_state]; > $_orientation = $results_returned[$num_returned][_orientation]; > $_status = $results_returned[$num_returned][_status]; > $_race = $results_returned[$num_returned][_race]; > $_religion = $results_returned[$num_returned][_religion]; > $_height = $results_returned[$num_returned][_height]; > $_weight = $results_returned[$num_returned][_weight]; > $_hair = $results_returned[$num_returned][_hair]; > $_eyes = $results_returned[$num_returned][_eyes]; > $_education = $results_returned[$num_returned][_education]; > $_occupation = $results_returned[$num_returned][_occupation]; > $_interests = $results_returned[$num_returned][_interests]; > $_match = $results_returned[$num_returned][_match]; > $_other = $results_returned[$num_returned][_other]; > $_date = $results_returned[$num_returned][_date]; > > print "<BR><BR><TABLE border=1>\n"; > print "<TR>\n"; > print > > "\t<TD>$_fname</TD>\n<TD>$_lname</TD>\n<TD>$_email</TD>\n<TD>$_uname</TD>\n\ > > n<TR>&nbsp;</TR>\n\n<TD>$_pword</TD>\n<TD>$_sex</TD>\n<TD>$_age</TD>\n<TD>$_ > > city</TD>\n<TD>$_state</TD>\n<TD>$_orientation</TD>\n<TD>$_status</TD>\n<TD> > > $_race</TD>\n<TD>$_religion</TD></TR>\n\n<TR>&nbsp;</TR>\n\n<TR><TD>$_height > > </TD>\n<TD>$_weight</TD>\n<TD>$_hair</TD>\n<TD>$_eyes</TD>\n<TD>$_education< > > /TD><TD>$_occupation</TD>\n<TD>$_interests</TD>\n<TD>$_match</TD>\n<TD>$_oth > er</TD>\n<TD>$_date</TD>\n"; > print "</TR>\n"; > print "</TABLE><BR><BR>\n\n"; > print "<BR>Your search has completed, thank you for using the personals > system.<BR>"; > $num_returned++; > } > return true; > } > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#27810) next »