Php 将代码传输到OOP后将变量传递到mysql查询时出现问题
我正在尝试将我编写的搜索引擎转换为OOP架构 来自数据库连接(sqlconnect1)的相关代码:Php 将代码传输到OOP后将变量传递到mysql查询时出现问题,php,Php,我正在尝试将我编写的搜索引擎转换为OOP架构 来自数据库连接(sqlconnect1)的相关代码: public function query($query){ $this->stmt = $this->dbh->prepare($query); } public function bind($param, $value, $type = null){ if (is_null($type)) {
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
//$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
}
protected function __construct(){
//Make SQL Connection. Then Form Query
if(isset($_GET['search'])){
$sqlconnect1 = new sqlconnect1;
$sqlconnect1->query("SELECT * FROM data1 WHERE address_city LIKE ':get_city'");
//Bind User Input to Prepared Statement
$sqlconnect1->bind(':get_city', '$get_city');
$sqlconnect1->bind(':get_state', '$get_state');
$sqlconnect1->bind(':get_zip', '$get_zip');
$sqlconnect1->bind(':get_country', '$get_country');
$sqlconnect1->bind(':get_category1', '$get_category1');
$sqlconnect1->bind(':get_name', '$get_name');
//Execute Query
$sqlconnect1->execute();
}
protected function __construct(){
//Capture User Input
if(isset($_GET['search'])){
$this->get_city = filter_var($_GET['query_city'], FILTER_SANITIZE_STRING);
$this->get_state = filter_var($_GET['query_state'], FILTER_SANITIZE_STRING);
$this->get_zip = filter_var($_GET['query_zip'], FILTER_SANITIZE_STRING);
$this->get_country = filter_var($_GET['query_country'], FILTER_SANITIZE_STRING);
$this->get_category1 = filter_var($_GET['query_category1'], FILTER_SANITIZE_STRING);
$this->get_name = filter_var($_GET['query_name'], FILTER_SANITIZE_STRING);
echo $this->get_city, $this->get_state, $this->get_category1;
}
//Validate Data
if($this->get_state == '' or $this->get_city == '' or $this->get_category1 == ''){
echo "<center><b>Please, fill in the required fields.</b></center>";
exit();
}
}
相关搜索引擎代码:
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
//$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
}
protected function __construct(){
//Make SQL Connection. Then Form Query
if(isset($_GET['search'])){
$sqlconnect1 = new sqlconnect1;
$sqlconnect1->query("SELECT * FROM data1 WHERE address_city LIKE ':get_city'");
//Bind User Input to Prepared Statement
$sqlconnect1->bind(':get_city', '$get_city');
$sqlconnect1->bind(':get_state', '$get_state');
$sqlconnect1->bind(':get_zip', '$get_zip');
$sqlconnect1->bind(':get_country', '$get_country');
$sqlconnect1->bind(':get_category1', '$get_category1');
$sqlconnect1->bind(':get_name', '$get_name');
//Execute Query
$sqlconnect1->execute();
}
protected function __construct(){
//Capture User Input
if(isset($_GET['search'])){
$this->get_city = filter_var($_GET['query_city'], FILTER_SANITIZE_STRING);
$this->get_state = filter_var($_GET['query_state'], FILTER_SANITIZE_STRING);
$this->get_zip = filter_var($_GET['query_zip'], FILTER_SANITIZE_STRING);
$this->get_country = filter_var($_GET['query_country'], FILTER_SANITIZE_STRING);
$this->get_category1 = filter_var($_GET['query_category1'], FILTER_SANITIZE_STRING);
$this->get_name = filter_var($_GET['query_name'], FILTER_SANITIZE_STRING);
echo $this->get_city, $this->get_state, $this->get_category1;
}
//Validate Data
if($this->get_state == '' or $this->get_city == '' or $this->get_category1 == ''){
echo "<center><b>Please, fill in the required fields.</b></center>";
exit();
}
}
控制器:
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
//$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
}
protected function __construct(){
//Make SQL Connection. Then Form Query
if(isset($_GET['search'])){
$sqlconnect1 = new sqlconnect1;
$sqlconnect1->query("SELECT * FROM data1 WHERE address_city LIKE ':get_city'");
//Bind User Input to Prepared Statement
$sqlconnect1->bind(':get_city', '$get_city');
$sqlconnect1->bind(':get_state', '$get_state');
$sqlconnect1->bind(':get_zip', '$get_zip');
$sqlconnect1->bind(':get_country', '$get_country');
$sqlconnect1->bind(':get_category1', '$get_category1');
$sqlconnect1->bind(':get_name', '$get_name');
//Execute Query
$sqlconnect1->execute();
}
protected function __construct(){
//Capture User Input
if(isset($_GET['search'])){
$this->get_city = filter_var($_GET['query_city'], FILTER_SANITIZE_STRING);
$this->get_state = filter_var($_GET['query_state'], FILTER_SANITIZE_STRING);
$this->get_zip = filter_var($_GET['query_zip'], FILTER_SANITIZE_STRING);
$this->get_country = filter_var($_GET['query_country'], FILTER_SANITIZE_STRING);
$this->get_category1 = filter_var($_GET['query_category1'], FILTER_SANITIZE_STRING);
$this->get_name = filter_var($_GET['query_name'], FILTER_SANITIZE_STRING);
echo $this->get_city, $this->get_state, $this->get_category1;
}
//Validate Data
if($this->get_state == '' or $this->get_city == '' or $this->get_category1 == ''){
echo "<center><b>Please, fill in the required fields.</b></center>";
exit();
}
}
protectedfunction\uuuu构造(){
//捕获用户输入
如果(isset($\u GET['search'])){
$this->get\u city=filter\u var($\u get['query\u city'],filter\u SANITIZE\u STRING);
$this->get\u state=filter\u var($\u get['query\u state'],filter\u SANITIZE\u STRING);
$this->get\u zip=filter\u var($\u get['query\u zip'],filter\u SANITIZE\u STRING);
$this->get\u country=filter\u var($\u get['query\u country'],filter\u SANITIZE\u STRING);
$this->get\u category1=filter\u var($\u get['query\u category1'],filter\u SANITIZE\u STRING);
$this->get\u name=filter\u var($\u get['query\u name'],filter\u SANITIZE\u STRING);
echo$this->get_city,$this->get_state,$this->get_category1;
}
//验证数据
如果($this->get_state==''或$this->get_city=''或$this->get_category1=''){
echo“请填写必填字段。”;
退出();
}
}
当我搜索时什么也没发生。如果我将:get_city
替换为实际的城市,一切正常。您的问题在于:
$sqlconnect1->query("SELECT * FROM data1 WHERE address_city LIKE ':get_city'");
//Bind User Input to Prepared Statement
$sqlconnect1->bind(':get_city', '$get_city'); // <-- your problem
$sqlconnect1->query(“从数据1中选择*地址,如:获取城市”);
//将用户输入绑定到准备好的语句
$sqlconnect1->bind(':get_city','$get_city');//我仍然得到同样的结果。如果我回显$get_city,它将返回正确的输入。您更改了什么?我尝试了许多方法。首先,我把报价放在$get_city左右。然后,我用撇号替换了查询周围的引号,并在下面加上引号:get_city there。然后我在每件事上都加上引号。我还尝试了其他10种引号和撇号的组合。