Jump to content

[SOLVED] A complex piece of code...


yasic

Recommended Posts

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?
Link to comment
Share on other sites

1. query the db and retrieve the info, echo on screen

2. $var_name++ to increment by 1, then run another query to UPDATE that record

3. echo $var_name


This 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.

Link to comment
Share on other sites

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]
Link to comment
Share on other sites

[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
Link to comment
Share on other sites

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]


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.