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

what i'm trying to get at is this:

 

$result = mysqli_query($dbc, $q) or die(mysqli_error());

 

i suspect the query is failing, so $result is not a resource. and the problem is probably due to $dbc being undefined (not set).

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>";

  }

 

Archived

This topic is now archived and is closed to further replies.

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