2012-11-14 3 views
1

그래서 내 친구가 방금 내 PHP를 변경 했으므로 mysqli를 사용하고 있으며 약간의 수정을 한 후에도 여전히 나는 다음과 같이 나타납니다 : 치명적인 오류 : C의 mysqli :: fetch_object : \ WAMP \ www가 \ Web2입니다 \ database.php 라인에 44는 mysqli를 사용하는 테이블에 결과를 표시합니다

<?php 
class Database 
{ 
public $server = "localhost"; 
public $database = "database"; 
public $user = "root"; 
public $password = ""; 
public $row; 
public $result; 
public $sqlExtra = " ORDER BY firstname ASC"; 
public $dbLocalHost; 





//call connection method upon constructing 
public function __construct(){ 
    $this->createConnection(); 
} 

//connection to the database 
public function createConnection() 
    { 
    $this->dbLocalhost = new mysqli($this->server, $this->user, $this->password, $this->database) 
       or die("could not connect:"); 


     //mysql_select_db($this->database) 
     // 
     // or die("Could not find the database: ".mysql_error()); 



} 

//execute query string 
public function query($queryString) 
    { 


     $result = $this->dbLocalhost->query($queryString); 

     while($row = $this->dbLocalhost->fetch_object($result)) 
     { 
      echo "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td></tr>"; 
     } 

    } 

    public function newRecord($fname, $lname) 
    { 
     $firstname = $fname; 
     $lastname = $lname; 

     $this->emptyCheck($firstname, $lastname); 
     $this->insert($firstname, $lastname); 
    } 

    function insert($fname, $lname) 
    { 
     $insert = "INSERT INTO table (firstname, lastname) 
      VALUES ('$fname','$lname')"; 
     mysql_query($insert) 
     or die("could not insert:".mysql_error()); 
     header('Location: blah.php'); 

    } 

    function emptyCheck($fname, $lname) 
    { 
     if ($fname == "" || $lname == "") 
     { 
      echo "Please fill in all the fields."; 
     } 
    } 


} 

?> 

는이 페이지를 표시이 정말 명백한 경우, 나는이 WebDev과 새에 끔찍 해요

<?php 

include('database.php'); 
$db = new Database(); 
$sql = "SELECT * 
    FROM tables"; 

if ($_GET['sort'] == 'id') 
{ 
    $sql .= " ORDER BY id"; 
} 
elseif ($_GET['sort'] == 'fn') 
{ 
    $sql .= " ORDER BY firstname"; 
} 
elseif ($_GET['sort'] == 'ln') 
{ 
    $sql .= " ORDER BY lastname"; 
} 






?> 

<html> 
<body> 


    <table> 
    <tr> 
    <td> 

     <table border="1" align="center"> 
     <tr> 
     <th><a href="blah.php?sort=id">ID</a></th> 
     <th><a href="blah.php?sort=fn">First Name</a></th> 
     <th><a href="blah.php?sort=ln">Last Name</a></th> 
     <th>Edit</th> 
     <th>Delete</th> 
     </tr> 
<?php 
$db->query($sql); 
?> 
     </table> 
</table> 
    <a Href="new.php">Add new Record</a> 

</body> 
</html> 

죄송합니다

mysqli. 감사합니다. .

+0

구현 변화 이하,이 이제 오류 준다 : 치명적인 오류 : C의 배열로 입력 stdClass의 물체를 사용할 수 없습니다 \ WAMP \ WWW \ Web2입니다 \ database.php 라인 46 – normower

+0

가 변경됨 에코 " ". $ 로우 -> ID입니다." "$ 로우 -> FIRSTNAME.." "$ 로우 -> LASTNAME.." "; – normower

답변

2

당신은 당신의 데이터베이스 연결에 쿼리의 결과에 fetch_object를 사용하지 않아야

$result = $this->dbLocalhost->query($queryString); 

while($row = $result->fetch_object()) // here 
+1

이것은 다음과 같은 오류가 발생했습니다 : 치명적인 오류 : 46 행의 C : \ wamp \ www \ web2 \ database.php에 stdClass 유형의 객체를 사용할 수 없습니다. 은 ". $ row [0 ]. " ? – normower

+0

@normower 예, 결과를 객체에 저장하므로 각 열에'$ row-> COLUMN_NAME'이 필요합니다. – jeroen

1

필요 while($row = $this->dbLocalhost->fetch_object($result))이 줄은

$result = $this->dbLocalhost->query($queryString); 
while($row = $result->fetch_object()) 
또한

insert 기능은 MySQL을 사용하는 대신이 될 수 있습니다 mysqli입니다.

관련 문제