简体   繁体   English

一个PHP函数来处理不同的MySQLi select语句

[英]one PHP function to handle different MySQLi select statements

I have two MySQL tables with number of columns. 我有两个带有列数的MySQL表。 The table structure is given below, 表格结构如下:

1.pictures 1.图片
postedON 发表于
caption 字幕
imageName imageName
thumbName thumbName
imageLocation imageLocation
thumbLocation thumbLocation

2.Videos 2.视频
postedOn 发表于
category 类别
Link 链接

I am using the folowing PHP function to fetch data from DB using a select command. 我正在使用以下PHP函数,使用select命令从数据库中获取数据。

    function select($table){
    if($this->db_connection){
        $query = 'SELECT * FROM '. $table;

        $result = mysqli_query($this->db_connection,$query) or die($this->db_connection->error);
        //print_r($result);
        //echo "Affected rows: " . mysqli_affected_rows($this->db_connection);
        //var_dump($result);
        echo "<table>";
        echo "<tr>";
        echo "<th>Date Posted</th>";
        echo "<th>Category</th>";
        echo "<th>Link</th>";
        echo "</tr>";
        while($row = $result->fetch_assoc()){
            echo "<tr>";
            echo "<td>" . $row['postedOn'] . "</td>";
            echo "<td>".$row['category']. "</td>";
            echo "<td><a href=".$row['link'] .">" . $row['link'] . "</a></td>";
            echo "</tr>";
        }

            echo "</table>";
    }else{
        echo "db_connection is = " . $this->db_connection;
    }
}

} }

The problem with this function as you can see, it can only serve only one table and not dynamic. 如您所见,此功能存在问题,它只能服务一个表,不能动态服务。 Can someone please explain the way to dynamically fetch data from different table with different number of columns using only one PHP function? 有人可以解释仅使用一个PHP函数从具有不同列数的不同表中动态获取数据的方法吗? Thanks 谢谢

Try using mysqli_fetch_fields() 尝试使用mysqli_fetch_fields()

<?php
function select($table){
  if($this->db_connection){
    $query = 'SELECT * FROM '. $table;

    $result = mysqli_query($this->db_connection,$query) or die($this->db_connection->error);
    $fieldinfo = mysqli_fetch_fields($result);
    echo "<table>";
    echo "<tr>";
    foreach ($fieldinfo as $val)
    {
        echo "<th>".$val->name."</th>";
    }
    echo "</tr>";
    while($row = $result->fetch_assoc()){
        echo "<tr>";
        foreach ($fieldinfo as $val)
        {
           echo "<td>" . $row[$val->orgname] . "</td>";
        }            
        echo "</tr>";
    }

    echo "</table>";
  }else{
     echo "db_connection is = " . $this->db_connection;
  }
}

It could be hard to explain given all the wrong premises you approach is based on, but I'll try. 考虑到您所基于的所有错误前提,可能很难解释,但我会尽力而为。

First of all, you have to understand that a query like SELECT * FROM table has a very little use, next to none. 首先,您必须了解像SELECT * FROM table这样的查询几乎没有用处,几乎没有用处。 Most of time you are always have some WHERE or at least LIMIT clause. 大多数情况下,您总是会有一些WHERE或至少具有LIMIT子句。 So, such a function will have no use at all. 因此,这种功能根本没有用。

Next, you have to learn by heart that database interaction should never be intermixed with any output stuff like HTML. 接下来,您必须认真地了解到数据库交互绝不能与任何输出内容(例如HTML)混合在一起。

Given these two premises above, your function should accept a fill qualified query as a parameter and return an array with data as a result. 鉴于以上两个前提,您的函数应接受符合填充条件的查询作为参数,并返回包含数据的数组。

For which array, in turn you can write a helper function to display its contents in the form of HTML table. 对于哪个数组,您可以编写一个辅助函数,以HTML表格的形式显示其内容。

But again, such a generalized output function will be of little use as well, because, as you can see from your own example, different fields will need different formatting. 但是同样,这种通用输出函数也几乎没有用,因为,正如您从自己的示例中看到的那样,不同的字段将需要不同的格式。 So it's better to write output each time by hand. 因此最好每次都手动写输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM