Page 1 of 1
total newbie
Posted: Mon Apr 21, 2008 7:34 pm
by xliquidflames
heres a really newbie question..
lets say that in the merch1 colum in an sql database and there are 3 rows containing the information... 1, 3, and 8.
ive got this sql query:
select sum(merch1) from numbers
when the query is run it gives me an output of 12 which is the total of all the rows in the merch1 column.
so now i want to display this SUM total on my web page... so ive got this so far in php:
$query = "SELECT SUM(merch1) FROM numbers";
how in the world do i get the web page to display that results on the web page?
i tried:
echo $query;
but all i got on the webpage was:
Resource id #1
...what gives?
im totaly new to php and im going by a couple of books im reading, one is php/sql for dummies and the other is a book a friend gave me, and all the tutorials and manuals on the web that my brain can handle...
Re: total newbie
Posted: Mon Apr 21, 2008 8:33 pm
by yacahuma
Plenty of documentation on the web.
You have to execute the query and the fetch the data.
http://us2.php.net/manual/en/mysqli.query.php
if you use a nice library like php adodb
Code: Select all
$count = $db->getOne('select count(*) from table');
Re: total newbie
Posted: Tue Apr 22, 2008 12:26 am
by lbfb
xliquidflames
its like yacahuma said!
most of the tutorials for this kind of thing will say something like:
$sql = "SELECT * FROM myTable;
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
this will give you an array matching your selection criteria. if your table has two fields (field1, field2), then you van view them with
echo $row['field1'];
There is a lot more to it than that, but hopefully that will be enough to get you over this hurdle - and send you on to the next one.
Re: total newbie
Posted: Tue Apr 22, 2008 11:40 am
by xliquidflames
thank you guys so much for replying! i think im making progress but ive got another question..
in all my readings something i have yet to figure out is..
$restult and $row
are these just generic variable names that folks use for tutorial purposes or are those actual php functions?
for example.. could it actually read something like this:
Code: Select all
$getTotal = "SELECT * FROM myTable";
$done = mysql_query($getTotal);
$line = mysql_fetch_assoc($done);
and this has been a lot of help. its got me thinking about this in another way. would it be easier to say something like this?
Code: Select all
$sql = "SELECT merch1 FROM myTable";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo $row['field1' + 'field2' + 'field3'];
not sure if my syntax is correct there cause im at work right now - dont have all my reference materials with me..
...and the mysql_fetch_assoc is still a mystery to me. i thought i had a good handle on arrays until i ran across that... everything ive read so far on it is either a bunch of technical gobbledy gook or doesnt explain it thoroughly enough.. just a brief mention... "oh yeah by the way theres also mysql_fetch_assoc can be used also... moving on..."
if i understand things correctly when you query the database, that results is stored in a temporary location, an array, for example ..and then you have to pull it out of that temporary location when you want to display it.. i just dont know if im going about this the right way. perhaps i need to go back to the basics and look over syntax again.
thanks again guys.