2011-04-07 233 views
1

代碼不起作用的因素是什麼?執行sql命令

<?php 
class mysql 
{ 
    var $user; 
    var $password; 
    var $database; 
    var $host; 
    var $out; 
    var $query; 

    function mysql($username, $password, $database, $host, $query) 
    { 
     $this->user = $username; 
     $this->password = $password; 
     $this->database = $database; 
     $this->host = $host; 
     $this->query = $query; 
    } 
    function connect() 
    { 
     $this->out = mysql_connect($this->host, $this->user, $this->password) 
       or die("Error cannnot connect to mysql server"); 
       echo "Connected successfully to Mysql server"; 
     mysql_select_db($this->database) or die("Cannot select db"); 

    } 
    function execution() 
    { 
     $re = mysql_query($this->query); 
     while($row = mysql_fetch_array($re)) 
      echo $row; 
    } 
    function out() 
    { 
     mysql_close($this->out); 
    } 

} 
$connect = new mysql('root','','test','127.0.0.1'); 
$connect->connect(); 
$connect->execution('test','SELECT * FROM test'); 
$connect->out; 


?> 

更新

$connect = new mysql('root','','test','127.0.0.1','SELECT ss from test'); 

我已經嘗試上面的代碼及其不要返回任何結果..

SS包含列名ID及其包含的11個值。

+0

請修改您的問題以提供更多信息。此外,您可以在您提出的任何問題的任何答案下使用評論功能。請花些時間閱讀[faq](http://stackoverflow.com/faq)。 – 2011-04-07 18:44:59

回答

1

你的構造應該是new mysql('root','','test','127.0.0.1', 'SELECT * FROM test');

3

它看起來像一對夫婦的函數調用正在傳遞的參數不正確的量。你的mysql()構造函數應該取5,但你只給它4。另外,執行()不會取任何,並且你正在試圖給它2。

1

你缺少$query參數在你的mysql()撥打:

$connect = new mysql('root','','test','127.0.0.1'); 

應該是

$connect = new mysql('root','','test','127.0.0.1', 'SELECT * FROM test'); 

雖然移動爲$query參數到execution()方法清潔器。