darkeye Posted December 8, 2009 Share Posted December 8, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/184464-variables-in-mysql-records/ Share on other sites More sharing options...
mikesta707 Posted December 8, 2009 Share Posted December 8, 2009 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/ Quote Link to comment https://forums.phpfreaks.com/topic/184464-variables-in-mysql-records/#findComment-973773 Share on other sites More sharing options...
darkeye Posted December 8, 2009 Author Share Posted December 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/184464-variables-in-mysql-records/#findComment-973780 Share on other sites More sharing options...
mikesta707 Posted December 8, 2009 Share Posted December 8, 2009 yes.. assuming that you go about it correctly Quote Link to comment https://forums.phpfreaks.com/topic/184464-variables-in-mysql-records/#findComment-973781 Share on other sites More sharing options...
darkeye Posted December 8, 2009 Author Share Posted December 8, 2009 alright thanks! Quote Link to comment https://forums.phpfreaks.com/topic/184464-variables-in-mysql-records/#findComment-973783 Share on other sites More sharing options...
mikesta707 Posted December 8, 2009 Share Posted December 8, 2009 No problem! you can mark your topic as solved by pressing the "Mark Solved" button at the bottom of the thread, and don't be afraid to come back if you hit a snag Quote Link to comment https://forums.phpfreaks.com/topic/184464-variables-in-mysql-records/#findComment-973788 Share on other sites More sharing options...
lemmin Posted December 9, 2009 Share Posted December 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/184464-variables-in-mysql-records/#findComment-973839 Share on other sites More sharing options...
darkeye Posted December 9, 2009 Author Share Posted December 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/184464-variables-in-mysql-records/#findComment-974408 Share on other sites More sharing options...
darkeye Posted December 10, 2009 Author Share Posted December 10, 2009 *-Bump for help-* Quote Link to comment https://forums.phpfreaks.com/topic/184464-variables-in-mysql-records/#findComment-974483 Share on other sites More sharing options...
darkeye Posted December 10, 2009 Author Share Posted December 10, 2009 *-bump-* Any suggestions? Do I have to use PHP for this? Quote Link to comment https://forums.phpfreaks.com/topic/184464-variables-in-mysql-records/#findComment-975090 Share on other sites More sharing options...
lemmin Posted January 4, 2010 Share Posted January 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/184464-variables-in-mysql-records/#findComment-988407 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.