rayv Posted October 3, 2010 Share Posted October 3, 2010 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 https://forums.phpfreaks.com/topic/215045-mysql_fetch_array-expects-parameter-1-to-be-resource-null-given/ Share on other sites More sharing options...
BlueSkyIS Posted October 3, 2010 Share Posted October 3, 2010 does mysqli_query() run without an error? Link to comment https://forums.phpfreaks.com/topic/215045-mysql_fetch_array-expects-parameter-1-to-be-resource-null-given/#findComment-1118558 Share on other sites More sharing options...
rayv Posted October 3, 2010 Author Share Posted October 3, 2010 outside of the function, YES. If I remove: latestpost() { } it works fine. Note, I am calling the function from another file. Link to comment https://forums.phpfreaks.com/topic/215045-mysql_fetch_array-expects-parameter-1-to-be-resource-null-given/#findComment-1118559 Share on other sites More sharing options...
BlueSkyIS Posted October 3, 2010 Share Posted October 3, 2010 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). Link to comment https://forums.phpfreaks.com/topic/215045-mysql_fetch_array-expects-parameter-1-to-be-resource-null-given/#findComment-1118560 Share on other sites More sharing options...
rayv Posted October 3, 2010 Author Share Posted October 3, 2010 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 https://forums.phpfreaks.com/topic/215045-mysql_fetch_array-expects-parameter-1-to-be-resource-null-given/#findComment-1118631 Share on other sites More sharing options...
rayv Posted October 3, 2010 Author Share Posted October 3, 2010 Thank you Guys, I already figure it out. Since the connection to the database was outside of the function, it was throwing an error. I managed to put it inside the function and it worked. Link to comment https://forums.phpfreaks.com/topic/215045-mysql_fetch_array-expects-parameter-1-to-be-resource-null-given/#findComment-1118645 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.