/    Sign up×
Community /Pin to ProfileBookmark

Warning: Invalid argument supplied for foreach()

I’m very new to PHP. I’m getting this message on this page and just don’t understand why?

Thanks for your help….
——–

<?php
//Sample Database Connection Syntax for PHP and MySQL.

//Connect To Database

$hostname=””;
$username=””;
$password=””;
$dbname=””;

mysql_connect($hostname,$username, $password) or die (“<html><script language=’JavaScript’>alert(‘Unable to connect to database! Please try again later.’),history.go(-1)</script></html>”);
mysql_select_db($dbname);

# Check If Record Exists

$query = “SELECT * FROM players
WHERE playeryear = 2011
ORDER BY number”;

$result = mysql_query($query);
?>

<?php foreach ($result as $playerslist) { ?>
<tr>
<td><?php echo $playerslist[‘playersname’]; ?></td>
<td><?php echo $playerslist[‘number’]; ?></td>
<td><?php echo $playerslist[‘playerpos’]; ?></td>
</tr>

<?php } ?>
———–

Any help would be greatly appreciated…

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@OctoberWindMar 05.2012 — Chek your SQL query. It's likely that it's not retuning what your expecting, thus $result is not an array.
Copy linkTweet thisAlerts:
@hyperionXSMar 05.2012 — You are using it wrong.
[CODE]
$query = "SELECT * FROM players
WHERE playeryear = 2011
ORDER BY number";

$result = mysql_query($query);

[COLOR="Red"] while ($playerslist = mysql_fetch_assoc($result)) { ?>[/COLOR]
<tr>
<td><?php echo $playerslist['playersname']; ?></td>
<td><?php echo $playerslist['number']; ?></td>
<td><?php echo $playerslist['playerpos']; ?></td>
</tr>
<?php } ?>
[/CODE]

You forgot to call mysql_fetch_assoc
Copy linkTweet thisAlerts:
@jeffh13_msn_comauthorMar 05.2012 — That doesn't surprise me...

Thanks for your help I'll try it tonight.
Copy linkTweet thisAlerts:
@jeffh13_msn_comauthorMar 05.2012 — Thanks it did work!!!

Here is what the text book originally told me to write it....
-----


<?php

//Sample Database Connection Syntax for PHP and MySQL.

//Connect To Database

$hostname="";

$username="";

$password="";

$dbname="";

mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");

mysql_select_db($dbname);

# Check If Record Exists



$query = "SELECT * FROM players

WHERE playeryear = 2011

ORDER BY number";

$players = $db->query($query);

?>

<?php foreach ($players as $playerlist) : ?>

<tr>

<td><?php echo $playerlist['playersname']; ?></td>

<td><?php echo $playerlist['number']; ?></td>

<td><?php echo $playerlist['playerpos']; ?></td>

<td><?php echo $playerlist['height']; ?></td>

<td><?php echo $playerlist['weight']; ?></td>

<td><?php echo $playerlist['grade']; ?><br></td>

</tr>

<?php endforeach; ?>
----



I get this error..

Fatal error: Call to a member function query()

Line 19 which is ...

$players = $db->query($query);

How should it really be written using the example I was testing...

Thanks for all your help....
×

Success!

Help @jeffh13_msn_com spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...