0

I am trying to get data from mysql using this template but it is not working and I am unable find errors in below code:

<?php
$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
$db = mysql_select_db("company", $connection); // Selecting Database
//MySQL Query to read data
$query = mysql_query("select * from employee", $connection);
while ($row = mysql_fetch_array($query)) {
echo "<b><a href="readphp.php?id={$row['employee_id']}">{$row['employee_name']}</a></b>";
echo "<br />";
}
?>

I am getting below Error:

Parse error: syntax error, unexpected 'readphp' (T_STRING), expecting ',' or ';'
4
  • Sorry Errors Indicating on this line echo "<b><a href="readphp.php?id={$row['employee_id']}">{$row['employee_name']}</a></b>"; Commented May 4, 2018 at 9:13
  • 1
    Further suggestion: Use mysqli_-functions. mysql_-functions are deprecated. Commented May 4, 2018 at 9:14
  • 1
    You are mixing PHP and HTML-Limiters ("). That's why it does not work. You Need to write the Syntax like echo '<a href="#">'; or echo "<a href='#'>"; Commented May 4, 2018 at 9:15
  • Well you see php is giving you error on parts it should not interpret at all. You should find why is so. Commented May 4, 2018 at 9:17

2 Answers 2

1

You have to escape " when inside other quotes ". Change :

echo "<b><a href="readphp.php?id={$row['employee_id']}">{$row['employee_name']}</a></b>";

To :

echo "<b><a href=\"readphp.php?id={$row['employee_id']}\">{$row['employee_name']}</a></b>";

Sign up to request clarification or add additional context in comments.

Comments

0

Change " to ' of href url. it conflicts with open comma.

echo "<b><a href='readphp.php?id={$row['employee_id']}'>{$row['employee_name']}</a></b>";


<?php
$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
$db = mysql_select_db("company", $connection); // Selecting Database
//MySQL Query to read data
$query = mysql_query("select * from employee", $connection);
while ($row = mysql_fetch_array($query)) {
echo "<b><a href='readphp.php?id={$row['employee_id']}'>{$row['employee_name']}</a></b>";
        echo "<br />";
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.