简体   繁体   English

如何从SQL查询中获取整数输出

[英]How to get an integer output from an SQL query

I have an SQL query as follows: 我有一个SQL查询如下:

    $tagID = mysql_query("SELECT tagID FROM tags WHERE tagName = '$tag'");
     echo $tagID;

I want $tagID to contain something like 3, or 5, or any integer. 我希望$ tagID包含类似3,或5或任何整数的东西。 But when I echo it, the output is: 但当我回应它时,输出是:

resource id #4

How can I make it a simple integer? 我怎样才能使它成为一个简单的整数?

$result = mysql_query("SELECT tagID FROM tags WHERE tagName = '$tag'"); // figure out why an existing tag gets the ID zero instead of 'tagID'
$row = mysql_fetch_assoc($result);
echo $row["tagID"];

mysql_query() returns result resource, not the value in the query. mysql_query()返回结果资源,而不是查询中的值。 You have to use fetch functions to get the actual data. 您必须使用获取函数来获取实际数据。

If you want this code to be cleaner, check that $result is not false (query error) and $row is not false (nothing found). 如果您希望此代码更清晰,请检查$result是否为false (查询错误)并且$row不是false (找不到任何内容)。

It's always a shock to see not a single programmer in the answers. 在答案中看到不是一个程序员总是令人震惊。
I know the OP is not a programmer too, so, my answer would be totally in vain but what the heck. 我知道OP也不是程序员,所以,我的答案完全是徒劳的但是到底是什么。

Here is a example of a thing called a function: 这是一个称为函数的例子

<?
function dbgetvar(){
  $args = func_get_args();
  $query = array_shift($args);
  foreach ($args as $key => $val) {
    $args[$key] = "'".mysql_real_escape_string($val)."'";
  }
  $query = vsprintf($query, $args);

  $res = mysql_query($query);
  if (!$res) {
    trigger_error("dbgetarr: ".mysql_error()." in ".$query);
    return FALSE;
  } else {
    $row = mysql_fetch_row($res);
    if (!$row) return NULL;
    return $row[0];
  }
}

this code can be saved in some configuration file and then called in this manner: 此代码可以保存在某个配置文件中,然后以这种方式调用:

$tagID = dbgetvar("SELECT tagID FROM tags WHERE tagName = %s",$tag);
echo $tagID;

You're missing a single step. 你错过了一步。 Try this: 尝试这个:

$resource = mysql_query("SELECT tagID FROM tags WHERE tagName = '$tag'");
$tagID = mysql_fetch_assoc($resource);

print_r($tag_id);

If your query returns more than one row (ie there is more than one tag with the same tagName), you'll want to put it in a loop: 如果您的查询返回多行(即,有多个具有相同tagName的标记),您将要将其置于循环中:

$resource = mysql_query("SELECT tagID FROM tags WHERE tagName = '$tag'");
while($tagID = mysql_fetch_assoc($resource)) {
    echo $tagID['tagID'];
}

Addendum 附录

Although the above code will solve your problem, I urge you to stop right there and learn about mysqli instead. 虽然上面的代码可以解决你的问题,但我建议你停在那里,然后去了解mysqli It's a much newer, more robust solution than using the mysql_* functions. 与使用mysql_*函数相比,它是一个更新,更强大的解决方案。 From the docs: 来自文档:

The mysqli extension, or as it is sometimes known, the MySQL improved extension, was developed to take advantage of new features found in MySQL systems versions 4.1.3 and newer. mysqli扩展,或者有时已知的MySQL改进扩展,是为了利用MySQL系统版本4.1.3和更新版本中的新功能而开发的。 The mysqli extension is included with PHP versions 5 and later. mysqli扩展包含在PHP 5及更高版本中。

The mysqli extension has a number of benefits, the key enhancements over the mysql extension being: mysqli扩展有许多好处,对mysql扩展的关键增强是:

  1. Object-oriented interface 面向对象的接口
  2. Support for Prepared Statements 支持准备好的陈述
  3. Support for Multiple Statements 支持多个语句
  4. Support for Transactions 支持交易
  5. Enhanced debugging capabilities 增强的调试功能
  6. Embedded server support 嵌入式服务器支持

Also from the docs: 同样来自文档:

If you are using MySQL versions 4.1.3 or later it is strongly recommended that you use [the MySQLi] extension. 如果您使用的是MySQL 4.1.3或更高版本, 强烈建议您使用[MySQLi]扩展。

The mysql_query function, by itself, returns a 'resource' on success and false on error. mysql_query函数本身在成功时返回'resource',在出错时返回false。 In this case, you're getting a resource that has id #44, which is what you might expect from that function. 在这种情况下,您将获得一个id为#44的资源,这可能是您对该函数的期望。

What you could do is take the result of mysql_query and use mysql_fetch_assoc to convert the resource to an associative array. 你可以做的是获取mysql_query的结果并使用mysql_fetch_assoc将资源转换为关联数组。 (Also check out mysql_fetch_row, or mysql_fetch_field for other techniques). (另请查看mysql_fetch_row或mysql_fetch_field获取其他技术)。 Here's a typical way of structuring this problem: 以下是构建此问题的典型方法:

$query = "SELECT tagID FROM tags WHERE tagName = '$tag'";
$result = mysql_query($query);
$array = mysql_fetch_assoc($result);

$tagID = $array['tagID']; //your integer.

Please see the mysql_query PHP Manual entry for more info. 有关详细信息,请参阅mysql_query PHP手册条目 Check out the user comments at the bottom for particularly good advice and sample code. 查看底部的用户评论,以获得特别好的建议和示例代码。

A SQL query always returns a SQL Resource result, an arguably unreadable object that contains the results of the query. SQL查询始终返回SQL资源结果,这是一个可以说是不可读的对象,其中包含查询结果。 Because of the way that databases are stored, the way that users may want to manipulate data, and the sheer amount of data, it's easier to store it as an identifier than as an object. 由于存储数据库的方式,用户可能想要操作数据的方式以及数据量庞大,因此将其作为标识符而不是对象存储更容易。

To get the data, you need, you must first convert it to an array: 要获取数据,您必须先将其转换为数组:

$result = mysql_query("SELECT tagID FROM tags WHERE tagName = '$tag'");
$row = mysql_fetch_assoc($result);
echo $row["tagID"];

(Where $row[column] is the column you want to pull data from( (其中$ row [column]是您要从中提取数据的列(

Or an object: 或者一个对象:

$result = mysql_query("SELECT tagID FROM tags WHERE tagName = '$tag'");
$object = mysql_fetch_object($result);
echo $object->tagID;

(where $object->column is the column you want to pull data from) (其中$ object-> column是您要从中提取数据的列)

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM