SQL SELECT 使用 PDO 时查询不工作 AJAX
SQL SELECT Query not working while working with PDO AJAX
我有一个 SQL 查询,它在执行 Web 托管 PhpMyadmin 时运行良好。
但是当我通过 php 代码使用它时,它不会返回任何数据。
等待任何帮助。
SQL 查询:
SELECT
`students`.`name`
, `students`.`fatherName`
, `students`.`motherName`
, `students`.`regNo`
, `course`.`courseName`
, `subject`.`subjectName`
, `subject`.`subjectID`
, `course`.`examRollNo`
, `year`.`yearName`
, `papers`.`paperCode`
FROM
`jnuskenresult`.`course`
INNER JOIN `result`.`students`
ON (`course`.`ID` = `students`.`ID`)
INNER JOIN `result`.`subject`
ON (`subject`.`courseID` = `course`.`courseID`)
INNER JOIN `result`.`year`
ON (`year`.`subjectID` = `subject`.`subjectID`)
INNER JOIN `result`.`papers`
ON (`papers`.`yearID` = `year`.`yearID`)
WHERE (`students`.`regNo` ='ABC-AA-12345'
AND `year`.`yearName` ='Third Year');
一切正常
但是下面的 php PDP 代码不适用于同一个数据库。
$data =urldecode(($_GET['appno']));
//$dataSubject =htmlspecialchars(trim($_GET['subject']));
$dataYear =htmlspecialchars($_GET['year']);
$sql="SELECT
`students`.`name`
, `students`.`fatherName`
, `students`.`motherName`
, `students`.`regNo`
, `course`.`courseName`
, `subject`.`subjectName`
, `subject`.`subjectID`
, `course`.`examRollNo`
, `year`.`yearName`
, `papers`.`paperCode`
FROM
`SOMEDBNAME`.`course`
INNER JOIN `SOMEDBNAME`.`students`
ON (`course`.`ID` = `students`.`ID`)
INNER JOIN `SOMEDBNAME`.`subject`
ON (`subject`.`courseID` = `course`.`courseID`)
INNER JOIN `SOMEDBNAME`.`year`
ON (`year`.`subjectID` = `subject`.`subjectID`)
INNER JOIN `SOMEDBNAME`.`papers`
ON (`papers`.`yearID` = `year`.`yearID`)
WHERE (`students`.`regNo` =?
AND `year`.`yearName` =?)";
// we have to tell the PDO that we are going to send values to the query
$stmt = $conn->prepare($sql);
// Now we execute the query passing an array to execute();
$results = $stmt->execute(array($data,$dataYear));
//$results = $stmt->execute(array($data));
// Extract the values from $result
$rows = $stmt->fetchAll();
$error = $stmt->errorInfo();
//echo $error[2];
当我发送一个参数时,其他相同的代码正在工作。
我通过 ajax 调用作为 GET 方法发送两个参数。
感谢您的帮助。
尝试改成:
INNER JOIN `SOMEDBNAME`.`students`
ON `course`.`ID` = `students`.`ID`
INNER JOIN `SOMEDBNAME`.`subject`
ON `subject`.`courseID` = `course`.`courseID`
INNER JOIN `SOMEDBNAME`.`year`
ON `year`.`subjectID` = `subject`.`subjectID`
INNER JOIN `SOMEDBNAME`.`papers`
ON `papers`.`yearID` = `year`.`yearID`
WHERE `students`.`regNo` = :regno
AND `year`.`yearName` = :yearname";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':regno',$data,PDO::PARAM_STR);
$stmt->bindParam(':yearname',$dataYear,PDO::PARAM_STR);
$results = $stmt->execute();
然后你可以简单地使用:
$data =$_GET['appno'];
$dataYear =$_GET['year'];
因此,为了确保问题出在您的变量值上,将您的代码更改为:
$stmt->bindParam(':regno','ABC-AA-12345',PDO::PARAM_STR);
$stmt->bindParam(':yearname','Third Year',PDO::PARAM_STR);
让我们看看结果...
经过长时间的研究,我终于找到了解决这个问题的方法。
if (isset($_GET['appno'])) {
$data =$_GET['appno'];
$data = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $data);
$dataYear =$_GET['year'];
$stmt = $conn->prepare("SELECT
`students`.`name`
, `students`.`fatherName`
, `students`.`motherName`
, `course`.`courseName`
, `subject`.`subjectName`
, `year`.`yearName`
, `students`.`applicationNo`
, `papers`.`paperCode`
FROM
`SOMEDBNAME`.`students`
INNER JOIN `SOMEDBNAME`.`course`
ON (`students`.`ID` = `course`.`ID`)
INNER JOIN `SOMEDBNAME`.`subject`
ON (`subject`.`courseID` = `course`.`courseID`)
INNER JOIN `SOMEDBNAME`.`year`
ON (`subject`.`subjectID` = `year`.`subjectID`)
INNER JOIN `SOMEDBNAME`.`papers`
ON (`year`.`yearID` = `papers`.`yearID`)
WHERE (`year`.`yearName` = '$dataYear'
AND `students`.`applicationNo` = '$data')");
$results = $stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
PHP PDO 的内部连接问题遍布整个互联网。我希望这个解决方案能帮助其他人顺利地完成那里的工作。
我还要感谢@Kim 抽出宝贵的时间来解决这个问题。
无论如何编码愉快!!!
我有一个 SQL 查询,它在执行 Web 托管 PhpMyadmin 时运行良好。 但是当我通过 php 代码使用它时,它不会返回任何数据。 等待任何帮助。
SQL 查询:
SELECT
`students`.`name`
, `students`.`fatherName`
, `students`.`motherName`
, `students`.`regNo`
, `course`.`courseName`
, `subject`.`subjectName`
, `subject`.`subjectID`
, `course`.`examRollNo`
, `year`.`yearName`
, `papers`.`paperCode`
FROM
`jnuskenresult`.`course`
INNER JOIN `result`.`students`
ON (`course`.`ID` = `students`.`ID`)
INNER JOIN `result`.`subject`
ON (`subject`.`courseID` = `course`.`courseID`)
INNER JOIN `result`.`year`
ON (`year`.`subjectID` = `subject`.`subjectID`)
INNER JOIN `result`.`papers`
ON (`papers`.`yearID` = `year`.`yearID`)
WHERE (`students`.`regNo` ='ABC-AA-12345'
AND `year`.`yearName` ='Third Year');
一切正常 但是下面的 php PDP 代码不适用于同一个数据库。
$data =urldecode(($_GET['appno']));
//$dataSubject =htmlspecialchars(trim($_GET['subject']));
$dataYear =htmlspecialchars($_GET['year']);
$sql="SELECT
`students`.`name`
, `students`.`fatherName`
, `students`.`motherName`
, `students`.`regNo`
, `course`.`courseName`
, `subject`.`subjectName`
, `subject`.`subjectID`
, `course`.`examRollNo`
, `year`.`yearName`
, `papers`.`paperCode`
FROM
`SOMEDBNAME`.`course`
INNER JOIN `SOMEDBNAME`.`students`
ON (`course`.`ID` = `students`.`ID`)
INNER JOIN `SOMEDBNAME`.`subject`
ON (`subject`.`courseID` = `course`.`courseID`)
INNER JOIN `SOMEDBNAME`.`year`
ON (`year`.`subjectID` = `subject`.`subjectID`)
INNER JOIN `SOMEDBNAME`.`papers`
ON (`papers`.`yearID` = `year`.`yearID`)
WHERE (`students`.`regNo` =?
AND `year`.`yearName` =?)";
// we have to tell the PDO that we are going to send values to the query
$stmt = $conn->prepare($sql);
// Now we execute the query passing an array to execute();
$results = $stmt->execute(array($data,$dataYear));
//$results = $stmt->execute(array($data));
// Extract the values from $result
$rows = $stmt->fetchAll();
$error = $stmt->errorInfo();
//echo $error[2];
当我发送一个参数时,其他相同的代码正在工作。 我通过 ajax 调用作为 GET 方法发送两个参数。 感谢您的帮助。
尝试改成:
INNER JOIN `SOMEDBNAME`.`students`
ON `course`.`ID` = `students`.`ID`
INNER JOIN `SOMEDBNAME`.`subject`
ON `subject`.`courseID` = `course`.`courseID`
INNER JOIN `SOMEDBNAME`.`year`
ON `year`.`subjectID` = `subject`.`subjectID`
INNER JOIN `SOMEDBNAME`.`papers`
ON `papers`.`yearID` = `year`.`yearID`
WHERE `students`.`regNo` = :regno
AND `year`.`yearName` = :yearname";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':regno',$data,PDO::PARAM_STR);
$stmt->bindParam(':yearname',$dataYear,PDO::PARAM_STR);
$results = $stmt->execute();
然后你可以简单地使用:
$data =$_GET['appno'];
$dataYear =$_GET['year'];
因此,为了确保问题出在您的变量值上,将您的代码更改为:
$stmt->bindParam(':regno','ABC-AA-12345',PDO::PARAM_STR);
$stmt->bindParam(':yearname','Third Year',PDO::PARAM_STR);
让我们看看结果...
经过长时间的研究,我终于找到了解决这个问题的方法。
if (isset($_GET['appno'])) {
$data =$_GET['appno'];
$data = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $data);
$dataYear =$_GET['year'];
$stmt = $conn->prepare("SELECT
`students`.`name`
, `students`.`fatherName`
, `students`.`motherName`
, `course`.`courseName`
, `subject`.`subjectName`
, `year`.`yearName`
, `students`.`applicationNo`
, `papers`.`paperCode`
FROM
`SOMEDBNAME`.`students`
INNER JOIN `SOMEDBNAME`.`course`
ON (`students`.`ID` = `course`.`ID`)
INNER JOIN `SOMEDBNAME`.`subject`
ON (`subject`.`courseID` = `course`.`courseID`)
INNER JOIN `SOMEDBNAME`.`year`
ON (`subject`.`subjectID` = `year`.`subjectID`)
INNER JOIN `SOMEDBNAME`.`papers`
ON (`year`.`yearID` = `papers`.`yearID`)
WHERE (`year`.`yearName` = '$dataYear'
AND `students`.`applicationNo` = '$data')");
$results = $stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
PHP PDO 的内部连接问题遍布整个互联网。我希望这个解决方案能帮助其他人顺利地完成那里的工作。
我还要感谢@Kim 抽出宝贵的时间来解决这个问题。 无论如何编码愉快!!!