Forum Moderators: coopster
I am designing a site for my baseball team. I am trying to create a basic page which would let the viewers see each player and a stat or two.
When I did this originally, it worked but the images wouldn't show. Well now that I have switched to a script I used to use that I copied from Webmonkey.com, it no longer works and I get an error saying there's a t_variable error on line 27.
Can anyone see anything wrong? The error occurs in the line with the $row["image"], $row["number"]
mysql_select_db("baseballteam", $con);
$result = mysql_query("SELECT * FROM players");
while($row = mysql_fetch_array($result))
echo "<center><table border=1>\n";
echo "<tr><td><b>Picture</b></td><td><b>Number</b></td><td align='center'><b>First Name</b></td><td><b>Last name</b></td><td><b>Hits</b></td><td><b>Strikeouts</b></td>\n";
echo "<br><br><br>";
do {
echo("<tr><td><img src='%s'/></td><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td><td align='center'>%s</td></tr>\n"
$row["image"], $row["number"], $row["firstname"], $row["lastname"], $row["hits"], $row["strikeouts"]);
}
while ($row = mysql_fetch_array($result));
echo "</table></center>\n";
echo "<br><br><br>\n";
mysql_close($con);
?>
As an afterthought, take a look at the printf() (or is it sprintf()) entry for the correct formatting. You may have some numeric values in your data that you want to have properly formatted - not just as a string.
i think there may be something syntax related higher up that might be causing that. when i changed all the echos to printf, i got a different error that pointed an unterminated line, maybe missing a ;
i just don't have the eyes yet to see something like that.
thanks!
john
printf("Some string here: %s", $string_variable);
The other two things I notice that I didn't prior; are you aware you have a while() loop up towards the top of the script that apparently outputs x amount of TABLE elements depending on how many records there are in the resultset? Then you perform a do..while() loop, which I would not recommend in a situation like this, but ok.
Also, I count 6 variables / array element values and only 5 "%s" formatting specifiers.
I made the changes you suggested and it still errors.
Here is the snippet of code referred to:
$result = mysql_query("SELECT * FROM players");
while($row = mysql_fetch_array($result))
echo "<center><table border=1>\n";
echo "<tr><td><b>Picture</b></td><td><b>Number</b></td><td align='center'><b>First Name</b></td><td><b>Last name</b></td><td><b>Hits</b></td><td><b>Strikeouts</b></td>\n";
echo "<br><br><br>";
do {
printf("<tr><td><img src='%s',/></td><td align='center'>%s,</td><td align='center'>%s,</td><td align='center'>%s,</td><td align='center'>%s,</td><td align='center'>%s,</td></tr>\n"
$row["image"], $row["number"], $row["firstname"], $row["lastname"], $row["hits"], $row["strikeouts"]);
}
this printed out 20 cells that you can see in the source code but it doesn't print content.
that may be the other problem you are referring to.
thank you for all you've done so far.
so you're saying it doesn't display the image, or it doesn't display anything? Let's revisit the while() loop situation. Near the top of the script, you had a while() loop construct that was on a single line, and a line that displayed a new TABLE element immediately after.
Let me give you an example of how a loop construct like while() or for() work in a script. Let's say I only have a single line of code I want to iterate. I can do something like this:
while( $r= some_function() )
echo $r ."<br />\n";
while( $r= some_function() ) {
/*
* now this entire block of code will run
* as many times as the loop iterates
*/
echo $r;
echo "<br />\n";
// do something else here
// or here
}
Note the curly braces.
So your code needs to be something more like this:
// begin the table element
echo "<table>\n";
while( $row= mysql_fetch_assoc($result) ) {
// start a new table row
echo "<tr>\n";
// populate the cells with data
echo "<td>{$row['data1']}</td>\n";
echo "<td>{$row['data2']}</td>\n";
// etc// close the row element
echo "</tr>\n";
}
// close the table
echo "</table>\n";
So the table element is started outside the loop, and each row in the table is created and populated on each iteration through the loop, for as many records as there are in the resultset. Then you end the loop block and close the table. Very glib example, but it should provide you with a sound starting point for your script.
I'd really take some time to read through the PHP manual [php.net], especially the Language Reference [php.net].
Yes. mysql_fetch_array() [php.net] retrieves each record in the resultset as both a numeric and associative indexed array. So in other words, there are two indexes and two values for every field in the row. This looks something like this:
array (
0 => "dogs",
"pet_type" => "dogs",
1 => "cats",
"pet_type" => "cats"
)
On the other hand, mysql_fetch_assoc() [php.net] only retrieves each record as an associative indexed array, so it retains the names of the fields as index values but not the numeric index. It's half the size! I suppose it's debatable as to whether or not there is real savings in server memory between one or the other, but I prefer the associative index only.
There are a couple of other options (mysql_fetch_row() [php.net] - a numeric index only array and mysql_fetch_object() [php.net] - where each record is represented by an object), both of which I don't use very often.