Fatal error: Call to a member function bind_param()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AliJ
Forum Commoner
Posts: 32
Joined: Sun Dec 06, 2009 8:03 pm

Fatal error: Call to a member function bind_param()

Post by AliJ »

Hi,
I'm having problem with the file 'index.php' and its tells me "Fatal error: Call to a member function bind_param() on a non-object in /home/alij/public_html/index.php on line 62" and here is the code that I'm having the problem with:

Code: Select all

<?php
 
require('includes/header.php');
 
// Should we sort by topic creation date or last bump?
if($_COOKIE['topics_mode'] == 1 && ! $_GET['bumps'])
{
    $topics_mode = true;
}
 
// Are we on the first page?
if ($_GET['p'] < 2 || ! ctype_digit($_GET['p'])) 
{
    $current_page = 1;
    
    if($topics_mode)
    {
        update_activity('latest_topics');
        $page_title = 'Latest topics';
        $last_seen = $_COOKIE['last_topic'];
    }
    else
    {
        update_activity('latest_bumps');
        $page_title = 'Latest bumps';
        $last_seen = $_COOKIE['last_bump'];
    }
}
// The page number is greater than one.
else 
{
    $current_page = $_GET['p'];
    update_activity('topics', $current_page);
    $page_title = 'Topics, page #' . number_format($current_page);
}
 
// Update the last_bump and last_topic cookies. These control
// both the last seen marker and the exclamation mark in main menu.
if($_COOKIE['last_bump'] <= $last_actions['last_bump']) 
{
    setcookie('last_bump', $_SERVER['REQUEST_TIME'], $_SERVER['REQUEST_TIME'] + 315569260, '/');
}
if($_COOKIE['last_topic'] <= $last_actions['last_topic'])
{
    setcookie('last_topic', $_SERVER['REQUEST_TIME'], $_SERVER['REQUEST_TIME'] + 315569260, '/');
}
 
// If ostrich mode is enabled, fetch a list of blacklisted phrases.
$ignored_phrases = fetch_ignore_list();
 
// Fetch the topics appropriate to this page.
$items_per_page = ITEMS_PER_PAGE;
$start_listing_at = $items_per_page * ($current_page - 1);
if($topics_mode)
{
    $stmt = $link->prepare('SELECT id, time, replies, visits, headline, body, last_post FROM topics ORDER BY id DESC LIMIT ?, ?');
}
else
{
    $stmt = $link->prepare('SELECT id, time, replies, visits, headline, body, last_post FROM topics ORDER BY last_post DESC LIMIT ?, ?');
}
$stmt->bind_param('ii', $start_listing_at, $items_per_page);
$stmt->execute();
$stmt->bind_result($topic_id, $topic_time, $topic_replies, $topic_visits, $topic_headline, $topic_body, $topic_last_post);
 
// Print the topics we just fetched in a table.
$table = new table();
 
$order_name = ($topics_mode) ? 'Age' : 'Last bump';
$columns = array
(
    'Headline',
    'Snippet',
    'Replies',
    'Visits',
    $order_name . ' ?'
);
            
if($_COOKIE['spoiler_mode'] != 1)
{
    // If spoiler mode is disabled, remove the snippet column.  
    array_splice($columns, 1, 1);
}
 
$table->define_columns($columns, 'Headline');
$table->add_td_class('Headline', 'topic_headline');
$table->add_td_class('Snippet', 'snippet');
 
while($stmt->fetch()) 
{
    // Should we even bother?
    if($_COOKIE['ostrich_mode'] == 1)
    {
        foreach($ignored_phrases as $ignored_phrase)
        {
            if(stripos($topic_headline, $ignored_phrase) !== false || stripos($topic_body, $ignored_phrase) !== false)
            {
                // We've encountered an ignored phrase, so skip the rest of this while() iteration.
                $table->num_rows_fetched++;
                continue 2;
            }
        }
    }
    
    // Decide what to use for the last seen marker and the age/last bump column.
    if($topics_mode)
    {
        $order_time = $topic_time;
    }
    else
    {
        $order_time = $topic_last_post;
    }
    
    // Process the values for this row of our table. 
    $values = array (
                        '<a href="/topic/' . $topic_id . '">' . htmlspecialchars($topic_headline) . '</a>',
                        snippet($topic_body),
                        replies($topic_id, $topic_replies),
                        format_number($topic_visits),
                        '<span class="help" title="' . format_date($order_time) . '">' . calculate_age($order_time) . '</span>'
                    );
    if($_COOKIE['spoiler_mode'] != 1)
    {   
        array_splice($values, 1, 1);
    }
    
    $table->last_seen_marker($last_seen, $order_time);
    $table->row($values);
}
$stmt->close();
$num_rows_fetched = $table->num_rows_fetched;
echo $table->output('topics');
 
// Navigate backward or forward ...
$navigation_path = 'topics';
if($_GET['bumps'])
{
    $navigation_path = 'bumps';
}
page_navigation($navigation_path, $current_page, $num_rows_fetched);
 
require('includes/footer.php');
 
?>
I don't see anything wrong at all!
I would appreciate if someone would help
Thank you,

AliJ
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Fatal error: Call to a member function bind_param()

