problem with mysql_fetch_array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danharibo
Forum Commoner
Posts: 76
Joined: Thu Aug 17, 2006 8:56 am

problem with mysql_fetch_array

Post by danharibo »

When i do this:

Code: Select all

$titleq = "SELECT name FROM ".TBL_GAMES.";";
$title = mysql_query($titleq);
$games = mysql_fetch_array($title, MYSQL_ASSOC);
echo "<td>.{$games['name']}.";
I get this:

Notice: Undefined index: name in c:\program files\easyphp1-8\www\cms\games.php on line 39
But The desctipt Value of my array works fine :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Add some error checking..

Code: Select all

mysql_query(...) or die(mysql_error());
Consider looking at what $games is: var_dump($games);
danharibo
Forum Commoner
Posts: 76
Joined: Thu Aug 17, 2006 8:56 am

Post by danharibo »

Ahha, it seems Name isnt being added to the array :/
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Maybe it's no array at all.

Code: Select all

$titleq = 'SELECT name FROM '.TBL_GAMES;
$title = mysql_query($titleq) or die(mysql_error());

if ( $games = mysql_fetch_array($title, MYSQL_ASSOC) ) {
	echo '<td>', $games['name'], '</td>';
}
else {
	echo '<td>---</td>';
}
danharibo
Forum Commoner
Posts: 76
Joined: Thu Aug 17, 2006 8:56 am

Post by danharibo »

there is an array, Description is added fine
http://danharibo.dyndns.org/cms/games.php See, the description bit loads
danharibo
Forum Commoner
Posts: 76
Joined: Thu Aug 17, 2006 8:56 am

Post by danharibo »

i fixed it :D, but now i need to get all of the collum? how do i so that with Aquery?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

while loop + mysql_fetch_assoc() + $foo[] = $row['var']
danharibo
Forum Commoner
Posts: 76
Joined: Thu Aug 17, 2006 8:56 am

Post by danharibo »

according to php.net mysql_fetch_assoc() is only availaable for php5
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Not true.
PHP.net wrote:mysql_fetch_assoc
(PHP 4 >= 4.0.3, PHP 5)
Will work with PHP 4.

Or if you don't have PHP 4, you could do this on PHP 3 with mysql_fetch_array($result, MYSQL_ASSOC)

an example of getting all values of a column into an array.

Code: Select all

$my_array = array();

$result = mysql_query($sql) or die(mysql_error());

while($array = mysql_fetch_assoc($result))
{
   $my_array[] = $array['column_name'];
}

echo '<pre>';
print_r($my_array);
echo '</pre>';
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply