Jump to content

[SOLVED] PHP error... please assist


smarthouseguy

Recommended Posts

I am getting the following error, not sure what I need to do to fix this...
[quote]Warning: mysql_fetch_array(): 6 is not a valid MySQL result resource in /usr/local/apache2/htdocs/homepbx/kevin/home/cdr.php on line 23[/quote]

[code]
<?php
$connection=mysql_connect("host","db","pass");
if (!$connection) {
echo "Could not connect to MySql server!";
exit;
}

$db=mysql_select_db("asterisk",$connection);
if (!$db) {
echo "Could not select database";
exit;
}

$sql = 'SELECT * FROM `table`';

$mysql_result=mysql_query($sql,$connection);
$num_rows=mysql_num_rows($mysql_result);
if ($num_rows == 0) {
echo "Sorry, we have no records";
} else {
echo "<div align=\"center\"><table summary=\"HomePBX Call Detail Records\" cellspacing=\"0\" cellpadding=\"0\"><caption>$name's Call Detail Records</caption>";
echo "<tr><th class=\"title\">Call Date</th><th>Caller ID</th><th>Source</th><th>Destination</th><th>Duration</th></tr>";
while ($row=mysql_fetch_array($mysql_result))
{
$result=$mysql_result;
$id=$row["calldate"];
$var_1=$row["clid"];
$var_2=$row["src"];
$var_3=$row["dst"];
$var_4=$row["duration"];
$i = 0;
while (($row=mysql_fetch_row($result)) !== false) {
$i++;
echo "<tr class=\"d".($i & 1)."\">";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>\n";
}
mysql_free_result($result);
}
}
mysql_close($connection);
?>
</table></div>
[/code]

is this because of double while statements???

if I try to remove the [quote]while ($row=mysql_fetch_array($mysql_result)) {[/quote] I no longer get my data...

all I am trying to accomplish with this right now is that the rows alternate color.. this is working, except for the error....

Any help will be greatly appreciated.

Kind Regards,
Smarthouseguy
Link to comment
Share on other sites

I am a newbie to php... loving it so far.. this is what I did and it seems to work... if this is not correct please let me know... :-)

[code]<?php
$connection=mysql_connect("host","db","pass");
if (!$connection) {
echo "Could not connect to MySql server!";
exit;
}

$db=mysql_select_db("asterisk",$connection);
if (!$db) {
echo "Could not select database";
exit;
}

$sql = 'SELECT * FROM `table`';

$mysql_result=mysql_query($sql,$connection);
$num_rows=mysql_num_rows($mysql_result);
if ($num_rows == 0) {
echo "Sorry, we have no records";
} else {
echo "<div align=\"center\"><table summary=\"HomePBX Call Detail Records\" cellspacing=\"0\" cellpadding=\"0\"><caption>$name's Call Detail Records</caption>";
echo "<tr><th class=\"title\">Call Date</th><th>Caller ID</th><th>Source</th><th>Destination</th><th>Duration</th></tr>";
if ($row=mysql_fetch_array($mysql_result))
{
$result=$mysql_result;
$id=$row["calldate"];
$var_1=$row["clid"];
$var_2=$row["src"];
$var_3=$row["dst"];
$var_4=$row["duration"];
$i = 0;
while (($row=mysql_fetch_row($result)) !== false) {
$i++;
echo "<tr class=\"d".($i & 1)."\">";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>\n";
}
mysql_free_result($result);
}
}
mysql_close($connection);
?>
</table></div>
</body>
</html>[/code]

it seems like changing the first while loop to an if statement works... my next steps for this are to:
[quote]1) get $row[4] to divide by 60 so I can take secs and make them mins
2) get $row[4] to sum for total mins and display above the table
3) add sort capabilities to the table data
4) have it split the data and make a new page after say 12 records and maybe an option to control how many records can be displayed
5) have export capabilites that will:
  a) export data from the records with a date range
  b) a php script that will auto bill by every 30 days exporting something like a monthly bill....
      i)email the results of the monthly bill in a pdf or some other nice format
      ii)add history links to previous month's data to the call detail record page
[/quote]
any ideas or direction will keep my frustration levels to a minimum...

Kind regards
Link to comment
Share on other sites

Using the IF statement for the mysql_fetch_array command will pull the whole array of information, whereas using a while loop, will iterate through each row of the results. Using the while loop with the fetch_array, means there is no need for the fetch_row command. I would use:

[code]
<?php

$i = 0;
while ($row=mysql_fetch_array($mysql_result))
{
$i++;
$id=$row["calldate"];
$clid=$row["clid"];
$src=$row["src"];
$dst=$row["dst"];
$duration=$row["duration"];

echo "<tr class=\"d".($i & 1)."\">";
echo "<td>".$id."</td>";
echo "<td>".$clid."</td>";
echo "<td>".$src."</td>";
echo "<td>".$dst."</td>";
echo "<td>".$duration."</td>";
echo "</tr>\n";
}
?>
[/code]
Link to comment
Share on other sites

  • 3 weeks later...
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.