Post by Weiry »

Your trying to call a class function on a non-object.
In other words, your $stmt is NOT a class object.
Your $link however IS a class object which i assume as been created in your includes/header.php file.

EDIT: Ignore this :banghead:
For instance, does:

Code: Select all

$stmt = $link->prepare('SELECT id, time, replies, visits, headline, body, last_post FROM topics ORDER BY id DESC LIMIT ?, ?');
return a class object which has a function bind_param()?
Or does it return something else?

Without knowing what $link->prepare() does, i can only assume your running it as a query and returning a mysql result resource.
However, does the bind_param() function exist within the $link class?
If so, you may want to rework you code to reflect that your using the $link class.[/size]
Last edited by Weiry on Sun Dec 06, 2009 9:26 pm, edited 1 time in total.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Fatal error: Call to a member function bind_param()

Post by Weiry »

Going through your code again, it seems your $stmt is never being created in order for it to ever get to your bind_param().
Try modifying your $stmt declarations inside your if statement to have this on the END:

Code: Select all

DESC LIMIT ?, ?') or die($link->error);
 
AliJ
Forum Commoner
Posts: 32
Joined: Sun Dec 06, 2009 8:03 pm

Re: Fatal error: Call to a member function bind_param()

Post by AliJ »

Okay, thanks I will try.
I'll report back to you.

Oh yes I forgot to mention,
'$link' in 'header.php' is declared like this:

Code: Select all

// Connect to the database.
$link = new mysqli($db_info['server'], $db_info['username'], $db_info['password'], $db_info['database']);
if (mysqli_connect_errno()) 
{
    exit('<p>Unable to establish a connection to the database.</p>');
}
That's why 'index.php' requires it.
AliJ
Forum Commoner
Posts: 32
Joined: Sun Dec 06, 2009 8:03 pm

Re: Fatal error: Call to a member function bind_param()

Post by AliJ »

I tried this:

Code: Select all

$stmt = $link->prepare('SELECT id, time, replies, visits, headline, body, last_post FROM topics ORDER BY last_post;DESC LIMIT ?, ?') or die($link->error);
and now I get "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';DESC LIMIT ?, ?' at line 1"
Any Idea?
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Fatal error: Call to a member function bind_param()

Post by Weiry »

As i thought :)
Remove the ";" before DESC.

EDIT::
Gahh.. stupid submit button. Look for the updates above.
Sorry, im really not focusing properly today, its something i should have picked up straight away.
AliJ
Forum Commoner
Posts: 32
Joined: Sun Dec 06, 2009 8:03 pm

Re: Fatal error: Call to a member function bind_param()

Post by AliJ »

Damn, now I get "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?' at line 1".
Why and if I remove "?, ?" I just get "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"
What is the problem?
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Fatal error: Call to a member function bind_param()

Post by Weiry »

Your selecting a LIMIT, you need 2 value's.

From what i understand, ?, ? are values which are defined outside of the query itself.
If you were to manually set them. ie, 0,5. The query will select 5 results only.
AliJ
Forum Commoner
Posts: 32
Joined: Sun Dec 06, 2009 8:03 pm

Re: Fatal error: Call to a member function bind_param()

Post by AliJ »

Thanks man, it worked but now on my web page it says "
Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statement in /home/alij/public_html/index.php on line 62"

So what should I change "0, 5" to?
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Fatal error: Call to a member function bind_param()

Post by Weiry »

You dont have to hard code it, but because your using ->bind_param(), you need to define the parameters which will be included in your statement, and then set them before you execute.

Code: Select all

$stmt = $link->prepare('SELECT id, time, replies, visits, headline, body, last_post FROM topics ORDER BY last_post DESC LIMIT ?, ?') or die($link->error);
 
$stmt->bind_param('ii', $start_sql_listing_at , $items_sql_per_page );
$start_sql_listing_at = $start_listing_at; // this is your start value
$items_sql_per_page = $items_per_page; // this is the number of rows
$stmt->execute();
AliJ
Forum Commoner
Posts: 32
Joined: Sun Dec 06, 2009 8:03 pm

Re: Fatal error: Call to a member function bind_param()

Post by AliJ »

No, Now I just get this again: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?' at line 1"

?
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Fatal error: Call to a member function bind_param()

Post by Weiry »

hmm doesnt make sense, im comparing against mysqli-stmt bind_param() and it looks like it should be working.

Well i suppose you could try doing it this way, but without the bind_param() all together.

Code: Select all

$stmt = $link->prepare("SELECT id, time, replies, visits, headline, body, last_post FROM topics ORDER BY last_post DESC LIMIT {$start_listing_at}, {$items_per_page}") or die($link->error);
$stmt->execute();
But thats probably not the best practice when using mysqli.
If that doesn't work, im afraid i don't have any more ideas.
AliJ
Forum Commoner
Posts: 32
Joined: Sun Dec 06, 2009 8:03 pm

Re: Fatal error: Call to a member function bind_param()

Post by AliJ »

Sorry for the late reply but it worked!
Thank you so much for your help.
I really do appreciate it.
So all that other stuff was a waste of time.
Thanks again

Ali J
Post Reply