0

这是我用于连接我的数据库的 oop php 代码。但是我该怎么做这个SELECT功能。我想在我的页面中显示来自数据库的数据。帮我解决这个问题

class createConnection //create a class for make connection
{
    var $host = "localhost";
    var $username = "root"; // specify the sever details for mysql
    var $password = "";
    var $database = "akpGroup";
    var $myconn;

    function connectToDatabase() // create a function for connect database
    {
        $conn = mysql_connect($this->host, $this->username, $this->password);
        if (!$conn) // testing the connection
        {
            die ("Cannot connect to the database");
        } else {
            $this->myconn = $conn;
            echo "Connection established";
        }

        return $this->myconn;
    }

    function selectDatabase() // selecting the database.
    {
        mysql_select_db($this->database); //use php inbuild functions for select database
        if (mysql_error()) // if error occured display the error message
        {
            echo "Cannot find the database " . $this->database;
        }
        echo "Database selected..";
    }

    function closeConnection() // close the connection
    {
        mysql_close($this->myconn);
        echo "Connection closed";
    }
4

2 回答 2

0

May be something like this

$link = mysql_connect('localhost','root','yourPassword')
mysql_select_db('database_name', $link);
$sql = 'SELECT column FROM tablename';
$result = mysql_query($sql, $link) or die(mysql_error());
$row = mysql_fetch_assoc($result);
print_r($row);
于 2013-08-30T10:25:45.700 回答
0

首先考虑使用 mylsqi_ 或 PDO 函数。

基本上,您的查询函数应该类似于下面的函数。它进行查询并将所有结果放入数组中。

您可以像在其他方法中一样返回数组或将其分配给类变量。

  function query($sql) {
        $result_array = array();
        $query = mysql_query($sql);
        if(mysql_affected_rows() > 0) {
              while($fetch = mysql_fetch_assoc($query)) {
                    $result_array[] = $fetch;
              }
        }
        else {
              //no results action if needed
        }

        return $result_array;
  }
于 2013-08-30T10:54:57.537 回答