Web Hosting Talk







View Full Version : mysql_fetch_array() error


Snitz
06-03-2008, 07:21 AM
Could somebody please tell me what's wrong with my code. I'm getting this error whenever I run it.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\myapp\database.php on line 90

Line 90 is this
for ($i=0; $row = mysql_fetch_array($result); $i++)

This is the code

<?

function db_connect()
{
$connection = @mysql_connect('localhost','','');
if (!$connection) {
return false;
}
if (!mysql_select_db('', $connection)) {
return false;
}
return $connection;
}

function create_post($param)
{
$connection = db_connect();
$query = sprintf("Insert into posts
Set
title = '%s',
body = '%s',
created_at = NOW(),
user_id = '%s'
",
mysql_real_escape_string($param['title']),
mysql_real_escape_string($param['body']),
mysql_real_escape_string($param['user_id']));
$result = mysql_query($query);
if (!$result) {
return false;
} else {
return true;
}
}


function update_post($param)
{
$connection = db_connect();
$query = sprintf("update posts
Set
title = '%s',
body = '%s',
user_id = '%s'
where id = '%s'
",
mysql_real_escape_string($param['title']),
mysql_real_escape_string($param['body']),
mysql_real_escape_string($param['user_id']),
mysql_real_escape_string($param['id']));
$result = mysql_query($query);
if (!$result) {
return false;
} else {
return true;
}
}

function delete_post($id)
{
$connection = db_connect();
$query = sprintf("delete from posts where id = '%s'",
mysql_real_escape_string($id));
$result = mysql_query($query);
if (!$result) {
return false;
} else {
return true;
}
}

function find_posts() {
$connection = db_connect();
$query = 'Select posts.title, posts.body, posts.user_id, users.username from posts, users where posts.user_id = users.id';
$result = mysql_query($query);
$number_of_posts = mysql_num_rows($result);
if ($number_of_posts == 0) {
return false;
}
$result = result_to_array($result);
return $result;
}

function result_to_array() {
$result_array = array();
for ($i=0; $row = mysql_fetch_array($result); $i++)
{
$result_array[$i] = $row;
return $result_array;
}

}

$posts = find_posts();

print_r($posts);

?>

azizny
06-03-2008, 08:48 AM
Change:


$query = 'Select posts.title, posts.body, posts.user_id, users.username from posts, users where posts.user_id = users.id';
$result = mysql_query($query);


To:


$query = 'Select posts.title, posts.body, posts.user_id, users.username from posts, users where posts.user_id = users.id';
$result = mysql_query($query) or die(mysql_error());


Peace,

X-TechMedia
06-03-2008, 09:03 AM
In your find_posts function you call:
$result = result_to_array($result);

But the result_to_array function doesnt accept any arguments.
function result_to_array()
should be
function result_to_array($result)

Snitz
06-03-2008, 09:23 AM
In your find_posts function you call:
$result = result_to_array($result);

But the result_to_array function doesnt accept any arguments.
function result_to_array()
should be
function result_to_array($result)

Thank you man, I don't know how didn't I notice that.
Thanks again.

Snitz
06-03-2008, 10:09 AM
I just made a little change to the script, replaced these 2 lines at the bottm:

$posts = find_posts();
print_r($posts);
with this:

$posts = find_posts();

foreach ($posts as $post) {
echo '<h2>'.$post['title'].'</h2>';
echo $post['body'].'<br/>';
echo $post['username'];
}
And now, although I have 3 rows in the "posts" table, it's only showing the first row only (id=1)

Why aren't all the other rows showing? :S

latheesan
06-03-2008, 10:49 AM
Here's a nicer (or even better) way to get data from sql db and display it:

$result = mysql_query("SELECT * FROM `table_name`");
while ($row = mysql_fetch_array($result))
{
echo '<h2>'.$row['title'].'</h2>';
echo $row['body'].'<br/>';
echo $row['username'];
echo '<hr noshade size="1" />';
}

Try this~