Jump to content

[SOLVED] mysqli queries a problem and a question.


next

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.