<?php
if(isset($_POST['Search']))
{
//Database connection
$num = $_POST['num'];
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error
connecting to mysql');
$dbname = 'vasuki';
mysql_select_db($dbname);
$Result = mysql_query("SELECT id, name, age FROM details WHERE id =
'$num'");
while($row = mysql_fetch_array($Result))
{
$name = $row['name'] ;
$age = $row['age'];
//Creating table
echo "<div style='top: 273px;
margin-left: 60px;
position: absolute;left: 30px;'>
<table border='1'><tr><th>name</th>
<th> age </th></tr>
//Connect to Database to fetch the data
<td>echo'<?php $name ?>;'</td>
<td>echo'<?php $age ?>;'</td>
</table></div>";
}
I have a data in my xampp database.
In front end using php I need to search the data and find the data from database.
I just pasted only php code here.I hope I did some mistake in above the code.
I have a doubt on this below code :
<td>echo'<?php $name ?>;'</td>
Is this a corrct syntax?.
Because I am getting error in above the line.
I am using it inside echo cmd so I have a doubt. Kindly clarify
<?php
if(isset($_POST['Search']))
{
//Database connection
$num = $_POST['num'];
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error
connecting to mysql');
$dbname = 'vasuki';
mysql_select_db($dbname);
$Result = mysql_query("SELECT id, name, age FROM details WHERE id =
'$num'");
while($row = mysql_fetch_array($Result))
{
$name = $row['name'] ;
$age = $row['age'];
//Creating table
echo "<div style='top: 273px;
margin-left: 60px;
position: absolute;left: 30px;'>
<table border='1'><tr><th>name</th>
<th> age </th></tr><tr>
<td> $name </td>
<td> $age </td></tr>
</table></div>";
}
?>
You already started echo keyword and all html code are inside of (") and (") then you can not write echo inside echo and PHP automatically understand $identifier as a variable.
Another way for same result is:
echo "<div style='top: 273px;
margin-left: 60px;
position: absolute;left: 30px;'>
<table border='1'><tr><th>name</th>
<th> age </th></tr><tr><td>".$name."</td><td>".$age."</td></tr></table></div>";
Here dot (.) is used for concatenate.
Or you can do this as well:
echo "<div style='top: 273px;
margin-left: 60px;
position: absolute;left: 30px;'>
<table border='1'><tr><th>name</th>
<th> age </th></tr><tr><td>";
echo $name;
echo "</td><td>";
echo $age;
echo "</td></tr></table></div>";
Do what you feel easy.
Change echo'<?php $name ?>;'
and echo'<?php $age ?>;'
to {$name}
and {$age}
.
Yes, your doubts are correct, this line is completely wrong:
echo "<div style='top: 273px;
[..snip..]
<td>echo'<?php $name ?>;'</td>
PHP is not recursively executable. That <?php
is NOT a php code tag, because it's already inside a PHP string started back on that echo
line.
Even if it WASN'T inside this string, and was proper PHP code, it'd do nothing.
<?php $foo; ?>
just says "here's a var", and php goes. "Gee, thanks", and moves on. You need to echo that var:
<?php echo $foo; ?>
to get that variable's value as output.
Since you're in a string already, all you need is
echo "<div etc...
<td>$name</td>
etc...
";
Note the LACK of any php code, other than the variable itself.