Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Mysqli - How To Fetch An Object Or Array From Record, "select * From `table` Where `id`=?";

mysqli oop fetch database bind_param mysql array

  • Please log in to reply
7 replies to this topic

#1 Stasonix

Stasonix

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 115 posts
  • Programming Language:C++, PHP, JavaScript, Delphi/Object Pascal, Pascal
  • Learning:C++, PHP, JavaScript, Delphi/Object Pascal

Posted 09 May 2012 - 07:01 AM

public function UpdateMatches()
{
if ($result = $this->mysqli->prepare('SELECT * FROM `table` WHERE id=? LIMIT 1'))
{
$result->bind_param("i",$this->ID);
$result->execute();
// fetching all to object or associative array
while ($data = $result->fetch_assoc())
{
$obj[] = $data;
}
$result->close();
}

print_r($obj);
}

this is always returns me an error: Call to undefined method mysqli_stmt::fetch_assoc()
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 09 May 2012 - 07:07 AM

The variable $result is of type mysqli_stmt, not mysqli_result. The mysqli_stmt class doesn't have a method "fetch_assoc()" defined for it.

You can get a mysqli_result object from your mysqli_stmt object by calling its get_result() method.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 Stasonix

Stasonix

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 115 posts
  • Programming Language:C++, PHP, JavaScript, Delphi/Object Pascal, Pascal
  • Learning:C++, PHP, JavaScript, Delphi/Object Pascal

Posted 09 May 2012 - 07:10 AM

Thank you gregwarner.
So anyway, I need an example of how to do it.
  • 0

#4 Stasonix

Stasonix

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 115 posts
  • Programming Language:C++, PHP, JavaScript, Delphi/Object Pascal, Pascal
  • Learning:C++, PHP, JavaScript, Delphi/Object Pascal

Posted 09 May 2012 - 07:32 AM

Found myself:

public function UpdateMatches()
{
$query = "SELECT * FROM `mytable` WHERE id=$this->ID LIMIT 1";
if ($result = $this->mysqli->query($query))
{
$obj = $result->fetch_object();
}
var_dump($obj);
}

  • 0

#5 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 09 May 2012 - 07:37 AM

That method gets rid of your use of prepared statements. I would recommend you stick with prepared statements.

There are lots of examples on the PHP documentation pages:

http://www.php.net/m....get-result.php

Look at Example #1. Also, reference the class synopsis to learn how to use its methods. In your case, all you have to do is call the "get_result()" method on your statement object:

mysqli_stmt

{


...

/* Methods */

...

mysqli_result get_result ( void )

...

}



Like this:
if ($stmt = $this->mysqli->prepare('SELECT * FROM `table` WHERE id=? LIMIT 1'))
{
    $stmt->bind_param("i",$this->ID);
    $stmt->execute();

    // This line is what you were missing.
    $result = $stmt->get_result();

    // fetching all to object or associative array
    while ($data = $result->fetch_assoc())
    {
        ...
    }
    $result->close();
}

  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#6 Stasonix

Stasonix

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 115 posts
  • Programming Language:C++, PHP, JavaScript, Delphi/Object Pascal, Pascal
  • Learning:C++, PHP, JavaScript, Delphi/Object Pascal

Posted 09 May 2012 - 07:52 AM

To be honest I can't run a code:

public function UpdateMatches()
{

$stmt=$this->mysqli->stmt_init();
if ($stmt = $this->mysqli->prepare("SELECT * FROM `table` WHERE `id`=? LIMIT 1"))
{
$stmt->bind_param("i",$this->ID);
$stmt->execute();
$result = $stmt->get_result();
while ($data = $result->fetch_assoc())
{
$obj[]=$data;
}
}

var_dump($obj);
}

it is always Fatal Error: Call to undefined method mysqli_stmt::get_result()
But my post before works great. Yes, but bind_param seems still required.
  • 0

#7 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 09 May 2012 - 07:55 AM

What version of PHP are you running? mysqli_stmt::get_result() is only available for versions >= 5.3.0
http://www.php.net/m....get-result.php
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#8 Stasonix

Stasonix

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 115 posts
  • Programming Language:C++, PHP, JavaScript, Delphi/Object Pascal, Pascal
  • Learning:C++, PHP, JavaScript, Delphi/Object Pascal

Posted 09 May 2012 - 08:03 AM

PHP v.5.2.16.
I understand. Will updating.
  • 0





Also tagged with one or more of these keywords: mysqli, oop, fetch, database, bind_param, mysql, array

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download