단일 행을 반환하는 PHP PDO
업데이트 2 :
그래서 이것이 얻을 수있는 가장 최적화 된 것일까 요?
$DBH = new PDO( "connection string goes here" );
$STH = $DBH -> prepare( "select figure from table1" );
$STH -> execute();
$result = $STH -> fetch();
echo $result ["figure"];
$DBH = null;
업데이트 1 :
SQL 쿼리에 제한을 추가 할 수 있다는 것을 알고 있지만 필요하지 않은 foreach 루프를 제거하고 싶습니다.
원래 질문 :
"foreach"섹션으로 인해 데이터베이스에서 많은 행을 반환하는 데 좋은 IMO 인 다음 스크립트가 있습니다.
데이터베이스에서 항상 하나의 행만 얻을 수 있다는 것을 알고 있다면 이것을 어떻게 최적화합니까? 데이터베이스에서 1 행만 얻을 수 있다는 것을 알고 있다면 foreach 루프가 필요한 이유는 알 수 없지만 코드를 변경하는 방법은 모릅니다.
$DBH = new PDO( "connection string goes here" );
$STH = $DBH -> prepare( "select figure from table1" );
$STH -> execute();
$result = $STH -> fetchAll();
foreach( $result as $row ) {
echo $row["figure"];
}
$DBH = null;
그냥 가져 오세요. 하나의 행만 가져옵니다. 따라서 foreach 루프가 필요하지 않습니다.
$row = $STH -> fetch();
예 (ty northkildonan) :
$dbh = new PDO(" --- connection string --- ");
$stmt = $dbh->prepare("SELECT name FROM mytable WHERE id=4 LIMIT 1");
$stmt->execute();
$row = $stmt->fetch();
$DBH = new PDO( "connection string goes here" );
$STH - $DBH -> prepare( "select figure from table1 ORDER BY x LIMIT 1" );
$STH -> execute();
$result = $STH -> fetch();
echo $result ["figure"];
$DBH = null;
You can use fetch and LIMIT together. LIMIT has the effect that the database returns only one entry so PHP has to handle very less data. With fetch you get the first (and only) result entry from the database reponse.
You can do more optimizing by setting the fetching type, see http://www.php.net/manual/de/pdostatement.fetch.php. If you access it only via column names you need to numbered array.
Be aware of the ORDER clause. Use ORDER or WHERE to get the needed row. Otherwise you will get the first row in the table alle the time.
Did you try:
$DBH = new PDO( "connection string goes here" );
$row = $DBH->query( "select figure from table1" )->fetch();
echo $row["figure"];
$DBH = null;
If you want just a single field, you could use fetchColumn instead of fetch - http://www.php.net/manual/en/pdostatement.fetchcolumn.php
You could try this for a database SELECT query based on user input using PDO:
$param = $_GET['username'];
$query=$dbh->prepare("SELECT secret FROM users WHERE username=:param");
$query->bindParam(':param', $param);
$query->execute();
$result = $query -> fetch();
print_r($result);
how about using limit 0,1
for mysql optimisation
and about your code:
$DBH = new PDO( "connection string goes here" );
$STH - $DBH -> prepare( "select figure from table1" );
$STH -> execute();
$result = $STH ->fetch(PDO::FETCH_ASSOC)
echo $result["figure"];
$DBH = null;
Thanks to Steven's suggestion to use fetchColumn, here's my recommendation to cut short 1 line from your code.
$DBH = new PDO( "connection string goes here" );
$STH - $DBH -> query( "select figure from table1" );
$result = $STH -> fetchColumn();
echo $result;
$DBH = null;
참고URL : https://stackoverflow.com/questions/5456626/php-pdo-returning-single-row
'IT story' 카테고리의 다른 글
jQuery [duplicate]로 이미지에 마우스 클릭의 X / Y 좌표 얻기 (0) | 2020.08.24 |
---|---|
SQL Server의 INSERT INTO SELECT 쿼리에서 중복 방지 (0) | 2020.08.24 |
프로그래밍 방식으로 탭에서 새 페이지 열기 (0) | 2020.08.24 |
Lisp의 장점은 무엇입니까? (0) | 2020.08.24 |
영구적으로 프록시를 사용하도록 curl을 설정하려면 어떻게해야합니까? (0) | 2020.08.24 |