如何使用php在特定位置回显特定的mysql数据?

如何使用php在特定位置回显特定的mysql数据?,mysql,Mysql,如果“SELECT”语句用于从数据库中选择数据,那么我们如何使用php将特定行回送到页面上的特定位置 为了更好地解释这一点,我尝试从一个表中选择*ALL,但使用php将多行回送到html页面上的特定位置 所以,想象一下,我的整个标记和css在一个页面上有20个缩略图,每个缩略图都有数据和图像,每个缩略图都是唯一的……我必须复制下面的20次吗 我认为最好的方法是使用这个语句(这可能是完全错误的) 从ID=4>>>的表格的名称中选择*即我希望回显特定数据的位置 那么,如果我有20个缩略图,我会做20

如果“SELECT”语句用于从数据库中选择数据,那么我们如何使用php将特定行回送到页面上的特定位置

为了更好地解释这一点,我尝试从一个表中选择*ALL,但使用php将多行回送到html页面上的特定位置

所以,想象一下,我的整个标记和css在一个页面上有20个缩略图,每个缩略图都有数据和图像,每个缩略图都是唯一的……我必须复制下面的20次吗

我认为最好的方法是使用这个语句(这可能是完全错误的)

从ID=4>>>的表格的名称中选择*即我希望回显特定数据的位置

那么,如果我有20个缩略图,我会做20次吗

    <?php 
 // Connects to your Database 
 mysql_connect("localhost", "username", "password") or die(mysql_error()); 
 mysql_select_db("Database_Name") or die(mysql_error()); 
 $data = mysql_query("SELECT * FROM name_of_table WHERE ID = 4;") 
 or die(mysql_error()); 
 Print "<table border cellpadding=3>"; 
 while($info = mysql_fetch_array( $data )) 
 { 
 Print "<tr>"; 
 Print "<th>Name:</th> <td>".$info['name'] . "</td> "; 
 Print "<th>Product:</th> <td>".$info['product_name'] . " </td></tr>"; 
 } 
 Print "</table>"; 
 ?> 
这样做的最佳方式是什么


谢谢

简单的例子。。首先获取想要的ID为s的数据。为数据请求创建函数

<?php 
     // Connects to your Database 
     mysql_connect("localhost", "username", "password") or die(mysql_error()); 
     mysql_select_db("Database_Name") or die(mysql_error()); 
     $data = mysql_query("SELECT * FROM name_of_table WHERE ID IN (2,3,4,5,6);") 
     or die(mysql_error()); 

     // This holds all data rows
     $data_array = array(); 

     while($info = mysql_fetch_array( $data )) 
       $data_array[] = $data;

     // Function for rendering data to html 
     function getItemHtml($id) {

       $html = "";

       foreach($data_array as $row) {
          if ($row['ID'] == $id) {
            $html = "<td>" . $row['title'] . "</td>";
            // etc.. create item html here
            break;
          }
       }

       return $html;
     }

     // To create one item just call this with item id.

     echo getItemHtml(4); 

?> 


分离模型。将所有数据读入一个数组——也就是说,将SQL放在函数后面,这样只返回相关数据的数组。然后为每个数组项发出适当的HTML输出,例如在循环中。如果没有其他连接,则在中使用
(指定相关ID)可能是合适的,但如果图像是相关容器(如图库或产品列表)的一部分,则通常可以使用其他连接或选择。所以不,你什么都做一次。谢谢这个快速的问题-为什么$html=”“是空的?它只是初始化代码。。就像找不到传递id的行一样,函数返回空字符串。
<?php 
     // Connects to your Database 
     mysql_connect("localhost", "username", "password") or die(mysql_error()); 
     mysql_select_db("Database_Name") or die(mysql_error()); 
     $data = mysql_query("SELECT * FROM name_of_table WHERE ID IN (2,3,4,5,6);") 
     or die(mysql_error()); 

     // This holds all data rows
     $data_array = array(); 

     while($info = mysql_fetch_array( $data )) 
       $data_array[] = $data;

     // Function for rendering data to html 
     function getItemHtml($id) {

       $html = "";

       foreach($data_array as $row) {
          if ($row['ID'] == $id) {
            $html = "<td>" . $row['title'] . "</td>";
            // etc.. create item html here
            break;
          }
       }

       return $html;
     }

     // To create one item just call this with item id.

     echo getItemHtml(4); 

?>