【问题标题】:How to call a fetch query function in different page(PHP)如何在不同的页面中调用获取查询函数(PHP)
【发布时间】:2018-11-13 03:06:49
【问题描述】:

我写了以下代码,数据无法获取。谁能帮帮我。我主要做过程编程,所以我对 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 function oop mysqli


    【解决方案1】:

    functions.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>";
    
              }
    
            }    
        }}
    

    test.php 需要:

    include_once 'includes/dbconnect.php';
    
    include_once 'includes/functions.php';
    
    echo user_fetch($mysqli);
    

    既不使用 store_result() 也绝对没有意义

    【讨论】:

    • 我做到了,但仍然无法获取
    • 然后检查你的 SQL
    猜你喜欢
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    相关资源
    最近更新 更多