Jump to content

Recommended Posts

Hi there!

 

I'm just wondering if it possible to store php variables, such as $int, into a mysql database, and then retrieve it.

 

For example, lets say that I declare $int=0.

$string="hi there ".$int;

Then I write that string to the table.

 

I then set $int=1.

 

Later on, I retrieve that string from the table. Now I want the string to say "hi there 1", not "hi there 0";

 

Is there someway to do this is?

 

Thanks a lot!

Link to comment
https://forums.phpfreaks.com/topic/184464-variables-in-mysql-records/
Share on other sites

You can achieve that by following the steps you laid out....

 

add a record into the database with whatever information, and retrieve the data from the database.

 

may i suggest a MySQL tutorial?

http://www.tizag.com/mysqlTutorial/

Really?

 

So If I write "Hi ".$username to the table, When $username is set to user.

 

Will it not write "Hi user" to the table?

 

If I change the $username later on in the program, to admin, for example, and then pull the string from the table...

Will it be changed to "Hi admin?"

 

Sorry for my newbiness  :D

 

 

I think mikesta707 might not understand exactly what you mean (or maybe it is me). If I understand you correctly, you want to store a "reference" to a php variable so that, when it changes in php, it changes in the database? Theoretically this idea isn't logical because you will only be changing the variable in php, so when querying the database, you will already have that value in php.

 

By your example, if you just stored "Hi" in the database without the $username variable, then you can just use the value from the database and concatenate it with whatever the $username variable has changed to ($dbvalue . $), it would be the same thing.

 

Can you be more specific as to what you are trying to accomplish? I think you might be going about this the wrong way.

What I want to accomplish is the following:

 

In my code:

 

<?php
$haystack=$_REQUEST['msg'];
$needle='<reply>';
$username="fearmyawesome_1";
$password="eragon1";
$database="fearmyawesome_bo";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$itemquery="SELECT item FROM inventory ORDER BY RAND()";
$item=mysql_query($itemquery);
$pos1=stripos($haystack, $needle);
if($pos1!==false){
$pos2=$pos1+7;
$condition=substr($haystack, 0, $pos1);
$reply=substr($haystack, $pos2);
$query="INSERT INTO responses VALUES ('".$condition."','".$reply."')";
$int=0;
$print='stuff';
$loop="yes";
while ($loop=="yes"){
$testquery="SELECT * FROM `responses` WHERE `condition`='".$condition.$int."'";
$test=mysql_query($testquery);
if(mysql_num_rows($test) > 0){
$int=$int+1;
}
else{
$query="INSERT INTO responses VALUES ('".$condition.$int."','".$reply."')";
mysql_query($query);
mysql_close(); 
echo "success if no error message.";
$loop="no";
}
}
}
else if('bucket: remember that' == $_REQUEST['msg']){
$value=$_POST['step'];
$value=$value-2;
$writestring=$_POST['value'.$value];
$writestring=$writestring."\n\n";
echo 'Okay, Remembering: ';
echo $writestring;
$file='remember.txt';
file_put_contents($file, $writestring, FILE_APPEND);
}
else if('bucket: remember all' == $_REQUEST['msg']){
echo 'www.fearmyawesome.justfree.com/remember.txt';
}
else{
$needle2="/me gives bucket";
$pos3=stripos($haystack, $needle2);
if($pos3!==false){
$pos4=$pos3+16;
$insertitem=substr($haystack, $pos4);
$delquery="DELETE FROM `inventory` ORDER BY time ASC LIMIT 1";
$query="INSERT INTO inventory VALUES ('$insertitem',NOW())";
mysql_query($delquery);
mysql_query($query);
echo "Bucket is now carrying".$insertitem;
mysql_close();
}
else{
$query="SELECT * FROM `responses` WHERE `condition` LIKE '".$_POST['msg']."%' ORDER BY RAND() LIMIT 1";
$result=mysql_query($query);
$print=mysql_fetch_assoc($result);
echo $print['reply'];
mysql_close();
}
}
?>

 

This works fine for appending nearly all values into the table. However, in the first part of my code, $item is a random item pulled from the inventory table.

 

Let's say I have written the following into my responses table:

 

condition                          reply

generate item                    $item

 

Now when I pull that record out of the reply table, I would like $item to change to what $item is in my PHP code.

  • 4 weeks later...

If you want $item to change to what $item is in your PHP, why not just use $item from your PHP? If I understand you correctly, you could accomplish what you want by simply overwriting the query response with whatever is in your PHP:

$row['reply'] = $item;

 

If that is not what you are trying to do, try explaining what you are logically trying to accomplish.

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.