Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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