0

i'm new to pdo because i'm habitual to using mysql procedural so i have made a conn.php in which i'm having database connectivity

<?php
class Connection{
private $localhost="xxxx";
private $username="xxxx";
private $dbname="xxxx";
private $password="";
public $conn="";
function __construct(){

    $this->connect();   

}

function connect(){
try {
    $this->conn = new PDO("mysql:host=$this->localhost;dbname=$this->dbname", $this->username, $this->password);
    // set the PDO error mode to exception
    $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }}
}
?>

and including it in another file named function.php

<?php
include("conn.php");//here i'm including connection
Class Query{
    function _construct(){
        $this->select();
    }
    function select(){
$sql = "SELECT * FROM demotable";   
    $result = $conn->query($sql);
    if($result->rowCount() > 0){
        echo "<table>";
            echo "<tr>";
                echo "<th>id</th>";
                echo "<th>first_name</th>";
                echo "<th>last_name</th>";
                echo "<th>email</th>";
            echo "</tr>";
        while($row = $result->fetch()){
            echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['first_name'] . "</td>";
                echo "<td>" . $row['last_name'] . "</td>";
                echo "<td>" . $row['email'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        // Free result set

    } else{
        echo "No records matching your query were found.";
    }
}
}
?>

but my problem is that it isnt showing anything no error no output nothing

Raju Ghosh
  • 19
  • 4
  • `$conn` isn't defined in the scope of the `select()` method. – Nigel Ren Feb 08 '18 at 09:42
  • i dont how to change this code can anyone help and it is not about error reporting – Raju Ghosh Feb 08 '18 at 09:50
  • You explicitly wrote *"my problem is that it isn't showing anything no error no output nothing"*. That is indeed a common issue when you're relatively new to PHP development, and it makes debugging really hard. So I hope I interpreted your call correctly, and you do indeed want to become a better developer who is able to debug his own code at least to a certain extent. The question I linked is certainly going to help, and if you can't solve your (next) issue, you can post a new question that shows the actual error messages you will be getting, as well as the things you've tried to solve them. – GolezTrol Feb 08 '18 at 09:55

0 Answers0