简体   繁体   English

使用mysqli在表格中显示结果

[英]displaying results in a table using mysqli

so my friend just changed my php so I'm using mysqli and after slight modifications to my code I still receive: Fatal error: Call to undefined method mysqli::fetch_object() in C:\\wamp\\www\\web2\\database.php on line 44 所以我的朋友刚刚更改了我的php,所以我使用了mysqli,经过对我的代码的轻微修改,我仍然收到:致命错误:在C:\\ wamp \\ www \\ web2 \\ database.php中调用未定义的方法mysqli :: fetch_object()在44号线上

   <?php
class Database 
{ 
 public $server = "localhost";
 public $database = "database"; 
 public $user = "root"; 
 public $password = ""; 
 public $row;
 public $result;
 public $sqlExtra = " ORDER BY firstname ASC";
 public $dbLocalHost;





 //call connection method upon constructing 
 public function __construct(){
  $this->createConnection(); 
 }

 //connection to the database
 public function createConnection() 
    { 
     $this->dbLocalhost = new mysqli($this->server, $this->user, $this->password, $this->database)
                or die("could not connect:");


      //mysql_select_db($this->database)
       //
       //  or die("Could not find the database: ".mysql_error());



 } 

 //execute query string 
 public function query($queryString) 
    {


        $result = $this->dbLocalhost->query($queryString);

        while($row = $this->dbLocalhost->fetch_object($result))
        {
            echo "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td></tr>";
        }

    } 

    public function newRecord($fname, $lname)
    {
        $firstname = $fname;
        $lastname = $lname;

        $this->emptyCheck($firstname, $lastname);
        $this->insert($firstname, $lastname);
    }

    function insert($fname, $lname)
    {
        $insert = "INSERT INTO table (firstname, lastname)
            VALUES ('$fname','$lname')";
        mysql_query($insert)
        or die("could not insert:".mysql_error());
        header('Location: blah.php');

    }

    function emptyCheck($fname, $lname)
    {
        if ($fname == "" || $lname == "")
        {
            echo "Please fill in all the fields.";
        }
    }


} 

?>

and this page displays it: 这个页面显示它:

  <?php

include('database.php');
$db = new Database();
$sql = "SELECT * 
    FROM tables"; 

if ($_GET['sort'] == 'id')
{
    $sql .= " ORDER BY id";
}
elseif ($_GET['sort'] == 'fn')
{
    $sql .= " ORDER BY firstname";
}
elseif ($_GET['sort'] == 'ln')
{
    $sql .= " ORDER BY lastname";
}






?>

<html>
<body>


    <table>
  <tr>
    <td>

      <table border="1" align="center">
      <tr>
        <th><a href="blah.php?sort=id">ID</a></th>
        <th><a href="blah.php?sort=fn">First Name</a></th>
        <th><a href="blah.php?sort=ln">Last Name</a></th>
        <th>Edit</th>
        <th>Delete</th>
      </tr>
<?php
$db->query($sql);
?>
      </table>
</table>
    <a Href="new.php">Add new Record</a>

</body>
</html>

Sorry if this is really obvious, I'm terrible at webdev and new to mysqli. 对不起,如果这是非常明显的,我在webdev和mysqli的新手很糟糕。 Thanks. 谢谢。

You should use fetch_object on the result of the query and not on your database connection: 您应该对查询结果使用fetch_object而不是数据库连接:

$result = $this->dbLocalhost->query($queryString);

while($row = $result->fetch_object())    // here

This line while($row = $this->dbLocalhost->fetch_object($result)) needs to be 这行while($row = $this->dbLocalhost->fetch_object($result))需要

 $result = $this->dbLocalhost->query($queryString);
 while($row = $result->fetch_object())

Also, the insert function uses mysql, instead of mysqli . 此外, insert函数使用mysql而不是mysqli

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

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