FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » determine a mysqli_result object
Show: Today's Messages :: Unread Messages :: Show Polls :: Message Navigator
| Subscribe to topic | Bookmark topic 
Switch to threaded view of this topic Create a new topic Submit Reply
determine a mysqli_result object [message #181268] Sat, 27 April 2013 19:11 Go to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
I have a class that I am converting from mysql to mysqli in PHP 5.2.x

The problem I ran into is this.

Previously I could check the mysql_query if it return a resource by using:

is_resource($resource)

and could parse the resource using mysql_fetch_assoc or similar

and if it was an insert, update, etc.. it would not return a resource
and I would just return a true and carry on.

Now with mysqli it return a mysql_result object.

How do I check for this so I know whether to parse or return

Thanks

Here is a nutshell of what was using for mysql

// Already connected to mysqli

public function query($query){
$resource = $this->connection->query($query);

if($resource) {

if(is_resource($resource)) { // parse

$data = array();

$idx = 0;

while($result = $this->connection->fetch_assoc()) {

$data[$idx] = $result;
// Object assembly

}
} else { // return

// update, insert, etc..
return true;

}
} else {
// error
}
}

Scotty
Re: determine a mysqli_result object [message #181269 is a reply to message #181268] Sat, 27 April 2013 19:18 Go to previous message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 4/27/2013 4:11 PM, Scott Johnson wrote:
> I have a class that I am converting from mysql to mysqli in PHP 5.2.x
>
> The problem I ran into is this.
>
> Previously I could check the mysql_query if it return a resource by using:
>
> is_resource($resource)
>
> and could parse the resource using mysql_fetch_assoc or similar
>
> and if it was an insert, update, etc.. it would not return a resource
> and I would just return a true and carry on.
>
> Now with mysqli it return a mysql_result object.
>
> How do I check for this so I know whether to parse or return
>
> Thanks
>
> Here is a nutshell of what was using for mysql
>
> // Already connected to mysqli
>
> public function query($query){
> $resource = $this->connection->query($query);
>
> if($resource) {
>
> if(is_resource($resource)) { // parse
>
> $data = array();
>
> $idx = 0;
>
> while($result = $this->connection->fetch_assoc()) {
>
> $data[$idx] = $result;
> // Object assembly
>
> }
> } else { // return
>
> // update, insert, etc..
> return true;
>
> }
> } else {
> // error
> }
> }
>
> Scotty


Crap figured it out. Was binging for the wrong answer.

if($result_obj) {
if($result_obj instanceof mysqli_result) {
echo 'good';
$data = array();
$idx = 0;
while($result = $result_obj->fetch_assoc()) {
$data[$idx] = $result;
$idx++;
}
print_r($data);
} else {
return true;
}
}

Scotty
Quick Reply
Formatting Tools:   
  Switch to threaded view of this topic Create a new topic
Previous Topic: Undefined variable
Next Topic: Got a chuckle and wanted to share.
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Dec 12 05:30:34 EST 2017

Total time taken to generate the page: 0.00570 seconds