2nd MySQL Dataset not working (1 Viewer)

vjmehra

Registered User.
Local time
Today, 03:54
Joined
Mar 17, 2003
Messages
57
I have a function:

Code:
//----------------------------------------------------------------------
$server_name='xxx';
$user_name='xxx';
$password='xxx';
$database_name='xxx';
$db1= new mysqli($server_name, $user_name, $password, $database_name);
//----------------------------------------------------------------------

function get_dataset($db1, $query_string) {
//---------------------------------------------------------------------
$result1 = $db1->query($query_string);
//$num_results = $result1->num_rows;

$num_results = $result1->num_rows;

$res_array = array();

for ($count=0; $row = $result1->fetch_assoc(); $count++) {
$res_array[$count]=$row;
}

return $res_array;


//$result8 = db_result_to_array($result1);
//return $result1;
$query_string->free();
$result1->free();
//$db1->close();
}
//---------------------------------------------------------------------

Now, this works fine, sort of....

On a separate page, I call the function:

Code:
$result=get_dataset($db1,"call sp_filter_issues_reduced_view(".$subsectorid.",".$bondticker.",".$currencyid.",".$countryid.")");

This works fine, however, I also have a few comboboxes I want to pull datasets into. This wasn't working, so just to test, I tried the following:

Code:
$result=get_dataset($db1,"call sp_filter_issues_reduced_view(".$subsectorid.",".$bondticker.",".$currencyid.",".$countryid.")");
$result2=get_dataset($db1,"call sp_filter_issues_reduced_view(".$subsectorid.",".$bondticker.",".$currencyid.",".$countryid.")");

So the 2nd, is simply a copy/paste of the first, yet it returns this error:

Fatal error: Call to a member function fetch_assoc() on a non-object in FILE PATH\functions.php on line 33

(line 33 being the line including fetch_assoc() in the function get_dataset, shown above).

Now, from this, I'm assuming somehow my dataset isn't clearing properly, or something like that. If I call the function once it works, twice it doesn't.

This is really bugging me and has been for days, so I'd really appreciate it, if anyone could help out!!!
 
Any ideas, anyone, I'm really stuck here, can't find anything on google that seems to suggest why this might be occurring :-(
 
Help!!! I'm still stuck on this, surely someone has loaded 2 datasets on a page before????
 
AFAIK, loading 2 data sets should not be a problem. Don't have the solution, all the same, just check out if u have not done the below.
1) have you checked the page source for the final page which throws up the error, to see if you can get any hint.
2) Have you tried putting in a few echo or print stmts at strategic locations inside the function & in your calling page to check exactly at which point the error takes place or whether your second $result2 is even passing the required parameters.

Edit : Be sure to put an echo statement after the Return statement in the function.

Thanks
 
Last edited:
The hint is this:

Fatal error: Call to a member function fetch_assoc() on a non-object in FILE PATH\functions.php on line 33

(line 33 being the line including fetch_assoc() in the function get_dataset, shown above).


Yet it only ever occurs on the 2nd dataset (note I'm using the same query for dataset 1 & 2, just to test it, so the parameters are definitely correct)...
 
Does an echo after the Return statement in the function print.

Thanks
 
Only of the first dataset, if I do a var_dump, the first one shows as expected, the second one (which is identical) shows nothing!
 
Basically, AFAIK, anything after the return statement in the Function should not execute.

Just as a trial, try the below & see what happens :
1) Move
PHP:
$query_string->free();
$result1->free();
above
PHP:
return $res_array;
inside the function,
and see what happens.

or
2)

Move
PHP:
$query_string->free();
$result1->free();
from the function,
after
PHP:
$result=get_dataset($db1,"call sp_filter_issues_reduced_view(".$subsectorid.",".$bondticker.",".$currencyid.",".$countryid.")");
and before
PHP:
$result2=get_dataset($db1,"call sp_filter_issues_reduced_view(".$subsectorid.",".$bondticker.",".$currencyid.",".$countryid.")");

Thanks

Edit : Am not in to PHP or MySQL currently, just dabbled in them long way back. What I am trying to say here is : perhaps we have not managed to set the recordset i.e. $result1 empty.
 
