为什么这个PHP脚本只返回6号?

This script is supposed to get the current popularity from Mysql, add one digit to that number then update it. But this isnt happening and it only returns '6' Any idea why?

Source:

<?php
include_once("../scripts/config.php");


$url = mysql_real_escape_string($_POST['url']);
preg_match("/id=(\\d+)/", $url, $matches);
$like = $matches[1];
$current_pop = mysql_query("SELECT pop FROM likes WHERE id=$like") or die ("Query failed: " . mysql_error());

$one = '1';

$pop = $current_pop + $one;  

print $pop;
$update = mysql_query("UPDATE likes SET pop = ".$pop." WHERE id = ".$like."") or die ("Query failed: " . mysql_error());

?>

You need to use mysql_fetch_array() to get the value from the first row:

 $row = mysql_fetch_array($current_pop);
 $current_pop = $row[0];

You could, however, update the pop value with just one SQL query:

mysql_query("UPDATE likes SET pop=(pop+1) WHERE id=$like") or die ("Query failed: " . mysql_error());

You have to fetch the values from the result. Use mysql_fetch_assoc or something similar to do that.

Try

<?php
include_once("../scripts/config.php");


$url = mysql_real_escape_string($_POST['url']);
preg_match("/id=(\\d+)/", $url, $matches);
$like = $matches[1];
$current_pop = mysql_query("SELECT pop FROM likes WHERE id=$like") or die ("Query failed: " . mysql_error());
$row = mysql_fetch_assoc($result)
$one = 1;

$pop = $row["pop"] + $one;  

print $pop;
$update = mysql_query("UPDATE likes SET pop = ".$pop." WHERE id = ".$like."") or die ("Query failed: " . mysql_error());

?>

mysql_query returns a reference to a resource not the result of the pop field in your query.

You need to do something like this...

$result = mysql_query("SELECT pop FROM likes WHERE id=$like") or die ("Query failed: " . mysql_error());
if ($result) {
    $row = mysql_fetch_assoc($result);
    $current_pop = $row['pop'];
} else {
    // handle error here
}

You should also use mysql_real_esacpe_string around any variable in raw SQL queries like yours.

it should be

<?php
include_once("../scripts/config.php");

$url = mysql_real_escape_string($_POST['url']);

preg_match("/id=(\\d+)/", $url, $matches);

$current_pop = mysql_query("UPDATE likes SET pop=pop+1 WHERE id='{$matches[1]}'") or die ("Query failed: " . mysql_error());

?>