seabro Posted March 3, 2009 Share Posted March 3, 2009 Hi, Here is my mysql query. It works. SELECT dstchannel,sum(duration) from cdr where calldate like '2009-03-03%' group by substring(dstchannel,3,6) How can I display the result on my web page. I am trying the following but it not working and I don't know how to fix it. <?php $connect = mysql_pconnect("localhost","user","pass") or Die("connect failed"); $selectdb = mysql_select_db("dbase") or Die("select failed"); $query = "SELECT dstchannel,sum(duration) from cdr where calldate like '2009-03-03%' group by substring(dstchannel,3,6)"; $connect; $selectdb; mysql_query($query) or die ("query x failed"); $res = mysql_query($query,$connect) or die("failed"); while($qry = mysql_fetch_array($res)) { $dstchannel=$qry["dstchannel"]; print "$dstchannel"; print "<br>"; print "$duration"; print "<br>"; }; ?> Link to comment https://forums.phpfreaks.com/topic/147755-i-have-mysql-query-how-do-i-get-it-into-php/ Share on other sites More sharing options...
cooldude832 Posted March 3, 2009 Share Posted March 3, 2009 Add in a test for mysql_num_rows if it is 0 then the while part is automatically failing Also check you are reporting errors I've never seen a while loop closed with a ; Link to comment https://forums.phpfreaks.com/topic/147755-i-have-mysql-query-how-do-i-get-it-into-php/#findComment-775605 Share on other sites More sharing options...
John_S Posted March 3, 2009 Share Posted March 3, 2009 Hey there, I think it doesn't work since it has MySQL specific functions like: sum(duration) or substring(dstchannel,3,6). Also, why are you using the variable $connect twice? Plus there is a better way to do: <?php print "$dstchannel"; print "<br>"; print "$duration"; print "<br>"; ?> Using one and only display function, print "$dstchannel<br>$duration"; but I would suggest you to use "echos" instead of print, in my opinion echos' faster and plus they take multiple expressions. I changed your code a bit, hope it helps. <?php $connect = mysql_pconnect("localhost","user","pass") or Die("connect failed"); mysql_select_db("dbase") or Die("select failed"); $query = "SELECT dstchannel,sum(duration) from cdr where calldate like '2009-03-03%' group by substring(dstchannel,3,6)"; $res = mysql_query($query,$connect) or die("failed"); while($qry = mysql_fetch_array($res)) { $dstchannel=$qry["dstchannel"]; echo "$dstchannel<br>$duration"; }; ?> Link to comment https://forums.phpfreaks.com/topic/147755-i-have-mysql-query-how-do-i-get-it-into-php/#findComment-775608 Share on other sites More sharing options...
cooldude832 Posted March 3, 2009 Share Posted March 3, 2009 Hey there, I think it doesn't work since it has MySQL specific functions like: sum(duration) or substring(dstchannel,3,6). Also, why are you using the variable $connect twice? You can use mysql functions in a query... Mysql_query as a php function only facilitates passing your query phrase to mysql for turning into a resource of the queries result and doing any action in it (like SUM) Select is a mysql function from is a mysql function Where is a mysql function So to speak of Link to comment https://forums.phpfreaks.com/topic/147755-i-have-mysql-query-how-do-i-get-it-into-php/#findComment-775612 Share on other sites More sharing options...
sasa Posted March 3, 2009 Share Posted March 3, 2009 try <?php $connect = mysql_pconnect("localhost","user","pass") or Die("connect failed"); $selectdb = mysql_select_db("dbase") or Die("select failed"); $query = "SELECT dstchannel,sum(duration) from cdr where calldate like '2009-03-03%' group by substring(dstchannel,3,6)"; $res = mysql_query($query,$connect) or die("failed"); while($qry = mysql_fetch_array($res)) { $dstchannel=$qry["dstchannel"]; $duration = $qry['sum(duration)']; print "$dstchannel"; print "<br>"; print "$duration"; print "<hr>"; }; ?> Link to comment https://forums.phpfreaks.com/topic/147755-i-have-mysql-query-how-do-i-get-it-into-php/#findComment-775672 Share on other sites More sharing options...
seabro Posted March 5, 2009 Author Share Posted March 5, 2009 Thanks to all who replied. it's working now, I appreciate your help. Link to comment https://forums.phpfreaks.com/topic/147755-i-have-mysql-query-how-do-i-get-it-into-php/#findComment-777246 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.