【问题标题】:php echo data from certain id来自某个id的php回显数据
【发布时间】:2012-11-16 09:22:13
【问题描述】:

编辑:对不起,我不清楚,这次我试着解释一下。

我在一个名为 tMenu 的数据库表中有这些数据:

id page_nl 文本
1 index_1 index1_text
2 索引_2 索引2_文本
3 index_3 index3_text

这些是我网站上的 3 个页面,在本例中称为 index_1、index_2 和 index_3。我已经编写了这样一种方式,每个页面都显示 index1_text。

我现在想要的是在菜单中显示 page_nl。我现在的代码是:

$qh = mysql_query('SELECT id, page_nl FROM tMenu ORDER BY id');
$row = mysql_fetch_array($qh);
$id = 'id';

<a href="index_1.php"><? echo $row['page_nl']; $id=="1" ;?></a>
<a href="index_2.php"><? echo $row['page_nl']; $id=="2" ;?></a>
<a href="index_3.php"><? echo $row['page_nl'];?></a>

现在它只显示来自 id 1 的 page_nl,但我希望下一个链接显示来自 id 2 的 page_nl。我希望我的问题现在更清楚了。

【问题讨论】:

    标签: php row echo


    【解决方案1】:

    你的问题不是很清楚——你是在问这样的问题吗

    $sql = "select * from yourtable where id = 1";
    $result = mysql_query($sql);
    //I am assuming there are more than 1 rows for ID 1
    while($row = mysql_fetch_assoc($result)) { 
    echo $row['page_nl'];
    }
    

    或者=============================

    $sql = "select * from yourtable"; //Select All
    $result = mysql_query($sql);
    while($row = mysql_fetch_assoc($result)) { 
        if($row['id'] == 1) 
        {
             echo $row['page_nl'];
        }
    }
    

    【讨论】:

      【解决方案2】:

      假设你的意思是数据库表,你需要一个例程来连接到数据库然后获取信息:

      <?php
      $mysqli = new mysqli("localhost", "my_user", "my_password", "databasename"); // database name
      
      /* check connection */
      if (mysqli_connect_errno()) {
          printf("Connect failed: %s\n", mysqli_connect_error());
          exit();
      }
      
      $query = "SELECT * FROM table_name"; // put table name here
      $result = $mysqli->query($query);
      
      /* numeric array */
      
      /* associative array */
      $row = $result->fetch_array(MYSQLI_ASSOC);
      printf ("%s (%s)\n", $row["id"], $row["page_nl"]);
      
      
      /* free result set */
      $result->free();
      
      /* close connection */
      $mysqli->close();
      ?>
      

      【讨论】:

        【解决方案3】:

        您需要使用foreach($var as $key =&gt;$value) 循环

        【讨论】:

        • 我需要放哪些变量而不是$var、$key和$value?使用上面的代码时
        猜你喜欢
        • 1970-01-01
        • 2017-06-03
        • 1970-01-01
        • 2016-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多