0

First of all, this is my first meeting with MySQLi... I heard that MySQLi is better, but every time I wrote some code, I get

Fatal error: Call to a member function bind_param() on a non-object

My code is this:

<?php
/* Create a new mysqli object with database connection parameters */
$m = new mysqli('localhost', 'root', '', 'mysqlisample');
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
$ida=1;
$statement = $m->prepare("SELECT * FROM post WHERE `id` =  ?");
$statement->bind_param("i",$ida);
$id = 0;
$post_title = '';
$post_content = '';
$statement->bind_result($id,$post_title,$post_content);
$statement->execute();
while ($statement->fetch()){
echo $id.' '.$post_title.' '.$post_content.'\n'; //These variables will get the values of the current row
}
?>

This is just one of many code sample that I read somewhere, but, none of them working.

What is the right way for executing MySQLi query and print the results?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Aleksandar
  • 644
  • 1
  • 11
  • 29
  • 1
    Use [PDO](http://php.net/manual/en/book.pdo.php). – PenguinCoder Feb 05 '13 at 14:47
  • PDO is harder to understand, and I am beginer... – Aleksandar Feb 05 '13 at 14:50
  • See ["Looking for a good php mysqli class tutorial"](http://stackoverflow.com/questions/750301/looking-for-a-good-php-mysqli-class-tutorial) for a short list of possibly helpful tutorials. Google is also your friend. – Jonah Bishop Feb 05 '13 at 14:50
  • @JonahBishop I know that Google is my friend, and I try to find good tutorial or example for 3 days, but i can't find any that can solve my issue – Aleksandar Feb 05 '13 at 15:00

2 Answers2

1

i worked this out when going through the OReilly book, which was using the old mysql_stuff and none of the examples worked. obviously you'll need to modify it for your tables :) but it works for the tables i have. this will work for prepared statements:

<?php

//this file is just where my db info is, you can use the literal values
require 'login.php';

$db = new mysqli($db_hostname, $db_username, $db_password, $db_database);
$stmt = $db->stmt_init();
$data = array("Emily Bronte", "Wuthering Heights", "Classic Fiction", "1847", "9780553212587");

if($stmt->prepare("INSERT INTO classics(author, title, category, year, isbn) VALUES(?,?,?,?,?)"))
{
    $stmt->bind_param('sssss', $data[0], $data[1], $data[2], $data[3], $data[4]);
    $stmt->execute();
    $stmt->close();
}

?>

this will work for queries:

<?php
require_once 'login.php';
$dbt = new mysqli($db_hostname, $db_username, $db_password, $db_database);
if ($dbt->connect_errno) 
    die("Unable to connect to MySQL: " . $dbt->connect_errno);

$results = $dbt->query("SELECT * FROM cats");

if (!$results) 
    die ("Database access failed: " . $db->error);

$dbt->close();

echo "<table><tr> <th>Id</th> <th>Family</th>
<th>Name</th><th>Age</th></tr>";

for ($j = 0 ; $j < $results->num_rows ; ++$j)
{
    $row = $results->fetch_row();
    echo "<tr>";
    for ($k = 0 ; $k < sizeof($row) ; ++$k) 
        echo "<td>$row[$k]</td>";
    echo "</tr>";
}
echo "</table>";

?>
Jeff Hawthorne
  • 568
  • 2
  • 12
0

The problem is that $statement->bind_param("i",$ida); returns false, so you can't call the method bind_param on false

See: http://php.net/manual/de/mysqli.prepare.php

Try:

mysqli_stmt_bind_param($statement, "i", $ida);

instead of:

$statement->bind_param("i",$ida);
bpoiss
  • 13,673
  • 3
  • 35
  • 49
  • Now I have `Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in...` and `Fatal error: Call to a member function bind_result() on a non-object in...` – Aleksandar Feb 05 '13 at 14:56
  • 1
    Try remove `` from `id` in `$statement = $m->prepare("SELECT * FROM post WHERE id=?");` (id=?, no spaces, no ``) – bpoiss Feb 05 '13 at 15:09
  • table post exists (consider case sensitive)? – bpoiss Feb 05 '13 at 15:22
  • I am so sorry! My mistake,your comment is good, but I just copy this from your comment and I get error.In your query, the table is post but, in my question is posts. This is right answer,thanks a lot!!! And thanks to @BernhardPoiss for noticing! – Aleksandar Feb 05 '13 at 15:39