Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in E:\Work\Aptech\XAmpp\htdocs\myproject\rest\models\Post.php:45 #13

Open
vuivehieu opened this issue May 17, 2021 · 1 comment

Comments

@vuivehieu
Copy link

vuivehieu commented May 17, 2021

I'm having this trouble, any suggests?
Thank you for the codes

Post.php
`<?php
class Post{
//DB
private $conn;
private $table = 'posts';

//post properties
public $id;
public $category_id;
public $category_name;
public $title;
public $body;
public $author;
public $created_at;

//constructor
public function __construct($db)
{
	$this->conn = $db;
}
//get posts

public function read(){
	//creat query
	$query = 'SELECT 
	C.name as category_name,
    p.id,
    p.category_id,
    p.title,
    p.body,
    p.author,
    p.created_at    
    FROM
    ' .$this->table. ' p
    LEFT JOIN 
     categories c ON p.category_id = c.id
     ORDER BY 
     p.created_at DESC';


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

     //execute
     $stmt->execute();

     return $stmt;
}

}`

Database.php
`<?php
/**
*
*/
class Database
{
private $host = 'localhost';
private $db_name = 'testt';
private $username = 'root';
private $password = '';
private $conn;

//DB connect
public function connect()
{
	$this->conn = null;

	try{
		$this->conn = new PDO('mysql:host='.$this->host.';dbname ='.$this->db_name,$this->username, $this->password);
		$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	}
	catch(PDOException $e){
		echo 'Connection Error: '.$e->getMessage();
	}
	return $this->conn;
}

}
?>`

read.php
`<?php
//Headers
header('Access-Control-Allow-Origin: *');
header('Content-Type:application/json');

include_once 'E:/Work/Aptech/XAmpp/htdocs/myproject/rest/Database.php';
include_once 'E:/Work/Aptech/XAmpp/htdocs/myproject/rest/models/Post.php';

//Install DB & connect
$database = new Database();
$db = $database->connect();

//Instantiate Post object
$post = new Post($db);

//Blog post query
$result = $post->read();
//Get row count
$num = $result->rowCount();

//check if any posts
if($num>0)
{
//post array
$posts_arr = array();
$posts_arr['data']=array();

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

	$post_item = array(
    'id'=>$id,
    'title'=>$title,
    'body'=> html_entity_decode($body),
    'author'=>$author,
    'category_id'=>$category_id,
    'category_name'=> $category_name
	);

    //push to "data"
    array_push($posts_arr['data'], $post_item);
}

//turn to JSON &output
echo json_encode($posts_arr);

}else{
//no post
echo json_encode(
array('message'=>'No Posts Found')
);
}
`

@Omdal
Copy link

Omdal commented Aug 10, 2021

I had the same error. Removing a misplaced space worked for me:

Database.php in function connect:

try{
    $this->conn = new PDO('mysql:host='.$this->host.';dbname ='.$this->db_name,$this->username, $this->password);
                                       // Remove this space ^

the corrected line should be:
$this->conn = new PDO('mysql:host='.$this->host.';dbname='.$this->db_name,$this->username, $this->password);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants