0

I have an integer in a SQL database, I am trying to find the value of that integer in php. The integer is the number of likes of a photo. Here is my query:

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

How do I get the integer value out of "$results"?

Jason
  • 15,017
  • 23
  • 85
  • 116
user906357
  • 4,575
  • 7
  • 28
  • 38
  • 2
    Hi, this is very basic and broad - you are not specifying which database library you are using. You should probably work through a basic mySQL tutorial for your library. – Pekka Jun 01 '13 at 20:36
  • 1
    be more specific are what are you using? mysqli or pdo? little more details would be needed – rsz Jun 01 '13 at 20:36
  • I believe I am using mysqli, I worked through a tutorial here: http://www.raywenderlich.com/13511/how-to-create-an-app-like-instagram-with-a-web-service-backend-part-12 – user906357 Jun 01 '13 at 20:49
  • Which DBMS are you using? –  Jun 01 '13 at 20:52

1 Answers1

2

Assuming your using MySQL you would do something like:

// 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.';

}
ajtrichards
  • 29,723
  • 13
  • 94
  • 101
  • here is how I connect to the database, I don't think I am using MySQL, $link = mysqli_connect("$db_host","$db_username","$db_pass"); mysqli_select_db($link, $db_name) or die("no database"); – user906357 Jun 01 '13 at 21:13
  • The way your connecting is through `mysqli` procedural style. The example i've given is in Object style. – ajtrichards Jun 02 '13 at 07:26