thebusiness Posted July 12, 2011 Share Posted July 12, 2011 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 More sharing options...
AbraCadaver Posted July 12, 2011 Share Posted July 12, 2011 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']; Link to comment https://forums.phpfreaks.com/topic/241829-mysql-returning-result-array/#findComment-1241910 Share on other sites More sharing options...
thebusiness Posted July 12, 2011 Author Share Posted July 12, 2011 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! Link to comment https://forums.phpfreaks.com/topic/241829-mysql-returning-result-array/#findComment-1241911 Share on other sites More sharing options...
thebusiness Posted July 12, 2011 Author Share Posted July 12, 2011 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 Link to comment https://forums.phpfreaks.com/topic/241829-mysql-returning-result-array/#findComment-1241921 Share on other sites More sharing options...
thebusiness Posted July 12, 2011 Author Share Posted July 12, 2011 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 Link to comment https://forums.phpfreaks.com/topic/241829-mysql-returning-result-array/#findComment-1241928 Share on other sites More sharing options...
thebusiness Posted July 12, 2011 Author Share Posted July 12, 2011 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 Link to comment https://forums.phpfreaks.com/topic/241829-mysql-returning-result-array/#findComment-1241998 Share on other sites More sharing options...
AbraCadaver Posted July 12, 2011 Share Posted July 12, 2011 I didn't think so, but you may have to replace the * with all of the columns from ava_games: ava_games.description, ava_games.name, etc. Or try just ava_games.*, ava_cats.name AS cat Link to comment https://forums.phpfreaks.com/topic/241829-mysql-returning-result-array/#findComment-1242015 Share on other sites More sharing options...
thebusiness Posted July 12, 2011 Author Share Posted July 12, 2011 I didn't think so, but you may have to replace the * with all of the columns from ava_games: ava_games.description, ava_games.name, etc. Or try just ava_games.*, ava_cats.name AS cat Yeah the simple ava_games.* seemed to work at first shot Link to comment https://forums.phpfreaks.com/topic/241829-mysql-returning-result-array/#findComment-1242019 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.