【问题标题】:php select query with where parameter errorphp选择查询与where参数错误
【发布时间】:2017-11-26 11:24:09
【问题描述】:

我编写了一个 php 脚本来从 mysql 中检索特定用户名的数据。用户名在选择查询中传递,我正在使用邮递员检查 php 脚本。以下是我的php代码

    <?php
        //getting the database connection
 require_once 'MyDbConnect.php';

 //an array to display response
 $response = array();

 //if it is an api call
 //that means a get parameter named api call is set in the URL
 //and with this parameter we are concluding that it is an api call
 if(isset($_GET['apicall'])){

 switch($_GET['apicall']){
   case 'getSpecificData':
     case 'getSpecificData':
if(isTheseParametersAvailable(array('your_username'))){
//getting values
$your_username = $_POST['your_username'];

$heroes = array();
$sql = "SELECT your_username,your_mobile,referral_name,referral_contact,referral_email,
loan_type,loan_amount  FROM mytable WHERE your_username = ? ";
$sql->bind_param("s",$your_username);

$stmt->execute();
$stmt->bind_result($your_username, $your_mobile,$referral_name,$referral_contact,
$referral_email,$loan_type,$loan_amount);

//looping through all the records
while($stmt->fetch()){
 $temp = [
 'your_username'=>$your_username,
 'your_mobile'=>$your_mobile,
 'referral_name'=>$referral_name,
 'referral_contact'=>$referral_contact,
 'referral_email'=>$referral_email,
 'loan_type'=>$loan_type,
 'loan_amount'=>$loan_amount
 ];

 //pushing the array inside the hero array
 array_push($heroes, $temp);

}
echo json_encode($heroes);
}
break;

default:
$response['error'] = true;
$response['message'] = 'Invalid Operation Called';
}
}
else{
//if it is not api call
//pushing appropriate values to response array
$response['error'] = true;
$response['message'] = 'Invalid API Call';
}

function isTheseParametersAvailable($params){

 //traversing through all the parameters
 foreach($params as $param){
 //if the paramter is not available
 if(!isset($_POST[$param])){
 //return false
 return false;
 }
 }
 //return true if every param is available
 return true;
 }
?>

问题出在选择查询中。当我在我的 php 代码中编写上面提到的选择查询时,我什么也得不到。但是如果我按如下方式编写选择查询,我会得到正确的数据

$sql = "SELECT your_username,your_mobile,referral_name,referral_contact,referral_email, loan_type,loan_amount FROM mytable WHERE your_username = 'Rohan' ";

有人可以解释一下错误的原因吗?任何帮助将不胜感激。

【问题讨论】:

    标签: php mysql select where


    【解决方案1】:

    您缺少变量的绑定:

    //  $stmt->bind_param("ss",$your_username);
    

    已在您的代码中。改成:

    $stmt->bind_param("s",$your_username);
    

    “s”表示变量是字符串,绑定“替换”了“?”在查询中。

    编辑: 而不是使用这个 sn-p ;)

    $your_username = $_POST['your_username'];
    
          //creating the query
          $stmt = $conn->prepare("SELECT id,your_username,your_mobile,referral_name,referral_contact,referral_email,
    loan_type,loan_amount FROM mytable WHERE your_username = ? ");
        $stmt->bind_param("s",$your_username);
    

    【讨论】:

    • 对不起,我粘贴了错误的代码,你可以看看。我已经编辑了问题
    猜你喜欢
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 2016-01-14
    • 1970-01-01
    相关资源
    最近更新 更多