在php中连接到mysql时,query_id没有返回任何内容
我的问题是:在php中连接到mysql时,query_id没有返回任何内容,php,mysql,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,$_dbNam
$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并执行查询。
打印$\u link\u Id时,它有一个值。
打印$\u query\u Id时,它什么都没有?问题就在这里
$sqlcommand= addslashes($sqlcommand);
不要使用添加斜杠
像这样使用
//$sqlcommand= addslashes($sqlcommand);
$this->_query_Id=mysql_query($sqlcommand,$this->_link_Id);
addslashes函数是问题所在问题出在公共函数query$sqlcommand中
您没有在作用域中启动连接。您应该在函数query$sqlcommand中启动connect请正确缩进您的代码,其他人需要阅读它。也可以使用var_dump进行调试,而不是退出。谢谢hakra告诉我var_dump的相关信息。如果您正在寻找一些基于mysql_*函数的数据库类,其中有一个包含错误处理以及可以轻松扩展的结果对象。如下文所述,addslashes不属于查询方法。如果您想让编写SQL查询更舒适,可以添加另一个类来构建这些字符串,类似于:它不是完美的,但可能会给您一些提示,addslashes是问题所在,但我应该使用什么来防止sql注入和撇号?$this->\u link\u Id是资源Id 5从freecomputermarket.members中选择Id,其中UserName=“$this->\u UserName”;我试着不带瑜伽修行;Uselesst这是您从freecomputermarket.members编写的选择ID,其中UserName='$this->_UserName';这包括:;
<?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;
}
}
?>