-1

Hello

i am writing an external api for my wordpress website . My Code Includes Three Files :

File 01 : Connect.php

class Connect{

    private $con ;
    public function __construct()
    {



    }

    public function ConnectDb()
    {
        $this->con = mysqli_connect("localhost","root","","my_mydb");
        mysqli_set_charset($this->con,"utf8");
        if (!$this->con)
        {
            die("Connection error: " . mysqli_connect_error());
        }
        return $this->con;
    }

}

File 02 : DbOperations.php

include_once ('Connect.php');
require_once ('../../wp-load.php');
class DbOperations{

    private $con ;
    public function __construct()
    {
        $dbCon = new Connect();
        $this->con = $dbCon->ConnectDb();
    }


    public function get_all_posts()
    {
        $query = "SELECT post_author,post_date,post_content,post_title,post_name,comment_count FROM wp_users ";
        $result = mysqli_query($this->con,$query,MYSQLI_USE_RESULT);
        $num = mysqli_num_rows($result);
        $allposts = array();
        if (!$num==0)
        {
            while ($row=mysqli_fetch_array($result))
            {
                $allposts['post_title'] = $row['post_title'];
                $allposts['post_author'] = $row['post_author'];
                $allposts['post_date'] = $row['post_date'];
                $allposts['post_content'] = $row['post_content'];
                $allposts['post_name'] = $row['post_name'];
                $allposts['comment_count'] = $row['comment_count'];
            }
            $allposts['error'] = false ;
            $allposts['message'] = "Succeed";


        }
        else
        {
            $allposts['error'] = true ;
            $allposts['message'] = "cant get all posts";
        }
        return $allposts;

    }


    public function isUserLogined($username,$password)
    {
        $query = "SELECT user_login,user_pass FROM wp_users WHERE user_login='".$username."'";
        $result = mysqli_query($this->con,$query);
        $nums = mysqli_num_rows($result);
        if (!$nums==0)
        {
            $row = mysqli_fetch_assoc($result);
            $user_login = $row['user_login'];
            $user_pass = $row['user_pass'];
            if (wp_check_password($password,$user_pass))
            {
                $loginResponse['error'] = false ;
                $loginResponse['message'] = "Succees mysqli_fetch_assoc Error";
                $loginResponse['user_login'] = $user_login;
                $loginResponse['user_pass'] = $user_pass;

            }
            else
            {
                $loginResponse['error'] = true ;
                $loginResponse['message'] = "wp_chech_password and mysqli_fetch_assoc Error";
                $loginResponse['user_login'] = "";
                $loginResponse['user_pass'] = "";
            }
        }
        else
        {
            $loginResponse['error'] = true ;
            $loginResponse['message'] = "Can not find user,Query Error in isUserLogined";
            $loginResponse['user_login'] = "";
            $loginResponse['user_pass'] = "";
        }

        return $loginResponse;


    }

}

File 03 : myApi.php

include ('DbOperations.php');

$dbOperate = new DbOperations();

$resultArray = $dbOperate->get_all_posts();

echo json_encode($resultArray);

i put this three files into directory : C:\xampp\htdocs\wp-includes\rest-api\

and the address to access it is : http://localhost/wp-includes/rest-api/myApi.php

but when i enter this in the address bar , i get the below result :

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\wp-includes\rest-api\DbOperations.php on line 25
{"error":true,"message":"cant get all posts"}

where am i wrong ? i searched but found nothing

Mojtaba Michael
  • 155
  • 3
  • 9

1 Answers1

0

try using

if (!$num|| mysqli_num_rows($num) == 0){

instead of

if (!$num==0){

also remove MYSQLI_USE_RESULT from $result

charan kumar
  • 2,114
  • 2
  • 19
  • 24