next Posted July 24, 2008 Share Posted July 24, 2008 1) So i couldn't deal with PDO errors anymore and decided to learn MySQLi. I can't get queries to mork, here is my code: <?php include('config/config.php'); $dbh = new mysqli(SERVER, DB_USER, DB_PASSWORD, DB); $query = "CALL get_forums('general');"; $result = $dbh->query($query); while($row = $result->fetch_object()) echo $row->forum_title . "<br />"; $query = "CALL count_threads()"; $result = $dbh->query($query); while($row = $result->fetch_object()) echo $row->num_threads . "<br />"; ?> I the above first query executes with no problems, but second one results in an error: Fatal error: Call to a member function fetch_object() on a non-object in E:\PortableApps\WOS\www\MovieTalk\test.php on line 14 line 14: while($row = $result->fetch_object()) How do i get this to work? 2) Second query: $query = "CALL count_threads()"; $result = $dbh->query($query); while($row = $result->fetch_object()) echo $row->num_threads . "<br />"; returns a single integer, is there a better way of retrieving it, without having to use a loop? Quote Link to comment https://forums.phpfreaks.com/topic/116445-solved-mysqli-queries-a-problem-and-a-question/ Share on other sites More sharing options...
wildteen88 Posted July 24, 2008 Share Posted July 24, 2008 I the above first query executes with no problems, but second one results in an error: Fatal error: Call to a member function fetch_object() on a non-object in E:\PortableApps\WOS\www\MovieTalk\test.php on line 14 line 14: while($row = $result->fetch_object()) How do i get this to work? You most probably have an error in your query then. Change $query = "CALL count_threads()"; $result = $dbh->query($query); to $query = "CALL count_threads()"; $result = $dbh->query($query) or die($dbh->error); 2) Second query: $query = "CALL count_threads()"; $result = $dbh->query($query); while($row = $result->fetch_object()) echo $row->num_threads . "<br />"; returns a single integer, is there a better way of retrieving it, without having to use a loop? If you know your query is always going to return one result then don't use a loop at all. $row = $result->fetch_object(); echo $row->num_threads . "<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/116445-solved-mysqli-queries-a-problem-and-a-question/#findComment-598815 Share on other sites More sharing options...
next Posted July 24, 2008 Author Share Posted July 24, 2008 My query is fine, otherwise i wouldn't be able to make a procedure out of it. I added those lines, here's what i got: Commands out of sync; you can't run this command now How can i fix this? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/116445-solved-mysqli-queries-a-problem-and-a-question/#findComment-598820 Share on other sites More sharing options...
wildteen88 Posted July 24, 2008 Share Posted July 24, 2008 Use $result->free between each use of the stored procedure Quote Link to comment https://forums.phpfreaks.com/topic/116445-solved-mysqli-queries-a-problem-and-a-question/#findComment-598841 Share on other sites More sharing options...
next Posted July 24, 2008 Author Share Posted July 24, 2008 I tried: <?php include('config/config.php'); $dbh = new mysqli(SERVER, DB_USER, DB_PASSWORD, DB); $query = "CALL get_forums('general');"; $result = $dbh->query($query); while($row = $result->fetch_object()) echo $row->forum_title . "<br />"; $result->free(); $query = "CALL count_threads()"; $result = $dbh->query($query) or die($dbh->error); echo $result->fetch_object()->num_threads; ?> but was unsuccessful. I also tried replacing "$result->free();" with "$result->free_result();". Am i doing it wrong? Quote Link to comment https://forums.phpfreaks.com/topic/116445-solved-mysqli-queries-a-problem-and-a-question/#findComment-598843 Share on other sites More sharing options...
wildteen88 Posted July 24, 2008 Share Posted July 24, 2008 Have a read of the following article here specifically the last paragraph in the Executing Stored Procedures section. Quote Link to comment https://forums.phpfreaks.com/topic/116445-solved-mysqli-queries-a-problem-and-a-question/#findComment-598851 Share on other sites More sharing options...
next Posted July 24, 2008 Author Share Posted July 24, 2008 Thanks for your help, I'll have a look. Quote Link to comment https://forums.phpfreaks.com/topic/116445-solved-mysqli-queries-a-problem-and-a-question/#findComment-598861 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.