2011-08-23 7 views
3

데이터베이스에 연결하는 기능이 작동하지 않는 이유를 이해하지 못하고 있습니다. 변수에 실수가 있으면 트리플을 검사했습니다. 당신은PHP : 데이터베이스가 선택되지 않았습니다.

No database selected 

class.php

<?php 

class blog { 
    private $host; 
    private $username; 
    private $password; 
    private $db; 
    private $link; 

    public function __construct($host, $username, $password, $db){ 
    $this->link = mysql_connect($host, $username, $password, $db); 
    mysql_select_db($this->db, $this->link) or die (mysql_error()); 

    } 

    function get_content(){ 
    $sql = "SELECT * FROM content"; 
    $res = mysql_query($sql); 
    while($row = mysql_fetch_assoc($res)){ 
     echo '<h1>'.$row['title'].'</h1>'; 
     echo '<p>'.$row['body'].'</p>'; 
     } 
    } 
}// End of Class 
?> 

index.php를

<?php include 'includes/class.php' ?> 
<?php 


//Setup Connection 
$obj = new blog('localhost', 'root', '', 'blog'); 

//Connect to DB 

?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<link rel="stylesheet" type="text/css" href="css/style.css" mce_href="styles1.css"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Mi Blog</title> 
</head> 

<body> 

<div id="page-wrap"> 
    <?php $obj->get_content() ?> 
</div> 


</body> 
</html> 

답변

10

위치 :

나는 다음과 같은 오류가 라인 12 class.php에 mysql_error를 기능을 사용하여 $this->db을 사용하지만 결코 설정하지 마십시오. 이것을 시도하십시오 :

public function __construct($host, $username, $password, $db){ 
    $this->db = $db; 
    $this->link = mysql_connect($host, $username, $password, $db); 
    mysql_select_db($this->db, $this->link) or die (mysql_error()); 
} 
+0

커야한다, 고마워요! –

6

$this->db을 설정하지 못했습니다. 모든

mysql_select_db($this->db, $this->link) or die (mysql_error()); 

4

우선되어야한다 :

mysql_select_db($db, $this->link) or die (mysql_error()); 

$res = mysql_query($sql); 

$res = mysql_query($sql, $this->link); 
관련 문제