从SQL数据库中获取值(整数)

时间:2013-06-01 20:34:14

标签: php sql

我在SQL数据库中有一个整数,我试图在php中找到该整数的值。 整数是照片的喜欢数量。这是我的疑问:

$result = query("SELECT likes FROM NSPhotos WHERE IdPhoto='%s' LIMIT 1", $imageID);

如何从“$ results”中获取整数值?

1 个答案:

答案 0 :(得分:2)

假设你使用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.';

}