Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Variables

Php MySql Dynamic Variable

         

vikram

12:37 pm on May 1, 2007 (gmt 0)

10+ Year Member



Hi friends,

I am NEWBIE & trying to develop internal control system with PHP-MySql & have one problem.

I want to enter dynamic variable into MySql like :

"I have checked report of Mr. $userName from $previousDate to $currentDate…..”

Wherein - $username, $previousDate, $currentDate will be called from other table.

My codes :

PHP Code:

INSERT INTO `checkReport ` (`schID`, `schNo`, `noteNo`, `subschName`, `content`) VALUES (1, 1, 'a', '', ' I have checked report of Mr. ‘.$username.’ from ‘.$previousDate.’ to ‘.$currentDate.’ ….)

which is not working… Please guide me through.

Thanks,
Vikram

eelixduppy

12:42 pm on May 1, 2007 (gmt 0)



Welcome to WebmasterWorld, vikram!

Where are you defining those variables that you want to insert into your query? Also, are you receiving any errors from PHP or MySQL? The query that you have above looks fine. What's actually not working?

vikram

12:51 pm on May 1, 2007 (gmt 0)

10+ Year Member



Hi,

Thanks for your prompt reply & warm welcome.

At my php page - on echo or print I get following statement with blank dynamic values like :

"I have checked report of Mr. from to …..”

Please help me.

Vikram

mcavic

1:22 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:

$query = " INSERT INTO checkReport (schID, schNo, noteNo, subschName, content) ";
$query .= " VALUES ('1', '1', 'a', '', 'I have checked report of Mr. $username from $previousDate to $currentDate') ";

It's easier to read. If the contents of the variables still don't show up, it's because the variables aren't defined.

vikram

6:26 pm on May 1, 2007 (gmt 0)

10+ Year Member



I have tried it like this

<?
$username="Mr.XYZ";
$previousDate="31/12/2006";
$currentDate="31/04/2007";

print
'I have checked report of Mr. $username from $previousDate to $currentDate';

And result I get is :

'I have checked report of Mr. $username from $previousDate to $currentDate';

Values are not displayed

Vikram

mcavic

7:23 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



print 'I have

That's the problem -- when you use single quotes in PHP, the variables inside aren't expanded.

For PHP strings, you should use double quotes.
In PHP arrays like $a['foo'] you should use single quotes.
In MySQL statements, you should use single quotes.

vikram

7:26 am on May 2, 2007 (gmt 0)

10+ Year Member



I'll be adding content to database manually.

Vikram

eelixduppy

10:50 am on May 2, 2007 (gmt 0)



Giving up, vikram?

The problem seems to lie with your variables. It doesn't seem like they are being defined correctly. Show us where you are getting the variables $userName, $previousDate, $currentDate.

vikram

11:30 am on May 2, 2007 (gmt 0)

10+ Year Member



Thanks for your kind support & due to it I cant even think of giving up. I meant to say that data – content will be added manually to database for backend as it will be a one time job & the exercise we are doing is for ultimate front end result.

<?
//Variables defined

$username="Mr.XYZ";
$previousDate="31/12/2006";
$currentDate="31/04/2007";

print
'I have checked report of Mr. $username from $previousDate to $currentDate';

And result I get is :

'I have checked report of Mr. $username from $previousDate to $currentDate';

?>

Thanks again,
Vikram

eelixduppy

11:32 am on May 2, 2007 (gmt 0)



Yes, ok, this is an issue with the quotes. Try this:

print "I have checked report of Mr. $username from $previousDate to $currentDate";

vikram

12:07 pm on May 2, 2007 (gmt 0)

10+ Year Member



Some more & clear information – may be we can get some result.
<?

//Data fetched from Table A
$username=$row_view[‘username’];
$previousDate= $row_view[‘previousDate’];
$currentDate=$row_view[‘currentDate’];

//[value in Table A = ('Mr. XYZ’, ‘31/12/2006’, ‘02/05/2007’) ]

//Data fetched from Table B
$content =$row_view[‘content’];

//[value in Table B = ('1', '1', 'a', '', 'I have checked report of Mr. $username from $previousDate to $currentDate') ]

print
”$content”;

I want result to be like this

I have checked report of Mr. XYZ from 31/12/2006 to 02/05/2007.

Whereas I am getting result like this :

"I have checked report of Mr. $username from $previousDate to $currentDate"

?>

Thanks again,
Vikram

omoutop

12:13 pm on May 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



try something this:

$username="Mr.XYZ";
$previousDate="31/12/2006";
$currentDate="31/04/2007";

echo "I have checked report of Mr. ".$username." from ".$previousDate." to ".$currentDate."";

Do you get any results? I do when i run it to my pc

coopster

3:42 pm on May 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



vikram is trying to parse the "placeholder" variables that are previously written into database table B as PHP variables that have not yet been parsed. So, the text string in the column literally has the dollar sign and variable name.

Once the variables have been extracted from Table A and moved into the global namespace of the script we need to evaluate the string as PHP code. PHP has a function for this properly named eval() [php.net].

<?php 
// Variables extracted from Table A:
$username = "Mr.XYZ";
$previousDate = "31/12/2006";
$currentDate = "31/04/2007";
// Variable from Table B that contains PHP code
// we want to parse (evaluate):
// $row_view['content'] = 'I have checked report of Mr. $username from $previousDate to $currentDate';
// Create a variable named $content that contains the parsed PHP variables:
eval("\$content = \"{$row_view['content']}\";");
print $content;
// prints:
// I have checked report of Mr. Mr.XYZ from 31/12/2006 to 31/04/2007
exit;
?>

vikram

6:00 am on May 7, 2007 (gmt 0)

10+ Year Member



Perfect. Its working.

Thanks a million

Vikram