Why would the following code produce the above error the 3rd itteration?

while ($row_comp = $comp->fetch_array())
{
      $comp_count++;
      $comp_id = $row_comp['TID'];            
      $SqlString = "select * from pers where TID = \"$comp_id\"";
      echo "str = ".$SqlString."<br>";
      $pers = $conn->query($SqlString);
      while ($row_pers = $pers->fetch_array())
      {

The screen output looks like this:
str = select * from pers where TID = "10019706"
str = select * from pers where TID = "10019771"
str = select * from pers where TID = "10019779"

Fatal error: Call to a member function fetch_array() on a non-object in /home/langsyst/public_html/Lansco/Fix_Data_2A.php on line 58

Line 58 is the 2nd while
breeze351
Asked: 
breeze351
1 Solution
breeze351Author Commented: 
I forgot to add that I checked the 3rd sql statement and it does work.
Kim WalkerWeb Programmer/TechnicianCommented: 
The error means that $pers does not contain a query result possibly due to a problem with the db connection $conn. This could be a scope issue. Is the code you posted part of a function and if so, is the variable $conn defined within that same function?

Otherwise, we have another error to deal with and we need to determine what that is. Do you have error reporting turned on? If it is turned on and you're not seeing an error, please replace your code with the following and take note of the additions I've added for exposing the error. My first change is on line 7 and then I've added code after the end of the second while loop.
while ($row_comp = $comp->fetch_array())
{
      $comp_count++;
      $comp_id = $row_comp['TID'];            
      $SqlString = "select * from pers where TID = \"$comp_id\"";
      echo "str = ".$SqlString."<br>";
      if ($pers = $conn->query($SqlString) ) {
	      while ($row_pers = $pers->fetch_array()) {
	      	...
	      }
	  } else {
	  	if ($conn->error) {
	  		echo "{$conn->error}<br>SQL = $SqlString";
	  	} else {
	  		echo '$conn is a(n) '. gettype($conn);
	  	}
	  }
}

 Open in new window

Dave BaldwinFixer of ProblemsCommented: 
The usual reason is that the query failed.
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

breeze351Author Commented: 
Kim
How do I turn on the error reporting
breeze351Author Commented: 
Kim 
This is weird?!?!?!
I modified the code per your example.  I don't get an error message, however, if I keep refreshing the program, it will give me one more record before it craps out!
Kim WalkerWeb Programmer/TechnicianCommented: 
OK. This suggests that your first query is returning a TID value that doesn't exist in the pers table. In other words, the outer loop doesn't always have the right conditions for the inner loop.

To turn on error reporting, place these two lines at the beginning of your PHP page.
error_reporting(E_ALL);
ini_set('display_errors','on');

 Open in new window

breeze351Author Commented: 
I still don't get an error message other the "Fatal error....."
And again, it keeps giving me one more record before it fails.

I've attached the code.  It's small, I'm just trying to clean up some garbage data.
Thought this would be a piece of cake, after all how hard could it be!
Fix_Data_2A.php
Kim WalkerWeb Programmer/TechnicianCommented: 
Is the fatal error still on the inner while loop (line 61)?
Kim WalkerWeb Programmer/TechnicianCommented: 
One more modification (see lines 13-14, 22-24):
$comp_count = 0;
$per_count = 0;		
$SqlString = "select * from comp where ALFA = \"    \" and COMPANY = \"    \"";
$comp = $conn->query($SqlString);
while ($row_comp = $comp->fetch_array())
{
	$comp_count++;
	$comp_id = $row_comp['TID'];		
	$SqlString = "select * from pers where TID = \"$comp_id\"";
	echo "str = ".$SqlString."<br>";
	if ($pers = $conn->query($SqlString))
	{
		if ($pers->num_rows > 0)
		{
			while ($row_pers = $pers->fetch_array())
			{
				$pers_id = $row_pers['PNID'];
				$SqlString = "delete from pers where TID = '$comp_id' and PNID = '$pers_id'";
				$pers = $conn->query($SqlString);
				$per_count++;
			}
		} else {
			echo "<p>No records found for query $SqlString</p>\n";
		}
	}
	else 
	{
  		if ($conn->error) 
		{
  			echo "{$conn->error}<br>SQL = $SqlString";
	  	} 
		else 
		{
  			echo '$conn is a(n) '. gettype($conn);
  		}
	}
}

 Open in new window

breeze351Author Commented: 
No joy!
I didn't think it would work because it keeps blowing up on the while statement.
Try this yourself (I'm not worried about the data, I have backups and it's really not current).

Go to:
1: http://langsystems.net/Lansco/
(you need to do this for the session vars)
2: http://langsystems.net/Lansco/Fix_Data_2A.php

Look at the last "TID"# in the query, then refresh the page.  You'll get the next record.

There are only 234 records that match the 1st while loop.  Do I have to hit refresh 234 times? :)
Kim WalkerWeb Programmer/TechnicianCommented: 
AHAH! So the while loop iterates once then fails. It is because the while loop is attempting to read another record from the $pers result but the $pers result is being overwritten within the while loop. Change this
				$SqlString = "delete from pers where TID = '$comp_id' and PNID = '$pers_id'";
				$pers = $conn->query($SqlString);

 Open in new window

to
				$SqlString = "delete from pers where TID = '$comp_id' and PNID = '$pers_id'";
				$conn->query($SqlString);

 Open in new window

There is no need to store the results of the delete into a variable unless you need to view results such as affected_rows. If you still want to store those results, store them in a new variable instead of reusing $pers.
breeze351Author Commented: 
Thank you, Thank you!!!!!!!!!!!!!!

I just copied the $conn->query statement from code up above.  I knew how to delete the record but sometimes you can't see something so stupid.

Thank you again.
Glenn


'WEB > jQuery' 카테고리의 다른 글

PHP array_push  (0) 2018.01.14
Fatal error: Call to a member function query() on null  (0) 2018.01.14
달력 - 날짜입력기(Date Picker)  (0) 2018.01.14
javascript comma and uncomma  (0) 2018.01.14
[jQuery] following sidebar  (0) 2018.01.14

+ Recent posts