0

When I do a mysql select statement, I get the below when echo'ing $result;

echo $row['comment2'];

Gives me this:

$fullname = ddasdfd; 
$bacc = 324234234; 
$brouting = 23423434; 
$btype = checking; 
$btype2 = personal; 
$bankname = 234234234; 

How do I use that result that I got when select a text field inside my mysql table and assign it to php variables so I can do:

echo $fullname;

Any kind of help I can get on this is greatly appreciated!

Current code:

$query = "select * FROM `store`.`oc_order` WHERE `order_status_id`='10' AND `achgroup`='' ORDER BY `date_added` LIMIT 10"; 
  $result = mysql_query($query,$db) or die(mysql_error());
  while($row = mysql_fetch_array($result)) {

  echo $row['comment2'];

I want when echo'ing ddasdfd:

1
  • 2
    paste your current code Commented Feb 27, 2014 at 6:58

3 Answers 3

2

While most obvious way would be something that involves eval() - I strongly recommend you to avoid that. It's completely unsafe.

Thus, you can resolve your case with some regex stuff and extract() results:

$result='$fullname=ddasdfd; $bacc=324234234; $brouting=23423434; $btype=checking; $btype2=personal; $bankname=234234234;';
preg_match_all('/\$([a-zA-Z][a-zA-Z0-9]*)\s*\=\s*([^;]*);/ms', $result, $matches);
$matches = array_combine($matches[1], $matches[2]);
extract($matches); //now $fullname, $bacc and so on are in global scope

Note, that:

  • It will not check syntax. Yes, it will fail if syntax isn't correct
  • It will not check if variables have valid names
  • It will not check if values are "good" (so delimiter is ;)

And, in fact, it shouldn't do that (because regex isn't thing that may or should be used for such issues). To say more, as more correct way I see normalization of your initial data somehow that you will avoid things like this - i.e. dealing with parsing/executing PHP code.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there anyway we can remove any odd characters within the results... I noticed that if I have $fullname=dasdddf&adfsdfd - then the code acts up....
What characters are 'odd' and where?
1

If your $result variable contains a string with var_name = value separated by \n then you can do :

$results = explode('\n', $results);
foreach ($results as $result){
    eval($result);
}

echo $fullname;

Pay attention though, eval can lead to important security issues. Don't eval untrusted data.

Comments

0

Not having seen your code, it should look something like this:

$resultsArray = mysql_fetch_assoc($result);
$fullname = $resultsArray['fullname'];
$brouting = $resultsArray['brouting']; 
$btype = $resultsArray['btype']; 
$btype2 = $resultsArray['btype2']; 
$bankname = $resultsArray['bankname']; 

From here you should be able to echo the variables freely.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.