Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

2nd MySQL Dataset not working

Codes here !

Moderators: macek, egami, gesf

2nd MySQL Dataset not working

Postby vjmehra » Tue Mar 19, 2013 4:14 am

I have a function:




Code: Select all
//----------------------------------------------------------------------
$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: Select all

$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: Select all

$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 weeks now, so I'd really appreciate it, if anyone could help out!!!
vjmehra
New php-forum User
New php-forum User
 
Posts: 32
Joined: Mon Mar 05, 2012 12:07 pm

Re: 2nd MySQL Dataset not working

Postby vjmehra » Wed Mar 20, 2013 10:18 am

If it helps, its definitely to do with:

Code: Select all
$row = $result1->fetch_assoc();


It only ever creates an issue if there are 2 datasets (i.e. if I comment one out, the page loads correctly).

People on here must use multiple datasets all the time, so this must be a really simple fix for someone out there!

Please help, I've been stuck on this for weeks :-(
vjmehra
New php-forum User
New php-forum User
 
Posts: 32
Joined: Mon Mar 05, 2012 12:07 pm

Re: 2nd MySQL Dataset not working

Postby johnj » Thu Mar 21, 2013 10:44 pm

The problem is with the variable called $result1. This means that the query did not run successfully or it returned false. You need to see if $db1 still has a value when ever it throws this error.
Good to rewrite
Code: Select all
$result1 = $db1->query($query_string);
as
Code: Select all
if ($result1 = $db1->query($query_string))
{
// place the remaining relevant code here
}
else
{
    die("Error while executing query");
}
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron