Jump to content

[SOLVED] Calling PHP variables from MYSQL data?


woolyg

Recommended Posts

This could be an easy one.. or just not possible:)

 

I'm trying to write a commentary engine for a game idea that I'm doing. I'm declaring standard variables at the start of the page, then what I want to do is call a commentary bit of text from MySQL. The commentary itself contains references to PHP variables declared earlier in the page.

 

However, when I call the text from the DB, the variable is printed in plain text on the page - it doesn't refer to the variable declared earlier in the page.

 

 

example:

<?php

$sex_possessive_single = "his";

/*
$query = "
SELECT * 
FROM commentary 
WHERE ball_start = '$starter' 
ORDER BY RAND() 
LIMIT 1 
";
$run_query = mysql_query($query);
$display_query = mysql_fetch_assoc($run_query);

... as a result of the above query, the output is
*/

$commentary = "The keeper falls backwards, and somehow the ball hits the base of $sex_possessive_single feet, preventing catastrophe. The right defender takes the ball under control, and moves off.";


?>

 

The commentary being printed is:

The keeper falls backwards, and somehow the ball hits the base of $sex_possessive_single feet, preventing catastrophe. The right defender takes the ball under control, and moves off.

 

I was expecting it to say:

The keeper falls backwards, and somehow the ball hits the base of his feet, preventing catastrophe. The right defender takes the ball under control, and moves off.

 

- I want the output to say HIS where it says $sex_possessive_single.

 

Is there a way to place a PHP variable in the form of $variable into a mysql table (varchar field) and have the variable populate properly upon recall?

 

I hope this is clear enough to understand.

 

Cheers all,

WoolyG

 

Link to comment
Share on other sites

I don't know how to do exactly what you asked.

 

One thing you could do is write:

 

The keeper falls backwards, and somehow the ball hits the base of %s feet, preventing catastrophe. The right defender takes the ball under control, and moves off.

 

And then

 

$commentary = sprintf($commentary, $sex_possessive_single);

 

You can have as many %s (strings) or other things like %d for when an integer is expected, then just list them afterwards as parameters to sprintf() or printf(). So long as they are in the same order.

 

Or you can do a str_replace()

 

$commentary = str_replace("\$sex_possessive_single", $sex_possessive_single, $commentary);

 

Trouble with that is you have to write it for every different variable.

Link to comment
Share on other sites

Hey Carth,

 

Thanks for the answer. I've had a brainstorm and decided to do a simple str_replace(). If I drop the dollar sign from the start of the sex_posssessive_single variable within the DB, and do the following:

 

 

<?php
$sex_possessive_single = "his";

$commentary_ttr = ("sex_possessive_single"); 
$commentary_rt = ($sex_possessive_single);

$commentary1 = $display_query['commentary'];
$commentary = str_replace($commentary_ttr, $commentary_rt, $commentary1);

?>

 

Once I change the entry within the db for the commentary to:

The keeper falls backwards, and somehow the ball hits the base of sex_possessive_single feet, preventing catastrophe. The right defender takes the ball under control, and moves off.

 

.. the str_replace changes:

 

sex_possessive_single

 

to

 

his

 

.. and it prints beautifully.

 

Thanks!

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.