Jump to content

I have MySQL Query, how do I get it into PHP?


seabro

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.