douduoting8408 2016-08-22 14:19
浏览 37
已采纳

需要连接php和mysql [关闭]

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

  • 写回答

3条回答 默认 最新

  • doutan1637 2016-08-22 14:25
    关注
    <?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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思