yasic Posted January 31, 2007 Share Posted January 31, 2007 Can anyone please tell me what the specific code to do this will be:You have a mysql database, and in it theres a table with 2 columns, the first one will name names, and the second one will have numbers.Lets say in the variable $nicename is a name that is stored currently in the mysql table.You need to:1. Print out the current name and number.2. Increase the number by 1 (In The Database, not just on screen)3. print out the new number.How would you do this? Quote Link to comment Share on other sites More sharing options...
chronister Posted January 31, 2007 Share Posted January 31, 2007 1. query the db and retrieve the info, echo on screen2. $var_name++ to increment by 1, then run another query to UPDATE that record3. echo $var_nameThis is simplistic, but it's the general idea.Nobody wants to write the code for you, but get some code going and if you get stuck post and we'll help. Quote Link to comment Share on other sites More sharing options...
yasic Posted January 31, 2007 Author Share Posted January 31, 2007 Yeah, I kinda got that part, its some of the actual code pieces that I have been troubled with, I left those 2 pieces as comments. Various sites have shown me so far how to run a while loop to display all the pieces stored from a mysql_query WHERE command, however I could not find one without the use of a while loop which seems un-necesary for 1 piece of data. And I saw absolutly nothing about changing the value of a stored piece of data in mysql. Anyways, heres what it would look like:Oh, and thanks for your help :)[code]<?php$con = mysql_connect("localhost","The_User","The_Password");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("The_db", $con); $result = mysql_query("SELECT * FROM The_table WHERE Name='$nicename'", $con);// How would you select the number by itself? (If this requires a while loop, I know how to do it with that, so just say so)?echo "Your first number is ". $However_you_got_this . "<br />";$However_you_got_this++;// How do you run a query to modify the number in the database?echo "Your modified number is ". $However_you_got_this . "<br />";?>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 31, 2007 Share Posted January 31, 2007 your need to be crafty here and -- the first number meaning take one from the curent database result. Quote Link to comment Share on other sites More sharing options...
chronister Posted January 31, 2007 Share Posted January 31, 2007 [code]<?php$con = mysql_connect("localhost","The_User","The_Password");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("The_db", $con); $result = mysql_query("SELECT * FROM The_table WHERE Name='$nicename'", $con);$the_number=mysql_fetch_assoc($result);$number=$the_number['field_name'];echo "Your first number is ". $number . "<br />";$number++;$query="UPDATE The_table SET number=$number";$result=mysql_query($query)echo "Your modified number is ". $number . "<br />";?>[/code]Try that, it should work Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 31, 2007 Share Posted January 31, 2007 this will work the cheating way? lol[code]<?php$con = mysql_connect("localhost","The_User","The_Password");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("The_db", $con); $result = mysql_query("SELECT * FROM The_table WHERE Name='$nicename'", $con);while($x=mysql_fetch_assoc($result)){echo "Your first number is $x['However_you_got_this']-1; \n";echo "Your modified number is $x['However_you_got_this']; \n";}$query="UPDATE The_table SET number=number++";$result=mysql_query($query)?>[/code] Quote Link to comment Share on other sites More sharing options...
chronister Posted January 31, 2007 Share Posted January 31, 2007 @redarrow Out of Curiosity, why would another page be needed for the update query? Why not simply do it like I have it and run the update after the number is incremented? Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 31, 2007 Share Posted January 31, 2007 your way wont work as the update will show the number that has been updated and not the oreganal number that why i use -1.it all on one page now ok. Quote Link to comment Share on other sites More sharing options...
yasic Posted February 1, 2007 Author Share Posted February 1, 2007 This works great :)Thanks for all of your help! Quote Link to comment 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.