Jump to content

Selecting Data From MySql


superhoops

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?
Link to comment
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]
Link to comment
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.
Link to comment
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
Link to comment
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
Link to comment
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]
Link to comment
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?
Link to comment
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.
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.