无法调用PHP函数

时间:2015-03-30 18:47:25

标签: php database function mysqli

我在这里为这段代码创建了不同的文件。在一个我有数据库连接,在另一个我将在数据库和我想要调用的最后一个或在第二个文件中使用各种查询的所有查询。 当试图调用getData()时,由于某种原因我得不到结果。 另外在我的第二个文件中,因为我的所有代码都包含在一个包含函数的类中,函数没有运行。如果我拿出课程和功能并通过它自己保留代码,它就会运行。有什么建议?

谢谢

<?php

class DB 
{

protected $db_name = 'Users';
protected $db_user = 'root';
protected $db_pass = 'root';
protected $db_host = 'localhost';

public function connect() 
{

    $connect_db = new mysqli( $this->db_host, $this->db_user, $this->db_pass,             

$this->db_name );

    if ( mysqli_connect_errno() ) {
        printf("Connection failed: %s\ ", mysqli_connect_error());
        exit();
    }

    return $connect_db;
}
}

?>


<?php

class database_queries
{

include_once ('db_conn.php');
$db = new DB();
$link = $db->connect();


public function getData()
    {
        $query = "SELECT * FROM users";
        if ($result = mysqli_query($link, $query)) 
        {
            while ($row = mysqli_fetch_assoc($result)) 
            {
                    $row = array_map('stripslashes', $row);
                    $push[] = $row;
            }
        }
    return $push;   
    }
}

?>


<?php


$me = new database_queries();
$array = $me->getData();

echo "<table border= 1px><br>"; // start a table tag in the HTML
echo "<tr>
        <th>ID</th>
        <th>Password</th>
        <th>First Name</th>  
        <th>Last Name</th>
       </tr>";

foreach ($array as $row) 
{
echo 
"<tr>
    <td>" . $row['user_id'] . "</td>
    <td>" . $row['user_password'] . "</td>
    <td>" . $row['user_first_name'] .  "</td>
    <td>" . $row['user_last_name'] .  "</td>
</tr>";  //$row['index'] the index here is a field name
}   
echo "</table>";

?>

0 个答案:

没有答案