Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
SQL
create table test
(
	name varchar(20),
	gender varchar(20),
	salary varchar(20)
);

insert into test values ('nm1','male','20000');
insert into test values ('nm2','female','85000');
insert into test values ('nm3','male','1000');
insert into test values ('nm4','unknown',NULL);


I am new to PHP, just learning.

I've a sample data of my table. While showing data in front, for gender I've to show
only the first character i.e. m/f/u and for salary I've to sum the total amount from front i.e
by using PHP.

also in my table salary is in varchar would I've to parse it as number?

PHP
<pre lang="xml"><?php
        include('conn.php');
        $result =mysql_query("select * from test")
            or die(mysql_error());

        echo "<table cellpadding = '10'>";
        echo "<tr>
                <th>name:</th>
                <th>gender:</th>
                <th>total salary:</th>
            </tr>";

        while($row = mysql_fetch_array($result))
        {
            echo "<tr>";
            echo '<td>'.$row['name'].'</td>';
            echo '<td>'.$row['gender'].'</td>';
            echo '<td>'.$row['salary'].'</td>';
        }
    ?>
Posted
Updated 20-Jan-13 22:05pm
v2

1 solution

in gender you can use
echo '<table><tbody><tr><td>'.substr($row['gender'],0,1).'</td></tr></tbody></table>';
instead  of echo '<table><tbody><tr><td>'.$row['gender'].'</td></tr></tbody></table>';

and you don't have to change it to number or double. php will auto convert it
 
Share this answer
 
Comments
nischalinn 20-Jan-13 22:58pm    
Thank you. What about getting the total sum of salary??
Kislay Raj 23-Jan-13 14:15pm    
mean what you have all names are unique, each have unique salary value it's not monthly or any other period you have describe so they will give the values what you have entered if you want to get all sum value of salary you can use Sum

SELECT SUM(salary) FROM test

if you want to sum in php then set a variable like
$a=0;
before while and in while loop use
$a+=$row['salary'];

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900