Jump to content

Tokenize and Replace


neridaj

Recommended Posts

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

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

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

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

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.