I am having issues executing an SQL statement and printing the results on my HTML page.
This is the function:
function getFirstName($shId) {
$query = 'SELECT sh_firstname';
$query .= 'FROM Shaddr';
$query .= 'WHERE shopper_id = ?';
$dbo = db_connect();
try {
$statement = $dbo->prepare($query);
$statement->bindParam(1,$shId,PDO::PARAM_INT);
$statement->execute();
}
catch (PDOException $ex) {
error_log($ex->getMessage());
die("Errors encountered while obtaining first name");
}
return $statement;
}
and this is how i'm trying to show it in my HTML:
<?php
$results = getFirstName(2);
echo $results;
?>
The database is connected properly and everything as I have other working queries. The error i'm getting is the exception I made "Errors encountered while obtaining first name".
What is wrong with this function?
You need to space before FROM and WHERE
clause
$query = 'SELECT sh_firstname';
$query .= ' FROM Shaddr';
$query .= ' WHERE shopper_id = ?';
You need to return result set from your getFirstName()
try {
$statement = $dbo->prepare($query);
$statement->bindParam(1,$shId,PDO::PARAM_INT);
$statement->execute();
}
catch (PDOException $ex) {
error_log($ex->getMessage());
die("Errors encountered while obtaining first name");
}
return $result = $statement->fetch(PDO::FETCH_ASSOC);
And you get your value as
<?php
$results = getFirstName(2);
print_r($results);
?>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다