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