Jump to content

Recommended Posts

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?

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 />";

 

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.