This is the code I’m using:
<?php
// Set the MySQL Configuration
$db_host = "";
$db_user = "";
$db_password = "";
$db_name = "";
$db_table = "";
// Start Connection
$db_connect = mysql_connect ($db_host, $db_user, $db_password);
// Select Database
$db_select = mysql_select_db ($db_name, $db_connect);
// Update Values in Database
$query = "UPDATE $db_table SET
age = age + 1,
land = '".$_POST['data3']."'
WHERE name = '".$_POST['data1']."'
";
// Execution MySQL query
$result = mysql_query($query) or die(mysql_error($db_connect));
//Close MySQL connection
mysql_close($db_connect);
//HTTP Response
echo " your age: age";
?>
I want to echo the value of the $age variable, but instead I always get the word “age.” For example, the code should echo your age: 5 but instead it outputs your age: age
First, you’ll need to run a
SELECTquery to retrieve the updated value ofage. The query should look something like this:Once you’ve obtained the result of that query, with say
PDO::fetch(see my note below about PDO) and set it to the variable$age, you can output it with an echo statement:Also, please don’t use
mysql_*functions for new code. They are no longer maintained and the community has begun the deprecation process (see the red box). Instead, you should learn about prepared statements and use either PDO or MySQLi. If you can’t decide which, this article will help you. If you care to learn, this is a good PDO tutorial.The reason I’m not giving you the exact code for this is because it shouldn’t be done with the
mysql_*functions at all. Creating an SQL query with data directly from$_POSTlike this is extremely dangerous code to use and an incredibly bad idea all around. Never do this. You open yourself up to numerous SQL injection attacks. Even usingmysql_real_escape_stringis not enough. You should be using prepared statements.UPDATE: Here is a simple example that’s close to what you’re asking, but using PDO and prepared statements. This is by no means a comprehensive example, since there are several ways to alter it that will still work (e.g. prepared statements allow you to execute multiple statements on the server in one statement), and I don’t have a working server at the moment to test to make sure it’s exactly what you need, but I hope it gets the point of across.
All of this information came directly from the PHP documentation on prepared statements and PDO::fetchColumn().