Jump to content

Exploding sql query?


neridaj

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/86187-exploding-sql-query/
Share on other sites

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.
   }

Link to comment
https://forums.phpfreaks.com/topic/86187-exploding-sql-query/#findComment-440196
Share on other sites

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);
?>

Link to comment
https://forums.phpfreaks.com/topic/86187-exploding-sql-query/#findComment-440200
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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