Ok great, I will try that and report back (will have to be later on today unfortunately as I can't get access to the code until the evening).
 
If I put in:

Code:
$query_string->free(); 
$result1->free();

I get:

Code:
Fatal error: Call to a member function free() on a non-object in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\Blue_Ocean\functions.php on line 67

(same error if I put it above return $res_array or on the other page).

Now, this would suggest to me that the query wasn't correct, however if I comment out the lines:

Code:
//$query_string->free(); 
//$result1->free();

and

Code:
//$result2=get_dataset($db1,"call sp_filter_issues_reduced_view(".$subsectorid.",".$bondticker.",".$currencyid.",".$countryid.")");

The data for:

Code:
$result=get_dataset($db1,"call sp_filter_issues_reduced_view(".$subsectorid.",".$bondticker.",".$currencyid.",".$countryid.")");

magically displays correctly!!!

This makes absolutely no sense to me!

:-(
 
Actually I just noticed that if uncomment the following:

Code:
$query_string->free(); 
$result1->free();

But leave the following commented out:

Code:
//$result2=get_dataset($db1,"call sp_filter_issues_reduced_view(".$subsectorid.",".$bondticker.",".$currencyid.",".$countryid.")");

then $result displays correctly, however as soon as I uncomment $result2, it doesn't work again!

By doesn't work, I mean I still get the following:

Code:
Fatal error: Call to a member function fetch_assoc() on a non-object in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\Blue_Ocean\functions.php on line 62
 
Decided to check out & wrote a couple of Test Pages.
It is working at my end.
Note : I was not able to replicate the exact situation at your end due to my limitations of coding in php.

All the same take a look.
A simple table employees with fields EmpID, EmpName, EmpType_ID

The 1st Page
z_TheFunction.php
PHP:
<?php
class employee 
{ 
    // get all employee count for specific employee type id
	function getEmpCount($empTypeID) {
		global $mysqli;
		
		$getEmpCount = "SELECT count(*) as empCount FROM employees WHERE EmpType_ID = '$empTypeID'";
		$resultgetEmpCount = $mysqli->query($getEmpCount);
		
	    if ($resultgetEmpCount) {
			return $resultgetEmpCount;
        }
        else {
            $this->error = $mysqli->error;
        }
	} 	
	
	//---------------------------------------------------------------------
	
	// get all employees for specific employee type id
	function getEmps($empTypeID) {
		global $mysqli;
		
		$qrygetEmps = "SELECT EmpID, EmpName, EmpType_ID FROM employees WHERE EmpType_ID = '$empTypeID' ORDER BY EmpName ";
		$resultgetEmps = $mysqli->query($qrygetEmps);
		
	    if ($resultgetEmps) {
			return $resultgetEmps;
        }
        else {
            $this->error = $mysqli->error;
        }
	}
	
	//---------------------------------------------------------------------	
	
}
?>

The second Final Page :
z_TheFinalPage
PHP:
<?php
	echo " Test Debug 1<br />";
	include("z_TheFunction.php");

	// --------------------------- the connection part begins -----------------------------------
	// the connection
	// set server access variables 
	$host = ""; 
	$user = ""; 

	$pass ="";
	$db = ""; 

	// create mysqli object 
	// open connection 
	$mysqli = new mysqli($host, $user, $pass, $db); 

	// open connection
	$connection = mysqli_connect($host, $user, $pass, $db) or die("Unable to connect");

	// --------------------------- the connection part ends -----------------------------------
	echo " Test Debug 2<br />";
	//$EmpTypeID = $_POST['EmpTypeID'];
	$EmpTypeID = 2;
	echo " Test Debug : The EmpTypeID". $EmpTypeID.".<br />";
	echo " Test Debug 3<br />";
	
	$memberscount = new employee($mysqli);
	$countresult = $memberscount->getEmpCount($EmpTypeID);

	$row = $countresult->fetch_object();
	$recordscount = $row->empCount; // The First result
	$recordscount1 = $row->empCount; // The Second result
	
	// Both the results albeit the same were printed
	echo "The Final No Of Emps 1: ".$recordscount." .<br />"; 
	echo "The Final No Of Emps 2: ".$recordscount." .<br />";
	
	
	$members = new employee($mysqli);

	// The 1st record set
	$result = $members->getEmps($EmpTypeID);

	// The 2nd record set
	$result1= $members->getEmps($EmpTypeID);

	// --------------------- Start of 1st Looping & displaying records -------------------
?>

	<form method="post">
		<table align="center" cellpadding="3">
	</form>

	<tr><th>Emp ID</th><th>Emp Name</th><th>EmpType_ID</th></tr>	
	<?php
	while ($row = $result->fetch_object()) {
		?>
		<form method="post">
		<?php
		echo "<tr><td>";
			echo "<input type='text' name='EmpID' value='".$row->EmpID."' readonly='readonly'></input>";
			echo "</td>";		
			echo "<td>";		
			echo "<input type='text' name='EmpName' value='".$row->EmpName."' readonly='readonly'></input>";
			echo "</td>";			
			echo "<td>";		
			echo "<input type='text' name='EmpName' value='".$row->EmpType_ID."' readonly='readonly'></input>";
		echo "</td>";		
		echo "</tr>";				
		?>
		</form>
		<?php
	}

	echo " Test Debug 4<br />";

	// --------------------- End of 1st Looping & displaying records -------------------
	
	// --------------------- Start of 2nd Looping & displaying records -------------------
	?>	
	<form method="post">
		<table align="center" cellpadding="3">
	</form>
			<tr><th>Emp ID</th><th>Emp Name</th><th>EmpType_ID</th></tr>	
		<?php
	while ($row1 = $result1->fetch_object()) {
		?>
		<form method="post">
		<?php
		echo "<tr><td>";
		echo "<input type='text' name='EmpID' value='".$row1->EmpID."' readonly='readonly'></input>";
		echo "</td>";		
		echo "<td>";		
		echo "<input type='text' name='EmpName' value='".$row1->EmpName."' readonly='readonly'></input>";
		echo "</td>";			
		echo "<td>";		
		echo "<input type='text' name='EmpName' value='".$row1->EmpType_ID."' readonly='readonly'></input>";
		echo "</td>";		
		echo "</tr>";				
		?>
		</form>
		<?php
	}
		echo " Test Debug 5<br />";
	// --------------------- End of 2nd Looping & displaying records -------------------
?>

Thanks
 
Apologies for the delayed response...

Unfortunately however, using your code, I get the following error:

Fatal error: Call to a member function fetch_object() on a non-object in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\Blue_Ocean\z_TheFinalPage.php on line 57

With line 57 being:

Code:
    while ($row = $result->fetch_object()) {

So it appears to be going wrong at the same place as my code!!!

Now I'm really stuck :-(
 
I really don't know, why those two pages are not working at your end.
They are working perfectly fine at my end.
Sorry, hope some one comes across this thread & shows us some light.
Have you tried posting on any php forums?

Thanks
 
Not yet, always on VB ones as that's all I've done before, I will try that and see if I can shed any light :-)

I'll post back here, if I find a solution!
 

Users who are viewing this thread

Back
Top Bottom