将mysql修改为mysqli



我一直得到这个错误,任何帮助。谢谢你。

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 )
    {

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

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

中将变量$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 );
    ...

你可以在这里阅读全局作用域

最新更新