Re: smarter code (mySQL arrays)

From: Date: Tue, 22 Jan 2002 01:32:47 +0000
Subject: Re: smarter code (mySQL arrays)
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-81466@lists.php.net to get a copy of this message
function Build_Array($mysql_result_pointer) { if(mysql_num_rows($mysql_result_pointer)) { $res[] = mysql_field_name($mysql_result_pointer); while($row = mysql_fetch_array($mysql_result_pointer)) { $res[] = row; } } return($res); } Jim Lucas bend.com ----- Original Message ----- From: "Justin French" <justin@indent.com.au> To: "php" <php-general@lists.php.net> Sent: Monday, January 21, 2002 4:12 PM Subject: [PHP] smarter code (mySQL arrays) > Hi all, > > I've got into the habbit of pulling data out of a table something like this: > > > $sql = "SELECT * FROM cd_review WHERE publish='yes' ORDER BY id DESC > LIMIT 3"; > $sql_result = mysql_query($sql); > print mysql_error(); > while ($sql_myrow = mysql_fetch_array($sql_result)) > { > // pull out the data > $id = $sql_myrow["id"]; > $date = $sql_myrow["date"]; > $user_id = $sql_myrow["user_id"]; > $artist = $sql_myrow["artist"]; > $title = $sql_myrow["title"]; > // edited out about 10 colums for clarity > } > > Now, I reckon there must be a way of automating the task of making the > $title var out of $sql_myrow["title"] etc etc for starters, which would > really help on tables with lots of columns. > > Then, it'd be great if I could automate this further to automaticaly to > a stripslashes() on each var, then possibly nl2br() etc. > > > In psuedo-code I guess it looks something like: > > while($sql_myrow = mysql_fetch_array($sql_result)) > { > > // we have an array of one row > > <use a foreach loop to stripslashes() on each element of the array> > > <use a foreach loop to nl2br() on each element of the array> > > <use a foreach loop to take each element (eg $sql_myrow["id"]) and > create a var ($id)> > > } > > > Of course I'm a way newbie on arrays, and I've read the manual, but am > unclear on the syntax for the above. > > > Ultimately, I'd like to put this all into a function which I call that > does it all in one hit, but I'll take that in a smaller step :) > > > Justin French > > -- > 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 (#81466) next »