【发布时间】: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' ";
有人可以解释一下错误的原因吗?任何帮助将不胜感激。
【问题讨论】: