Coldman Posted October 6, 2007 Share Posted October 6, 2007 What i want to do is this. I have an field in one of my database tables with money price of product, i want to display the total amount of the table, i try to use "select sum(fieldname) from table where" but is not working please any help Quote Link to comment https://forums.phpfreaks.com/topic/72072-solved-help-with-mysql-column/ Share on other sites More sharing options...
trq Posted October 6, 2007 Share Posted October 6, 2007 Post your code. Quote Link to comment https://forums.phpfreaks.com/topic/72072-solved-help-with-mysql-column/#findComment-363165 Share on other sites More sharing options...
Coldman Posted October 6, 2007 Author Share Posted October 6, 2007 I dont know if you can find up what is happening with this code but lets try $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("manvisdatabse", $con); $query = mysql_query("select * from cart where CartID=$CartID"); $query1 = mysql_query ("SELECT SUM(TotalPrice) FROM Cart"); if(!qyery1) echo "Query lesh"; echo "<table align=center><tr><td width='100' >Product Name</td><td width='80'>Price</td><td width='80'>Quantuty</td><td width='80'>Total Price</td><td width='80'></td></tr></table><br>"; while($row=mysql_fetch_array($query)) { $CartID1=$row['CartID']; $ProductID1=$row['ProductID']; $ProductName1=$row['ProductName']; $Price1= $row['Price']; $Quantity1=$row['Quantity']; $TotalPrice1=$row['TotalPrice']; echo "<table align='center'><tr><td width='100'>$ProductName1</td><td width='80'>$Price1 $</td><td width='80'><input type='text' size=2 Value= $Quantity1 maxlength=5 name='Quantity'>"; echo "</td><td width='80'>$TotalPrice1 $</td><td width='80'><form action='show_cart.php' method='POST'>"; echo "<input type='hidden' size='5' maxlength='5' name='ProductID' Value=$ProductID1>"; echo "<input type='submit' value='Delete'></td></tr></table>"; } echo "<br>"; echo "<table align=center><tr><td width='100' ></td><td width='80'></td><td width='80'></td><td width='80'></td><td width='80'> $query1</td></tr></table><br>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/72072-solved-help-with-mysql-column/#findComment-363167 Share on other sites More sharing options...
Coldman Posted October 6, 2007 Author Share Posted October 6, 2007 Any help I just want to get the sum of one column i now this is the code but not working, something is missing $query = mysql_query ("SELECT SUM(TotalPrice) FROM cart"); if(!query) { echo "error"; } else echo $query; Any Help Quote Link to comment https://forums.phpfreaks.com/topic/72072-solved-help-with-mysql-column/#findComment-363177 Share on other sites More sharing options...
roopurt18 Posted October 6, 2007 Share Posted October 6, 2007 First off, try entering your query directly into phpMyAdmin to see if it behaves expectedly. SELECT SUM(TotalPrice) FROM Cart Once you're sure of that, you can check your PHP code for problems. The first obvious one I can see is in your first code post: $query1 = mysql_query ("SELECT SUM(TotalPrice) FROM Cart"); if(!qyery1) echo "Query lesh"; You have mistyped the variable name. In your second code post, you execute a query and save the result in $query, but echoing query isn't going to print the value from the DB. You still have to call mysql_fetch_array; normally I would use mysql_fetch_assoc but since you didn't alias the column, mysql_fetch_array is easier IMO. $query = mysql_query ("SELECT SUM(TotalPrice) FROM cart"); if(!$query) // <-- YOU FORGOT THE $ IN YOUR CODE { echo "error"; } else{ $row = mysql_fetch_array($query); echo $row[0]; } Basically you are being very sloppy. You should be able to catch things like missing dollar signs and misspelled variables based on interpreter errors, or just carefully reading your code. Also, even though they are optional when the body is a single line, you should always enclose your if and loop bodies in curly brackets. Don't be lazy. Quote Link to comment https://forums.phpfreaks.com/topic/72072-solved-help-with-mysql-column/#findComment-363181 Share on other sites More sharing options...
Coldman Posted October 6, 2007 Author Share Posted October 6, 2007 i found the solution any way thanks $result = mysql_query ("SELECT SUM(TotalPrice) as sum FROM cart where CartID=$CartID "); $sum = (int) mysql_result($result,0,"sum"); Quote Link to comment https://forums.phpfreaks.com/topic/72072-solved-help-with-mysql-column/#findComment-363182 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.