如何在不同页面(PHP)中调用获取查询函数

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (145)

我写了下面的代码,数据没有抓取。谁能帮我一下吗?大多数情况下,我是做过程编程的,所以我在OOPS方面没有那么多的知识。

1.xyzconfig.php

define("HOST", "localhost");            
define("USER", "root");             
define("PASSWORD", "");     
define("DATABASE", "end");           

2.dbconnect.php

include_once 'xyzconfig.php';   

$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);

if($mysqli->connect_error) 

{

header("Location: ../error.php?err=Unable to connect to MySQL");

exit();

}

3.Function.php

include_once 'xyzconfig.php';   

 function user_fetch($mysqli)

{

    if($stmt=$mysqli->prepare("SELECT id, username,email,role FROM members"))
    {
        $stmt->execute();   

        $stmt->store_result();

         if($stmt->num_rows == 1)

         {

          while($row=$stmt->fetch_assoc() )
          {

            echo "<tr><td>".$row["id"]."</td><td>".$row["username"]." ".$row["email"]." ".$row['role']."</td></tr>";

          }

        }    
    }}

4.test.php

include_once 'includes/db_connect.php';

include_once 'includes/functions.php';

echo user_fetch($mysqli);
提问于
用户回答回答于

php需要包含dbconnect.php而不是xyzconfig.php。

include_once 'dbconnect.php';   

 function user_fetch($mysqli)

{

    if($stmt=$mysqli->prepare("SELECT id, username,email,role FROM members"))
    {
        $stmt->execute();   

        $stmt->store_result();

         if($stmt->num_rows >= 1)

         {

          while($row=$stmt->fetch_assoc() )
          {

            echo "<tr><td>".$row["id"]."</td><td>".$row["username"]." ".$row["email"]." ".$row['role']."</td></tr>";

          }

        }    
    }}

php需要:

include_once 'includes/dbconnect.php';

include_once 'includes/functions.php';

echo user_fetch($mysqli);

扫码关注云+社区

领取腾讯云代金券