Jump to content


Photo

Selecting Data From MySql


  • Please log in to reply
14 replies to this topic

#1 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 28 August 2006 - 08:34 PM

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>";
?>

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

#2 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 28 August 2006 - 08:42 PM

Just a yes or no answer required guys. Im a newbie sorry.

#3 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 28 August 2006 - 08:43 PM

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


What do you mean before every part of the table?

This will do the same thing:

<?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>";
?>


#4 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 28 August 2006 - 09:41 PM

Ok I now get this error:

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

Here is the code i have:

<?
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);
?>

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

#5 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 28 August 2006 - 09:51 PM

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:

echo "</table>";
</td>
</tr>
</table>

Should be, at a minimum like this:

echo "</table>
</td>
</tr>
</table>";

this should work overall:

<?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);
?>

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

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

#6 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 28 August 2006 - 09:57 PM

Thank You for that and i will read up properly on PHP Syntax. Unfortunately i now get this error:

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

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

#7 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 28 August 2006 - 10:01 PM

You need to escape the double quotes in your echo statements if you are going to enclose them in double quotes.


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

should be"

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

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

Same holds true for this line:

<table border='1'>

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.

<table border=\'1\'>


#8 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 28 August 2006 - 10:18 PM

Ive done what you said and put \ before every "

I now get this error:

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

Here is the code of my script:

<?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);
?>

What do i need to do now?

#9 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 28 August 2006 - 10:20 PM

I must not be paying attention today.....

change:

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

to:

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

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

#10 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 28 August 2006 - 10:28 PM

Thanks, its getting better. I now get 2 more errors. here is the web link

http://www.fmprotasy...fer_market2.php

#11 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 28 August 2006 - 10:30 PM

change:

$result = mysql_query("SELECT * FROM Market");

to:

$result = mysql_query("SELECT * FROM Market") or die(mysql_error());

to see what the error is.

#12 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 28 August 2006 - 10:35 PM

Ive done that but there is still no difference.

#13 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 28 August 2006 - 10:59 PM

Hitman do you know what to do?

#14 spfoonnewb

spfoonnewb
  • Members
  • PipPipPip
  • Advanced Member
  • 276 posts

Posted 28 August 2006 - 11:10 PM

Something didnt look right so I copied and pasted your code made a database, debugged different errors and this worked:

<?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);
?>


#15 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 28 August 2006 - 11:11 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users