Jump to content

Archived

This topic is now archived and is closed to further replies.

superhoops

Selecting Data From MySql

Recommended Posts

[code]I know this might be a really simple answer but i am going to work on using this example for what i need to do which is select the data from my database and put it onto a html table.

Here is some of the code from the tutorial i am using:

[code}
<?
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . echo $row['FirstName'] . "</td>";
  echo "<td>" . echo $row['LastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
?>
[/code]

My first question is, is the echo before every part of the table nessary?

Share this post


Link to post
Share on other sites
[quote]My first question is, is the echo before every part of the table nessary?[/quote]

What do you mean before every part of the table?

This will do the same thing:

[code]<?php
echo "
<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysql_fetch_array($result)) {
echo "
  <tr>
  <td>" . echo $row['FirstName'] . "</td>
  <td>" . echo $row['LastName'] . "</td>
</tr>";
}

echo "</table>";
?>[/code]

Share this post


Link to post
Share on other sites
Ok I now get this error:

[b]Parse error: syntax error, unexpected '<' in /home/www/fmprotasy.com/Test/tranmark2main.php on line 14[/b]

Here is the code i have:

[code]
<?
mysql_select_db("fmprotasy_reg", $con);

$result = mysql_query("SELECT * FROM Market");
<table border="0" bgcolor="#FF8C00" width="100%" cellpadding="10">
<tr>

<td width="100%" valign="top" style="border-style: solid; border-width: 2">
<p align="left"><b><font face="Arial" size="2">Transfer Market</font></b></td>

