Jump to content

Mysql returning result Array


thebusiness

Recommended Posts

I have listed  a part of my code below that I am getting a strange error with.

 

Everything works except when I try to use $random_game_mini['category']

it just returns with the results "Array"

 

any ideas on this?  Thanks in advance

 

$sqla = mysql_query("SELECT * FROM ava_games WHERE published=1 ORDER BY rand() LIMIT $template[random_game_mini_limit]");

while($row = mysql_fetch_array($sqla)) {

$description_stripped = strip_tags($row['description']);

$random_game_mini['name'] = shortenStr($row['name'], $template['random_game_mini_chars']);
$random_game_mini['description'] = shortenStr($description_stripped, $template['random_game_mini_desc_chars']);

$random_game_mini['url'] = GameUrl($row['id'], $row['seo_url'], $row['category_id']);
$random_game_mini['image_url'] = GameImageUrl($row['image'], $row['import'], $row['url']);

$catid = $row['category_id'];
$random_game_mini['category'] = mysql_fetch_array(mysql_query("SELECT name FROM ava_cats WHERE id=$catid"));

Link to comment
https://forums.phpfreaks.com/topic/241829-mysql-returning-result-array/
Share on other sites

Because you fetch an array here:

 

$random_game_mini['category'] = mysql_fetch_array(mysql_query("SELECT name FROM ava_cats WHERE id=$catid"));

 

It's easy to fix, but you shouldn't be doing a query in a loop.  Use a join in the original query (not tested):

 

$sqla = mysql_query("SELECT *, ava_cats.name AS cat
FROM ava_games, ava_cats
WHERE ava_games.category_id = ava_cats.id AND published = 1
ORDER BY rand()
LIMIT {$template['random_game_mini_limit']}");

 

Then use:

 

$random_game_mini['category'] = $row['cat'];

 

 

 

Because you fetch an array here:

 

$random_game_mini['category'] = mysql_fetch_array(mysql_query("SELECT name FROM ava_cats WHERE id=$catid"));

 

It's easy to fix, but you shouldn't be doing a query in a loop.  Use a join in the original query (not tested):

 

$sqla = mysql_query("SELECT *, ava_cats.name AS cat
FROM ava_games, ava_cats
WHERE ava_games.category_id = ava_cats.id AND published = 1
ORDER BY rand()
LIMIT {$template['random_game_mini_limit']}");

 

Then use:

 

$random_game_mini['category'] = $row['cat'];

 

Your awesome, works like a charm!

Actually I seem to have broken a lot of stuff trying to use this same idea in other files that called the cat

 

turning

 

 

$sql = mysql_query("SELECT * FROM ava_games WHERE published=1 ORDER BY hits desc LIMIT 3");

 

into

 

$sql = mysql_query("SELECT *, ava_cats.name AS cat
FROM ava_games, ava_cats
WHERE ava_games.category_id = ava_cats.id AND published = 1
ORDER BY hits desc
LIMIT 3");

 

 

broke a few things, then

 

turning

$sql = mysql_query("SELECT * FROM ava_games WHERE published=1 ORDER BY id desc LIMIT 3");

 

$sql = mysql_query("SELECT *, ava_cats.name AS cat
FROM ava_games, ava_cats
WHERE ava_games.category_id = ava_cats.id AND published = 1
ORDER BY id desc
LIMIT 3");

 

 

Broke the whole thing

I cant figure out how to edit, but I should mention that using the first code no longer let any of the other calls work

 

like

$random_game_mini['name'] = shortenStr($row['name'], $template['random_game_mini_chars']);

 

It just displayed a blank value

 

upon further looking into it

 

when I do the code

 

$sql = mysql_query("SELECT *, ava_cats.name AS cat
FROM ava_games, ava_cats
WHERE ava_games.category_id = ava_cats.id AND published = 1
ORDER BY hits desc
LIMIT 3");

while($row = mysql_fetch_array($sql)) {

$description_stripped = strip_tags($row['description']);

$popularmod['name'] = shortenStr($row['name'], $template['popularmod_chars']);
$popularmod['description'] = shortenStr($description_stripped, $template['popularmod_desc_chars']);

$popularmod['url'] = GameUrl($row['id'], $row['seo_url'], $row['category_id']);
$popularmod['image_url'] = GameImageUrl($row['image'], $row['import'], $row['url']);

$popularmod['plays'] = $row['hits'];
$popularmod['category'] = $row['cat'];

 

Nothing works but the

$popularmod['category'] = $row['cat'];

 

Nothing else seems to pull from the database

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.