Hi,
I'm a beginner at MySQL though I'm a bit advanced in PHP.
I had a question.
Say, supposing I had a couple of rows in a table called "test_table" in my database (which has one integerical field).
Now, I want to display double the amount of the integerical values. So, how do I do that?
Thanks
| GameFreak wrote: |
| I want to display double the amount of the integerical values. So, how do I do that? |
You multiply the original value by 2
| Code: |
<?php
// ...
$double_value = $original_value * 2;
echo $double_value;
## or you could do it without the temporary variable
## echo $original_value * 2;
// ...
?> |
if you use phpMyAdmin, just add "X" amount more of rows/columns to the table..........
with phpMyAdmin, it does things automatically so you dont have to write out the actual code....
did answer your question?
good luck! 
I know how to multiply it. I just don't know how to display it.
You know, how to make the value an integerical value. Please create a PHP Code of an entire web page showing the connections to the database, etc.
Thanks
| GameFreak wrote: |
| I know how to multiply it. I just don't know how to display it. |
echo (and print()) works for me.
Would you please show some code?
I don't think you are understanding my problem.
I have a table in my database called "test_table".
It has two fields.
The first field is "test_var" and the second is "test_no".
The "test_no" field is what I am talking about.
I have 1 record in which the value of "test_no" is say "14". It is an INT datatype.
I want to display double of it("28")t on a page called "index.php". How do I do that? That's what I want to know.
| Code: |
<?php
### ZEROTH STEP: Define constants used throughout the code
### Better if you place them in a separate file and
### require it
define('DB_HOST', 'localhost');
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_DATA', 'database');
### FIRST STEP: Connect to the database
$con = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if (!$con) {
exit('There was an error connecting to the database.<br>The error was: ' . mysql_error());
}
if (!mysql_select_db(DB_DATA)) die('There was an error selecting the database.<br>The error was: ' . mysql_error());
### SECOND STEP: Get data from the table
$sql = 'select test_no from test_table';
$res = mysql_query($sql);
if (!$res) {
exit('There was a query error.<br>The error was: ' . mysql_error());
}
while ($row = mysql_fetch_row($res)) {
### THIRD STEP (inside the loop): print the data
echo 'The double of the value is ', 2*$row[0], ".<br>\n";
}
### FOURTH STEP: release resources
mysql_free_result($res);
### FIFTH STEP: close the database
mysql_close($con);
?> |
Thanks a lot.
I really appreciate it.
That's exactly what I needed.
This topic can be locked.