Jump to content

mysql_fetch_array() expects parameter 1 to be resource, null given


rayv

Recommended Posts

When I run my code outside of a function it works but when i put it on this function I get these three errors: 

  1.  mysql_fetch_array() expects parameter 1 to be resource, null given on line 20 which is:

      "while($row = mysql_fetch_array($result, MYSQLI_ASSOC))"

 

  2.  on line 19: mysqli_query() expects parameter 1 to be mysqli, null given which is:

      "$result = mysqli_query($dbc, $q);"

 

  3. on line 19: Undefined variable: dbc

Here is the function call:   

 

<h5>Latest Posts</h5>

<div class="box">

<div class="content">

<ul>

<?php

latestpost();

?>

</ul>

</div>

</div>

 

Here is the function:

 

 

 

require_once ('mysqli_connect.php');

 

 

function latestpost() {

 

    $q = "SELECT * FROM posts p, threads t, users u

        where t.thread_id = p.thread_id and u.user_id = t.user_id

        ORDER BY p.posted_on DESC LIMIT 0, 5";

 

                $result = mysqli_query($dbc, $q);

  while($row = mysql_fetch_array($result, MYSQLI_ASSOC))

               

{

 

extract($row);

 

echo "<li><a target='_blank' href='http://localhost/sample/forum/viewtopic.php?f=".      $p.post_id."&t=".$t.thread_id."'>

                    <strong>".$subject."</strong></a><br /><small>Member since ".      $p.posted_on."</small></li>";

}

 

}

 

 

 

Here is the connection:

 

<?php 

 

mysqli_connect.php is:

 

DEFINE ('DB_USER', 'username');

DEFINE ('DB_PASSWORD', 'password');

DEFINE ('DB_HOST', 'localhost');

DEFINE ('DB_NAME', 'database');

 

// Make the connection:

 

$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

 

 

// Set the encoding...

mysqli_set_charset($dbc, 'utf8');

 

// end

Link to comment
Share on other sites

original post shows: "while($row = mysql_fetch_array($result, MYSQLI_ASSOC))" which does not work.  The one that works is:

"while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))"

 

I forgot to correct the code to use mysqli_fetch instead i left it as mysql_fetch

 

 

This code works outside of the function:

 

 

$q = "SELECT * FROM posts p, threads t, users u

        where t.thread_id = p.thread_id and u.user_id = t.user_id

        ORDER BY p.posted_on DESC LIMIT 0, 5";

 

                $result = mysqli_query($dbc, $q);

  while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))

               

{

 

  extract($row);

 

echo "<li><a target='_blank' href='http://www.i-tech-system.com/forum/viewtopic.php?f=".$p.post_id."&t=".$t.thread_id."'>

                    <strong>".$subject."</strong></a><br /><small>Member since ".$p.posted_on."</small></li>";

  }

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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