</tr>
</table>
<table border="0" bgcolor="#FF8C00" width="100%" cellpadding="10" height="15">
<tr>
<td width="100%" valign="top" height="1" style="border-style: solid; border-width: 2">
echo "<table border='1'>
<tr>
<th>Player</th>
<th>Club</th>
<th>Position</th>
<th>Age</th>
<th>Ka</th>
<th>Ta</th>
<th>Pa</th>
<th>Sa</th>
<th>Type</th>
<th>Price</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['Player'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['Team'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['Position'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['Age'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['GK'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['DEF'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['MID'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['ATT'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['Type'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['Price'] . "</font></td>";
  echo "</tr>";
  }
echo "</table>";
</td>
</tr>
</table>

mysql_close($con);
?>
[/code]

Im sure there is loads of mistakes but if anyone could identify the problem for me i would be very greatful, thanks.

Share this post


Link to post
Share on other sites
You need to escape the double quotes in your echo statements if you are going to enclose them in double quotes.

You aren't echoing out the html that is inside of the php.  For example:

[code]echo "</table>";
</td>
</tr>
</table>[/code]

Should be, at a minimum like this:

[code]echo "</table>
</td>
</tr>
</table>";[/code]

this should work overall:

[code]<?php
mysql_select_db("fmprotasy_reg", $con);
$result = mysql_query("SELECT * FROM Market");

echo '
<table border="0" bgcolor="#FF8C00" width="100%" cellpadding="10">
<tr>
<td width="100%" valign="top" style="border-style: solid; border-width: 2">
<p align="left"><b><font face="Arial" size="2">Transfer Market</font></b>
</td>
</tr>
</table>
<table border="0" bgcolor="#FF8C00" width="100%" cellpadding="10" height="15">
<tr>
<td width="100%" valign="top" height="1" style="border-style: solid; border-width: 2">
<table border='1'>
<tr>
<th>Player</th>
<th>Club</th>
<th>Position</th>
<th>Age</th>
<th>Ka</th>
<th>Ta</th>
<th>Pa</th>
<th>Sa</th>
<th>Type</th>
<th>Price</th>
</tr>';

while($row = mysql_fetch_array($result)) {
  echo "<tr>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['Player'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['Team'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['Position'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['Age'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['GK'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['DEF'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['MID'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['ATT'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['Type'] . "</font></td>";
  echo "<td align="center"><font face="Arial" size="2">" . echo $row['Price'] . "</font></td>";
  echo "</tr>";
}
echo "
</table>
</td>
</tr>
</table>";

mysql_close($con);
?>[/code]

Please read a tutorial on basic php syntax, which will solve 95% of the problems you experience.

http://www.w3schools.com/php/default.asp

Share this post


Link to post
Share on other sites
Thank You for that and i will read up properly on PHP Syntax. Unfortunately i now get this error:

[b]Parse error: syntax error, unexpected T_LNUMBER, expecting ',' or ';' in /home/www/fmprotasy.com/Test/tranmark2main.php on line 25[/b]

I have looked at line 25 and the line before it and i can't see the error but im sure it is very simple.

Sorry

Share this post


Link to post
Share on other sites
[quote]You need to escape the double quotes in your echo statements if you are going to enclose them in double quotes.[/quote]

[code]echo "<td align="center"><font face="Arial" size="2">" . echo $row['Player'] . "</font></td>";[/code]

should be"

[code]echo "<td align=\"center\"><font face=\"Arial\" size=\"2\">" . echo $row['Player'] . "</font></td>";[/code]

All of the other echo statements should be the same way...you have to escape them.

Same holds true for this line:

[code]<table border='1'>[/code]

Because it's in an echo statment that encloses the echoed text in single quotes, they must be escaped or they will cause an error.

[code]<table border=\'1\'>[/code]

Share this post


Link to post
Share on other sites
Ive done what you said and put \ before every "

I now get this error:

[b]Parse error: syntax error, unexpected T_ECHO in /home/www/fmprotasy.com/Test/tranmark2main.php on line 40[/b]

Here is the code of my script:

[code]
<?php

$db = mysql_connect("db5.awardspace.com:3306", "fmpsite_reg", "password") or die("Could not connect.");
if(!$db)
die("no db");
if(!mysql_select_db("fmpsite_reg",$db))
die("No database selected.");
if(!get_magic_quotes_gpc())
?>
<?php
mysql_select_db("fmprotasy_reg", $con);
$result = mysql_query("SELECT * FROM Market");

echo '<table border=\"0\" bgcolor=\"#FF8C00\" width=\"100%\" cellpadding=\"10\">
<tr>
<td width=\"100%\" valign=\"top\" style=\"border-style: solid; border-width: 2\">
<p align=\"left\"><b><font face=\"Arial\" size="2">Transfer Market</font></b>
</td>
</tr>
</table>
<table border="0" bgcolor=\"#FF8C00\" width=\"100%\" cellpadding=\"10\" height=\"15\">
<tr>
<td width=\"100%\" valign=\"top\" height=\"1\" style=\"border-style: solid; border-width: 2\">
<table border=\"1\">
<tr>
<th>Player</th>
<th>Club</th>
<th>Position</th>
<th>Age</th>
<th>Ka</th>
<th>Ta</th>
<th>Pa</th>
<th>Sa</th>
<th>Type</th>
<th>Price</th>
</tr>';

while($row = mysql_fetch_array($result)) {
  echo "<tr>";
echo "<td align=\"center\"><font face=\"Arial\" size=\"2\">" . echo $row['Player'] . "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\" size=\"2\">" . echo $row['Team'] . "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\" size=\"2\">" . echo $row['Position'] . "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\" size=\"2\">" . echo $row['Age'] . "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\" size=\"2\">" . echo $row['GK'] . "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\" size=\"2\">" . echo $row['DEF'] . "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\" size=\"2\">" . echo $row['MID'] . "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\" size=\"2\">" . echo $row['ATT'] . "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\" size=\"2\">" . echo $row['Type'] . "</font></td>";
echo "<td align=\"center\"><font face=\"Arial\" size=\"2\">" . echo $row['Price'] . "</font></td>";
  echo "</tr>";
}
echo "
</table>
</td>
</tr>
</table>";

mysql_close($con);
?>
[/code]

What do i need to do now?

Share this post


Link to post
Share on other sites
I must not be paying attention today.....

change:

[code]echo "<td align=\"center\"><font face=\"Arial\" size=\"2\">" . echo $row['Player'] . "</font></td>";[/code]

to:

[code]echo "<td align=\"center\"><font face=\"Arial\" size=\"2\">" . $row['Player'] . "</font></td>";[/code]

In all of the other, equivalent, statements as well.

Share this post


Link to post
Share on other sites
Thanks, its getting better. I now get 2 more errors. here is the web link

http://www.fmprotasy.com/Test/transfer_market2.php

Share this post


Link to post
Share on other sites
change:

[code]$result = mysql_query("SELECT * FROM Market");[/code]

to:

[code]$result = mysql_query("SELECT * FROM Market") or die(mysql_error());[/code]

to see what the error is.

Share this post


Link to post
Share on other sites
Something didnt look right so I copied and pasted your code made a database, debugged different errors and this worked:

[code]<?php
mysql_select_db('fmprotasy_reg', $con);
$result = mysql_query('SELECT * FROM Market');

echo '<table border=0 bgcolor=#FF8C00 width=100% cellpadding=10>
<tr>
<td width=100% valign=top style=border-style: solid; border-width: 2>
<p align=left><b><font face=Arial size=2>Transfer Market</font></b>
</td>
</tr>
</table>
<table border=0 bgcolor=#FF8C00 width=100% cellpadding=10 height=15>
<tr>
<td width=100% valign=top height=1 style=border-style: solid; border-width: 2>
<table border=1>
<tr>
<th>Player</th>
<th>Club</th>
<th>Position</th>
<th>Age</th>
<th>Ka</th>
<th>Ta</th>
<th>Pa</th>
<th>Sa</th>
<th>Type</th>
<th>Price</th>
</tr>';

while($row = mysql_fetch_array($result)) {
 echo '<tr>';
 echo '<td align=center><font face=Arial size=2>' . $row['Player'] . '</font></td>';
 echo '<td align=center><font face=Arial size=2>' . $row['Team'] . '</font></td>';
 echo '<td align=center><font face=Arial size=2>' . $row['Position'] . '</font></td>';
 echo '<td align=center><font face=Arial size=2>' . $row['Age'] . '</font></td>';
 echo '<td align=center><font face=Arial size=2>' . $row['GK'] . '</font></td>';
 echo '<td align=center><font face=Arial size=2>' . $row['DEF'] . '</font></td>';
 echo '<td align=center><font face=Arial size=2>' . $row['MID'] . '</font></td>';
 echo '<td align=center><font face=Arial size=2>' . $row['ATT'] . '</font></td>';
 echo '<td align=center><font face=Arial size=2>' . $row['Type'] . '</font></td>';
 echo '<td align=center><font face=Arial size=2>' . $row['Price'] . '</font></td>';
 echo '</tr>';
}
echo '
</table>
</td>
</tr>
</table>';

mysql_close($con);
?>[/code]

Share this post


Link to post
Share on other sites
Hitman i sorted the problem, it was very simple in the end.

Thank You so much for all the help you have given me. I really appreicate it.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.