문제
MySQLI를 통해 준비된 진술을 실행하는 데 문제가 있습니다.
먼저 동기화 오류에서 명령을 얻었습니다. 결과를 저장하고 연결을 닫고 있으며이 오류가 발생하지 않았으므로 문제가 중단되었습니다.
그러나 명령이 동기화되지 않은 상태에서 정상적으로 작동하는 SQL 구문 오류의 오류가 다시 나타났습니다. 내 현재 코드는 다음과 같습니다.
Concat을 사용 하여이 Snytax 오류를 수정하기 위해 여러 가지 접근법을 시도했는데, 결합하기 전에 % 부호를 변수에 할당하는 것부터 실패했을 때 댓글을 달았습니다.
사용 시도 :
$numRecords->bind_param("s", "%".$brand."%");
참조별로 오류가 발생합니다.
<?php
$con = mysqli_connect("localhost", "blah", "blah", "blah");
if (!$con) {
echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
exit;
}
$con->query("SET NAMES 'utf8'");
$brand = "o";
$brand = "% ".$brand." %";
echo "\n".$brand;
$countQuery = "SELECT ARTICLE_NO FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE ?";
//CONCAT('%', ?, '%')";
echo "\ntest";
if ($numRecords = $con->prepare($countQuery)) {
$numRecords->bind_param("s", $brand);
echo "\ntest bind";
$numRecords->execute();
echo "\ntest exec";
$numRecords->store_result();
$data = $con->query($countQuery) or die(print_r($con->error));
$rowcount = $data->num_rows;
$numRecords->free_result();
$numRecords->close();
echo "/ntest before rows";
$rows = getRowsByArticleSearch("test", "Auctions", " ");
$last = ceil($rowcount/$page_rows);
} else {
print_r($con->error);
}
foreach ($rows as $row) {
$pk = $row['ARTICLE_NO'];
echo '<tr>' . "\n";
echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$row['USERNAME'].'</a></td>' . "\n";
echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$row['shortDate'].'</a></td>' . "\n";
echo '<td><a href="#" onclick="deleterec(\'Layer2\', \'' . $pk . '\')">DELETE RECORD</a></td>' . "\n";
echo '</tr>' . "\n";
}
function getRowsByArticleSearch($searchString, $table, $max) {
$con = mysqli_connect("localhost", "blah", "blah", "blah");
//global $con;
$recordsQuery = "SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME, date_format(str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s'), '%d %m %Y' ) AS shortDate FROM $table WHERE upper(ARTICLE_NAME) LIKE '%?%' ORDER BY str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s')" . $max;
if ($getRecords = $con->prepare($recordsQuery)) {
$getRecords->bind_param("s", $searchString);
$getRecords->execute();
$getRecords->bind_result($ARTICLE_NO, $USERNAME, $ACCESSSTARTS, $ARTICLE_NAME, $shortDate);
while ($getRecords->fetch()) {
$result = $con->query($recordsQuery);
$rows = array();
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
}
}
정확한 오류는 다음과 같습니다.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 11
11 행은 $ countquery를 정의하는 줄입니다.
보시다시피, 브랜드는 "O"로 제시됩니다.
따라서 SQL 문은 있어야합니다
SELECT ARTICLE_NO FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE %o%;
수동으로 넣을 때 잘 작동합니다.
해결책
mysqli_stmt::bind_param
표현식이 아닌 특정 변수 만 바인딩 할 수 있습니다. 제공된 변수는 값이 아닌 참조별로 '바인딩'으로 전달됩니다. 즉, 기본 SQL은 명령이 실행되는 시점에 변수가 가지고있는 모든 값을 얻습니다.
사용:
WHERE field LIKE CONCAT('%', ?, '%")
또는 do :
$brand = '%' . $brand . '%'
명령이 실행되기 직전에.
당신이 할 수없는 것은 다음과 같습니다.
WHERE field LIKE '%?%
때문에 ?
바운드 변수는 단일 문자열 또는 숫자 값에 해당해야하며, 하위 문자 (또는 필드 이름)가 아닙니다.
편집하다 이 경우 실제 문제는 준비된 진술을 혼합하는 것으로 보입니다 (지원대로 mysqli::prepare
그리고 mysqli_stmt::execute()
) 평범한 오래된 쿼리 (완료된대로 mysqli::query()
). 또한 데이터를 끌어 내고 num_rows를 사용하지 않고 DB 서버에서 직접 행 수를 요청해야합니다.
$countQuery = "SELECT COUNT(ARTICLE_NO) FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE ?";
if ($numRecords = $con->prepare($countQuery)) {
$numRecords->bind_param("s", $brand);
$numRecords->execute();
$numRecords->bind_result($num_rows);
$numRecords->fetch();
$numRecords->free_result();
$numRecords->close();
$last = ceil($rowcount/$page_rows);
} else {
print_r($con->error);
}
다른 팁
문제는 준비된 진술이 아니라 "표준"(준비되지 않은) 문을 실행하는 데 사용되므로 존재하지 않는 '쿼리'메소드를 호출하는 데 있습니다.
$data = $con->query($countQuery) or die(print_r($con->error));
$rowcount = $data->num_rows;
해야한다
$rowcount = $numRecords->num_rows;
getRowsByArticLeearch 함수에서 쿼리를 다시 삭제하고 출력을 위해 BIND_RESULT로 전달한 변수를 사용해야합니다.
$getRecords->bind_result($ARTICLE_NO, $USERNAME, $ACCESSSTARTS, $ARTICLE_NAME, $shortDate);
while ($getRecords->fetch()) {
$pk = $ARTICLE_NO;
echo '<tr>' . "\n";
echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$USERNAME.'</a></td>' . "\n";
// etc...
}
좀 더 많은 정보를 얻으려면 Bind_result의 PHP 매뉴얼을 확인하십시오. http://php.net/manual/en/mysqli-stmt.bind-result.php
이 게시물에 있습니다 mysql.com 그것을 제안하는 것 같습니다 CONCAT()
작동해야합니다 :http://forums.mysql.com/read.php?98,111039,111060#msg-111060
이름이 지정된 Params를 사용해 보셨습니까?
당신은 전화를 시도합니다 :
$numRecords->bind_param("s", "%".$brand."%");
그러나 당신의 SQL은 다음과 같습니다.
$countQuery = "SELECT ARTICLE_NO FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE ?";
그렇지 않습니까? (참고 LIKE ?s
)
$countQuery = "SELECT ARTICLE_NO FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE ?s";