我在SQL数据库中有一个整数,我试图在php中找到该整数的值。整数是一张照片的点赞数。以下是我的查询:
$result = query("SELECT likes FROM NSPhotos WHERE IdPhoto='%s' LIMIT 1", $imageID);
如何从"$results"中获得整数值?
假设您使用MySQL,您将做如下操作:
// Establish a connection to the database
$mysqli = new mysql('host', 'user', 'pass', 'database');
$query = "SELECT likes FROM NSPhotos WHERE IdPhoto = ".$imageID." LIMIT 1";
$result= $mysqli -> query($query);
$num = $mysqli -> num_rows;
if($num > 0){
// Fetch the result from the database
$row = $result -> fetch_object();
// Print the result or you could put it in a variable for use later.
echo 'Likes: '.$row -> likes;
$like_count = $row -> likes;
}else{
echo 'Photo not found.';
}