fantaaax33
New Member
I have a function that takes 2 variables, one for the database connection and the other for a query. For some reason, the mysqli link isn't happy unless it is inside the function; It isn't enough to just pass the link.Has anyone else had this happen and what can I do to avoid instantiating the db object inside the function.This does not work but if I put the object inside the function, it does.\[code\]function test($db, $query){ if($db->multi_query($query)){ do{ if($result = $db->store_result()){ while($row = $result->fetch_row()){ print_r($row); } $result->free_result(); $result->close(); } if($db->more_results()){ // print something here. } }while($db->next_result()); } $db->close();}\[/code\]Here is how I am calling them\[code\]$db = new mysqli('xxxx','xxxx','xxxx','xxxx');$q1 = ("SELECT * FROM table1");$q2 = ("SELECT * FROM table2");test($db,$q1);test($db,$q2);\[/code\]Without using the mysqli link inside the function, I will never see query #2 come back. The only thing I get are errors on the second query. (multi_query(): Couldn't fetch mysqli)\[code\]I just ran your code and here is what I get from the error log \[/code\][Fri Jan 08 08:12:43 2010] [error] [client 127.0.0.1] PHP Warning: mysqli::multi_query(): Couldn't fetch mysqli in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\1.php on line 24[Fri Jan 08 08:12:43 2010] [error] [client 127.0.0.1] PHP Warning: mysqli::close(): Couldn't fetch mysqli in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\1.php on line 38 [Fri Jan 08 08:12:43 2010][error] [client 127.0.0.1] PHP Warning: mysqli::multi_query(): Couldn't fetch mysqli in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\1.php on line 24 [Fri Jan 08 08:12:43 2010][error] [client 127.0.0.1] PHP Warning: mysqli::close(): Couldn't fetch mysqli in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\1.php on line 38 [Fri Jan 08 08:12:43 2010][error] [client 127.0.0.1] PHP Warning: mysqli::multi_query(): Couldn't fetch mysqli in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\1.php on line 24 [Fri Jan 08 08:12:43 2010][error] [client 127.0.0.1] PHP Warning: mysqli::close(): Couldn't fetch mysqli in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\1.php on line 38 [Fri Jan 08 08:12:43 2010][error] [client 127.0.0.1] PHP Warning: mysqli::multi_query(): Couldn't fetch mysqli in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\1.php on line 24 [Fri Jan 08 08:12:43 2010][error] [client 127.0.0.1] PHP Warning: mysqli::close(): Couldn't fetch mysqli in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\1.php on line 38Here is what I am running now but still only returns a single query which happens to be the first one listed 'Notes'\[code\]function query_db( $link, $query ) { /* execute multi query */ if ( $link->multi_query( $query ) ) { do { /* store first result set */ if ($result = $link->store_result() ) { while( $row = $result->fetch_row() ) { print_r( $row ); } echo '<br/>'; $result->close(); } if( $link->more_results() ) { // ... } } while ( $link->next_result() ); }}$db = new mysqli( 'xxxx', 'xxxx', 'xxxx', 'xxxx' );query_db( $db, 'CALL Notes();'query_db( $db, 'CALL Users();'query_db( $db, 'SELECT * FROM test';\[/code\]$db = new mysqli('xxxx','xxxx','xxxx','xxxx');\[code\]$f1 = fa($db,"CALL phones();");$f2 = fa($db,"CALL users();");\[/code\]OK, very simply.. We query the database 2x. Once to get all phones then assign the return set to var $f1. We do the same thing for $f2. When I run this code, $f2 never has a result. it's always empty. However, when I do a print_r on each call independently, I get the right results. This is really weird! You should be able to replicate this very same behavior with the code we have posted below. Just try and assign the returned set to each variable, then try and echo the results.