在php中连接到mysql时,query_id没有返回任何内容

时间:2012-09-27 12:37:02

标签: php mysql

我的查询是:

$sqlCommand="select `ID` from `freecomputermarket`.`members` where `UserName`='$this->_userName';";

我将XAMPP用作apache服务器,我正在使用端口(89)

这个负责数据库连接的类:

<?php
class MySql
{
    private $_link_Id,$_query_Id,$_serverName,$_userName,$_password,$_dbName,$_rowNum;

    public function __construct()
    {
       $this->_serverName="localhost";
       $this->_userName="root";
       $this->_password="";
       $this->_dbName="freecomputermarket";
    }
    public function connect()
    {
       $this->_link_Id=mysql_connect($this->_serverName,$this->_userName,$this->_password);
       if(!$this->_link_Id)
       {
          exit("The Connect is Failed");
       }
       $db_select=mysql_select_db($this->_dbName,$this->_link_Id);
       if(!$db_select)
       {
          exit("Can't Select DataBase");
       }
    }
    public function query($sqlcommand)
    {
       $sqlcommand= addslashes($sqlcommand);
       //echo $sqlcommand;
       $this->_query_Id=mysql_query($sqlcommand,$this->_link_Id);
       exit($this->_query_Id);//print it to check if it is available.
       if(!$this->_query_Id)
          exit("Query failed");
       $this->_rowNum=mysql_affected_rows();
    }
    public function getRow()
    {
       if($this->_rowNum)
       {
          return mysql_fetch_assoc($this->_query_Id);
       }
    }
    public function getAllRows()
    {
       $arr=array();
       $count=0;
       while($count<$this->_rowNum)
       {
          array_push($arr,$this->GetRow());
          $count++;
       }
       return $arr;
    }
    public function getAffectedRowsNumber()
    {
       return $this->_rowNum;
    }
}   
?>

此代码用于连接mysql dbms并执行查询。 打印$ _link_Id时,它有一个值。 打印$ _query_Id时,它什么都没有?

3 个答案:

答案 0 :(得分:0)

问题在这里

$sqlcommand= addslashes($sqlcommand);

不要使用addslashes。

像这样使用

//$sqlcommand= addslashes($sqlcommand);
$this->_query_Id=mysql_query($sqlcommand,$this->_link_Id);

答案 1 :(得分:0)

<?php
class MySql
{
    private $_link_Id,$_query_Id,$_serverName,$_userName,$_password,$_dbName,$_rowNum;

    public function __construct()
    {
        $this->_serverName="localhost";
        $this->_userName="root";
        $this->_password="";
        $this->_dbName="freecomputermarket";
    }
    public function connect()
    {
        $this->_link_Id=mysql_connect($this->_serverName,$this->_userName,$this->_password);
        if(!$this->_link_Id)
        {
                                                exit("The Connect is Failed");
        }
        $db_select=mysql_select_db($this->_dbName,$this->_link_Id);
        if(!$db_select)
        {
                                                exit("Can't Select DataBase");
        }
    }
    public function query($sqlcommand)
    {
                                        // $sqlcommand= addslashes($sqlcommand);
                                          //echo $sqlcommand;
        $this->_query_Id=mysql_query($sqlcommand,$this->_link_Id);
                                          exit($this->_query_Id);//print it to check if it is available.
        if(!$this->_query_Id)
                                                exit("Query failed");
        $this->_rowNum=mysql_affected_rows();
    }
    public function getRow()
    {
        if($this->_rowNum)
        {
                                                return mysql_fetch_assoc($this->_query_Id);
        }
    }
    public function getAllRows()
    {
        $arr=array();
        $count=0;
        while($count<$this->_rowNum)
        {
                                                array_push($arr,$this->GetRow());
                                                $count++;
        }
        return $arr;
    }
                    public function getAffectedRowsNumber()
                    {
                        return $this->_rowNum;
                    }
}   
?>

addslashes()函数是问题

答案 2 :(得分:0)

问题在于公共函数查询($ sqlcommand) 你没有在范围内开始连接。你应该在函数查询($ sqlcommand)

中启动connect()