执行sql命令

时间:2011-04-07 15:05:05

标签: php

代码不起作用的因素是什么?

<?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值。

3 个答案:

答案 0 :(得分:3)

看起来你的一些函数调用传递的参数数量不正确。你的mysql()构造函数应该是5,但你只给它4。而且,execution()不会占用任何你想要给它2。

答案 1 :(得分:1)

您的构造函数应为new mysql('root','','test','127.0.0.1', 'SELECT * FROM test');

答案 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()方法更清晰。