Trying to use PHP to display contents of MySQL table in HTML table. NOT WORKING!

Hi,
This is my very first post, and I would SO appreciate help! I have been choking on this code for a month and can’t figure out what’s wrong. I know enough to understand what the code is supposed to do, just not enough to know why it’s not doing it. I have been getting this error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\wamp\www\statsdisplay.php on line 36

The html table header is displayed, followed by the error warning. Here is my code. I am wondering if there’s a problem with my data definitions. Thanks in advance, I’d love to finish this website, it’s a freebie and it’s taking WAY too long.

<?php $connection= mysql_connect("localhost","root",""); if (!$connection) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $connection); mysql_query("SELECT * FROM cougars"); $result = array(); echo ""; while($row = mysql_fetch_array('$result')) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
Number Player Height Height Position Highschool College
" . $row['Number'] . "" . $row['Player'] . "" . $row['Height'] . "" . $row['Weight'] . "" . $row['Position'] . "" . $row['Highschool'] . "" . $row['College'] . "
"; mysql_close($connection); ?>

hello, (waving)

change these lines: [php]mysql_query(“SELECT * FROM cougars”);
$result = array();
[/php]

to
[php]$result = mysql_query(“SELECT * FROM cougars”);[/php]

and this line: [php]while($row = mysql_fetch_array(’$result’))[/php]
to
[php]while($row = mysql_fetch_array($result, MYSQL_ASSOC))[/php]

Hope that helps,

Red :wink:

Red,
It still doesn’t work. :frowning: Any other ideas? And thank you!

let me ask you a couple of things, forgive me if they sound like dumb questions…

  1. Do you have a password for the database? if so, did you leave it out this script deliberately…(which is good) (i don’t need to know it, i’m just making sure)
  2. is your database called my_db?
  3. is the table called cougars?
  4. is all the spelling correct? (including Capitolization?)

The changes i posted below the script works on my setup…
… also, you have named a column Height twice, rather than height/weight…

Red.

Ok, I changed “my_db” to the actual name of the database. There is no password for simplicity’s sake. I will make one when I launch this site. Changed the duplicate height column. Now I get this:
Notice: Undefined index: Number in C:\wamp\www\statsdisplay.php on line 39

First, I would “functionize” your Database information in a separate include file (func.inc.php)

[php]
function siteDB() {
mysql_connect(“host”, “user”, “pass”) or die();
mysql_select_db(“yourDB”);

I would change the following:
(somewhat inline with what Redscouse recommended, only I would go ahead and in the while loop, just declare the assoc_array with PHP’s mysql_fetch_assoc function. That is after you place the query into a variable (this case $result = mysql_query(“SELECT * FROM cougars”))

[php]
while($row = mysql_fetch_assoc(’$result’))
[/php]

and then when you pull the $row data…
[php]
{
// i output all my while() data to vars just because I’ve had problems in the past
// with the processes timing out, and then i get a $resourceID:x for a return.
// and this way, it simplifies your remembrance of the output data.
$num = $row[‘Number’];
$player = $row[‘Player’];
$height = $row[‘Height’];
$weight = $row[‘Weight’];
$pos = $row[‘Position’];
$hs = $row[‘Highschool’];
$col = $row[‘college’];

// I am a fool for echo <<< EOT, which i use on a lot of my database output’s through a while() loop.
// it is simple, quick, and you can place your $vars from above in the

's, and if you need to add
// any data with "'s or ’ - you DO NOT HAVE TO ESCAPE THEM or change the echo " to a echo ’ –
// your preference really.
echo <<< EOT $num $player $height $weight $pos $hs $col EOT; } echo ""; [/php]

Hope this helps!!

Wow… I’m so sorry…

[php]
function siteDB() {
mysql_connect(“host”, “user”, “pass”) or die();
mysql_select_db(“yourDB”);
}
[/php]

and then at the top of the file(s) you intend to use the database on you would simply either include() or require() the function file, and then just make the call of

[php]

<? siteDB(); # code code silly code # database code, etc ?>

[/php]
the great part is, you can use the function for EVERYTHING database related on the page, and then close it out after the last query you use, preferably in the same line you close out the rest of the code (i call it my EOF block).

Sponsor our Newsletter | Privacy Policy | Terms of Service