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