Solved

Fatal error: Call to a member function fetch_array() on a non-object

Posted on 2016-07-16
12
65 Views
Last Modified: 2016-07-16
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
0
Comment
Question by:breeze351
  • 6
  • 5
12 Comments
 

Author Comment

by:breeze351
ID: 41714705
I forgot to add that I checked the 3rd sql statement and it does work.
0
 
LVL 21

Expert Comment

by:Kim Walker
ID: 41714723
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

0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 41714726
The usual reason is that the query failed.
0
 

Author Comment

by:breeze351
ID: 41714731
Kim
How do I turn on the error reporting
0
 

Author Comment

by:breeze351
ID: 41714756
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!
0
 
LVL 21

Expert Comment

by:Kim Walker
ID: 41714774
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

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:breeze351
ID: 41714790
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
0
 
LVL 21

Expert Comment

by:Kim Walker
ID: 41714805
Is the fatal error still on the inner while loop (line 61)?
0
 
LVL 21

Expert Comment

by:Kim Walker
ID: 41714806
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

0
 

Author Comment

by:breeze351
ID: 41714824
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? :)
0
 
LVL 21

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 41714853
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.
0
 

Author Closing Comment

by:breeze351
ID: 41714873
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
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now