neridaj Posted January 23, 2008 Share Posted January 23, 2008 Hello, I can't figure out how to extract certain strings from a sql query to use them as variable names. I would like to take a sql query such as: "select publication_id, publication_title, publication_issue, publication_page, publication_name, publication_author, publication_date, publication_year from publications" and only be left with an array filled with id, title, issue, etc. Here is how I'm trying to extract the variable names from the sql string, which is what I can't figure out how to do: /* $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 = strtok($str, "_"); $dbvar = preg_replace("^[a-zA-Z]+_$", "", $dbvar); echo($dbvar[$i]); } */ I would like it to just echo only the column name following the "_". If someone can steer me in the right direction I would really appreciate it. Thanks, J Link to comment https://forums.phpfreaks.com/topic/87438-tokenize-and-replace/ Share on other sites More sharing options...
fanfavorite Posted January 23, 2008 Share Posted January 23, 2008 Try this: $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[$i] = substr(strrchr($str[$i],"_"), 1); echo $dbvar[$i]; } Link to comment https://forums.phpfreaks.com/topic/87438-tokenize-and-replace/#findComment-447238 Share on other sites More sharing options...
cooldude832 Posted January 23, 2008 Share Posted January 23, 2008 your both completly wrong because you have no clue what you are working with mysql_query returns a resource that resource must further be acted on to return anything useful out of it, it can't do anything for you on it. So exploding it does nothing. Read tutorials on mysql/php to figure this out because its so elementary I shouldn't need to explain it. Link to comment https://forums.phpfreaks.com/topic/87438-tokenize-and-replace/#findComment-447270 Share on other sites More sharing options...
fanfavorite Posted January 23, 2008 Share Posted January 23, 2008 Easy buddy, I was basing it on the string variable, not on mysql commands. $result = mysql_query("SHOW COLUMNS FROM Features"); if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { echo substr(strrchr($row[Field],"_"), 1); } } Link to comment https://forums.phpfreaks.com/topic/87438-tokenize-and-replace/#findComment-447329 Share on other sites More sharing options...
cooldude832 Posted January 23, 2008 Share Posted January 23, 2008 well if you gonna help someone don't be so vague about it, you need to explain instead of assuming. Link to comment https://forums.phpfreaks.com/topic/87438-tokenize-and-replace/#findComment-447336 Share on other sites More sharing options...
resago Posted January 24, 2008 Share Posted January 24, 2008 are you looking for something like extract() might give you? Link to comment https://forums.phpfreaks.com/topic/87438-tokenize-and-replace/#findComment-447427 Share on other sites More sharing options...
neridaj Posted January 24, 2008 Author Share Posted January 24, 2008 Thanks fanfavorite, that's almost what I needed. The only thing I need to fix is chopping off the "from" portion of the sql statement. I really appreciate the help. cheers, J Link to comment https://forums.phpfreaks.com/topic/87438-tokenize-and-replace/#findComment-448007 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.