0

UPDATE: There was no issue with the code used in counting. There was just a typo error in connecting to database

$this->conn = new PDO("mysql:host=". $this->host . ";db_name=" . $this->db_name, $this->username, $this->password);

the db_name should be dbname. So its something like

$this->conn = new PDO("mysql:host=". $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);

I am try a PHP REST api with mysql database but this doesn't seem to be working as i have tried to select from my database as the first step but this returns no result despite there being a record in the database

Below are my codes

database.php

<?php

class Database
{
    //Database Credentials
    private $host = "localhost";
    private $username = "root";
    private $password = "password";
    private $db_name = "rtmdb";

    public $conn;

    //Get Database Connection
    public function getConnection()
    {
        $this->conn = null;

        //Try connection
        try
        {
            $this->conn = new PDO("mysql:host=". $this->host . ";db_name=" . $this->db_name, $this->username, $this->password);
            $this->conn->exec("set names utf8");
        }
        catch(PDOException $exception)
        {
            echo "Connection error:" . $exception->getMessage();
        }

        return $this->conn;
    }
}

admin.php to select from database

<?php

class Admin
{
    //Database Connection and Table name
    private $conn;
    private $table_name = "admin";

    //object properties
    public $id;
    public $fname;
    public $lname;
    public $oname;
    public $uname;
    public $email;
    public $idnumber;
    public $password;
    public $profimage;
    public $datecreated;
    public $modifiedby;
    public $datemodified;
    public $status;

    //Construct $db as database connection

    public function __construct($db)
    {
        $this->conn = $db;
    }
    public function crawl()
    {
        $query = "SELECT * FROM " . $this->table_name; 

        $stmt = $this->conn->prepare($query);

        $stmt->execute();

        return $stmt;
    }
}

crawl.php file to get data and json_encode results

//Include database and object files

include_once "../config/database.php";
include_once "../objects/admin.php";

$database = new Database();
$db = $database->getConnection();

$admin = new Admin($db);

//Query Admin
$stmt = $admin->crawl();
$num = $stmt->rowCount();

echo json_encode($num);
if($num > 0)
{

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        extract($row);

        $admin_list = array(
            "id" => $id,
            "uname" => $uname
        );

        array_push($admin_arr['records'], $admin_list);

    }

    echo json_encode($admin_arr);
}
else
{
    echo json_encode(
        array("message" => "No registered admin found.")
    );
}

But i get rowCount() as 0 and "no registered admin" despite having records

Below are screenshots of my table structures

the database structure

enter image description here

I also have only one data in the database

enter image description here

But these seem to give me no result. Please what the issue and how do i fix?

Community
  • 1
  • 1
diagold
  • 493
  • 1
  • 7
  • 28
  • 3
    **Too much code**. You need to do a better job of troubleshooting this yourself. We are *not* debuggers. You need **isolate the problem** and debug from there. If you're stuck provide a **clear explanation of what isn't working** with a [**Minimal, Complete, and Verifiable example**](//stackoverflow.com/help/mcve). I suggest reading **[ask]** a good question and **[the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)**. Also, be sure to take the **[tour]** and read **[this](//meta.stackoverflow.com/q/347937/)**. – John Conde Jul 23 '18 at 16:51
  • ["PDOStatement::rowCount() returns the number of rows affected by a DELETE, INSERT, or UPDATE statement."](http://php.net/manual/en/pdostatement.rowcount.php) – iainn Jul 23 '18 at 16:53
  • Possible duplicate of [Row count with PDO](https://stackoverflow.com/questions/883365/row-count-with-pdo) – iainn Jul 23 '18 at 16:54
  • issue was found in a typo. I typed `db_name` instead of `dbname` in database connection. Thanks – diagold Jul 23 '18 at 17:09

0 Answers0