Skip to main content
Participant
March 17, 2010
Question

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given

  • March 17, 2010
  • 1 reply
  • 2616 views

Hi all,

I need help with this error that always appers when I add a dynamic table to my search results page. The search DOES produce the correct results, strangely. I search the database by name.

Dreamweaver cs3, xampp, PHP, mySQL     install dir: C:\xampp\htdocs

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in C:\xampp\htdocs\php\homeresults.php on line 47


The strange thing is that results actually get produced so it DOES work but, this error keeps getting thrown and i have to remove that line from my code to get rid of the error, but then i get a red exclamtion point on the repeat region server behavour and i want to solve this...

my server behavour is listed like this  ! Repeat Region (@@RecordsetName@@) 

line 47, the error line is in red below

<table border="0">
  <tr>
    <td>id</td>
    <td>firstname</td>
    <td>lastname</td>
    <td>address</td>
    <td>city</td>
    <td>state</td>
    <td>zip</td>
    <td>homephone</td>
    <td>cellphone</td>
    <td>businessphone</td>
    <td>product</td>
    <td>comments</td>
    <td>deposit</td>
    <td>balance</td>
    <td>total</td>
    <td>installdate</td>
    <td>invoice</td>
    <td>curdate</td>
    <td>jobsite</td>
    <td>source</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><?php echo $row_Recordset1['id']; ?></td>
      <td><?php echo $row_Recordset1['firstname']; ?></td>
      <td><?php echo $row_Recordset1['lastname']; ?></td>
      <td><?php echo $row_Recordset1['address']; ?></td>
      <td><?php echo $row_Recordset1['city']; ?></td>
      <td><?php echo $row_Recordset1['state']; ?></td>
      <td><?php echo $row_Recordset1['zip']; ?></td>
      <td><?php echo $row_Recordset1['homephone']; ?></td>
      <td><?php echo $row_Recordset1['cellphone']; ?></td>
      <td><?php echo $row_Recordset1['businessphone']; ?></td>
      <td><?php echo $row_Recordset1['product']; ?></td>
      <td><?php echo $row_Recordset1['comments']; ?></td>
      <td><?php echo $row_Recordset1['deposit']; ?></td>
      <td><?php echo $row_Recordset1['balance']; ?></td>
      <td><?php echo $row_Recordset1['total']; ?></td>
      <td><?php echo $row_Recordset1['installdate']; ?></td>
      <td><?php echo $row_Recordset1['invoice']; ?></td>
      <td><?php echo $row_Recordset1['curdate']; ?></td>
      <td><?php echo $row_Recordset1['jobsite']; ?></td>
      <td><?php echo $row_Recordset1['source']; ?></td>
    </tr>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
<?php require_once('Connections/localhost.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

$colname_Recordset1 = "-1";
if (isset($_GET['homephone'])) {
  $colname_Recordset1 = $_GET['homephone'];
}
mysql_select_db($database_localhost, $localhost);
$query_Recordset1 = sprintf("SELECT * FROM invoice WHERE homephone = %s ORDER BY homephone ASC", GetSQLValueString($colname_Recordset1, "text"));
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $localhost) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysql_query($query_Recordset1);
  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

mysql_free_result($Recordset1);
?>

TIA!!!

This topic has been closed for replies.

1 reply

David_Powers
Inspiring
March 20, 2010

You've got the cart before the horse. The PHP code that creates the recordset needs to come before the HTML/PHP that attempts to use it.