neridaj Posted January 15, 2008 Share Posted January 15, 2008 Hey, I'm trying to make a function that extracts variables from an sql query by looping through with explode. Is there a better way to go about this, maybe using a regular expression or something? /* $sql = "select publication_id, publication_title, publication_issue, publication_page, publication_name, publication_author, publication_date, publication_year from publications"; $demoResult = mysql_query( $sql ); $str = explode(", ", $sql); $i = count($str); for($i=count($str)-1; $i>=0; $i--){ $dbvar = explode("_", $str[$i]); echo($dbvar[$i]); } */ Thanks, Jason Quote Link to comment https://forums.phpfreaks.com/topic/86187-exploding-sql-query/ Share on other sites More sharing options...
The Little Guy Posted January 15, 2008 Share Posted January 15, 2008 I don't see any variables in your query. Quote Link to comment https://forums.phpfreaks.com/topic/86187-exploding-sql-query/#findComment-440179 Share on other sites More sharing options...
p2grace Posted January 15, 2008 Share Posted January 15, 2008 Is this what you're trying to accomplish?: $sql = "select publication_id, publication_title, publication_issue, publication_page, publication_name, publication_author, publication_date, publication_year from publications"; $demoResult = mysql_query( $sql ); while($arr = mysql_fetch_assoc($demoResult ){ extract($arr); echo $publication_id; echo $publication_title; // etc. } Quote Link to comment https://forums.phpfreaks.com/topic/86187-exploding-sql-query/#findComment-440196 Share on other sites More sharing options...
The Little Guy Posted January 15, 2008 Share Posted January 15, 2008 Or this? <?php $sql = "select publication_id, publication_title, publication_issue, publication_page, publication_name, publication_author, publication_date, publication_year from publications"; $arr = explode(", ",$sql); $narr = array(); foreach($arr as $val){ if(preg_match("~\s(.*)_(.*)~",$val,$matches)){ array_push($narr,trim($matches[0])); }elseif(preg_match("~(.*)_(.*)\s~",$val,$matches)){ array_push($narr,rtrim(str_replace(" from","",$matches[0]))); }else{ array_push($narr,$val); } } print_r($narr); ?> Quote Link to comment https://forums.phpfreaks.com/topic/86187-exploding-sql-query/#findComment-440200 Share on other sites More sharing options...
neridaj Posted January 15, 2008 Author Share Posted January 15, 2008 I guess I was a little vague on the variables. I want to extract the word following the "_" in the column names i.e., id, title, issue, page, etc. Quote Link to comment https://forums.phpfreaks.com/topic/86187-exploding-sql-query/#findComment-440230 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.