将mysql更改为mysqli

时间:2014-08-05 09:27:50

标签: php mysqli comments

我总是得到这个错误,任何帮助。谢谢。

1 - Undefined variable: db_conx

2- mysqli_query() expects parameter 1 to be mysqli, null given

我的代码:

include_once("db_conx.php");
require_once ("users.php"); 
class Comments{
public static function getComments( )       
{
    $output = array();
    $sql = "SELECT * FROM comments order by comment_id desc";
    $query = mysqli_query( $db_conx, $sql );
    if( $query )
    {
        if( mysqli_num_rows( $query ) > 0 )
        {
            while( $row = mysqli_fetch_object( $query ) )
            {
                $output[] = $row;
                }
            }
        }
        return $output;
    }
public static function insert( $comment_txt , $id )
{
    $comment_txt = addslashes( $comment_txt );
    $sql = "insert into comments values( '' , '$comment_txt' , $id )";
    $query = mysqli_query($db_conx, $sql);
    if( $query )
    {

................. ,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,

1 个答案:

答案 0 :(得分:0)

您确实意识到您的查询是在一个函数中,并且您的数据库连接不是。 这就是你能做的。在函数

中将变量$db_conx声明为global
include_once("db_conx.php");
require_once ("users.php"); 
class Comments{
public static function getComments( )       
{
    global $db_conx;

    $output = array();
    $sql = "SELECT * FROM comments order by comment_id desc";
    $query = mysqli_query( $db_conx, $sql );
    ...

您可以阅读全球范围here