Jump to content

Select mysql & display in an html table using php


skidmark10

Recommended Posts

Hello,

 

I am trying to retrieve and display data from a mysql table into a html table using php. I am using this code and it works successfully.

 

<?php
mysql_select_db("my_db", $con);

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

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

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

mysql_close($con);
?>

 

However, I want $row['FirstName'] and $row['LastName'] to appear in the SAME cell of the html table, next to one another. How would I write the code for that, if it is possible

 

Thanks in advance!

mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
echo "<table border='1'><tr><th>Name</th></tr>";
while($row = mysql_fetch_array($result))  {  
echo "<tr>";  echo "<td>" . $row['FirstName'] . ' ' . $row['LastName']"</td>";  
echo "</tr>";  
}
echo "</table>";
mysql_close($con);
?>

mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
echo "<table border='1'><tr><th>Name</th></tr>";
while($row = mysql_fetch_array($result))  {  
echo "<tr>";  echo "<td>" . $row['FirstName'] . ' ' . $row['LastName']"</td>";  
echo "</tr>";  
}
echo "</table>";
mysql_close($con);
?>

 

I don't think that works, I keep receiving the same error as before :-\

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';'

 

No where in your first post did you say anything about a parse error!  Post your ENTIRE script, that is the only way to deal with parse errors. (Include the acutal error also, there should be a line number in it.)

 

I tried a similar code (not the code I posted)

 

I put this and got a parse error

echo "<td>" . $row['FirstName'] . " . $row['LastName']"</td>"

;

 

Then I tried your code too and received a parse error

echo "<td>" . $row['FirstName'] . ' ' . $row['Last_Name']"</td>";

this line

. $row['LastName']"</td>";  

 

Is missing a '.'

 

try

<?php
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
echo "<table border='1'><tr><th>Name</th></tr>";
while($row = mysql_fetch_array($result))  {  
echo "<tr>";  echo "<td>" . $row['FirstName'] . ' ' . $row['LastName']. "</td>";  
echo "</tr>";  
}
echo "</table>";
mysql_close($con);
?>

 

this line

. $row['LastName']"</td>";  

 

Is missing a '.'

 

try

<?php
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
echo "<table border='1'><tr><th>Name</th></tr>";
while($row = mysql_fetch_array($result))  {  
echo "<tr>";  echo "<td>" . $row['FirstName'] . ' ' . $row['LastName']. "</td>";  
echo "</tr>";  
}
echo "</table>";
mysql_close($con);
?>

 

I apologize for that, I'm gonna have to get more sleep, and quit being mean to people!

this line

. $row['LastName']"</td>";  

 

Is missing a '.'

 

try

<?php
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
echo "<table border='1'><tr><th>Name</th></tr>";
while($row = mysql_fetch_array($result))  {  
echo "<tr>";  echo "<td>" . $row['FirstName'] . ' ' . $row['LastName']. "</td>";  
echo "</tr>";  
}
echo "</table>";
mysql_close($con);
?>

 

I apologize for that, I'm gonna have to get more sleep, and quit being mean to people!

 

Thank you so much!

Archived

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

×
×
  • 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.