SQL从特定行中选择特定列 [英] SQL Select specific column from specific row

查看:130
本文介绍了SQL从特定行中选择特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的SQL数据库中有一个表,该表有两个字段,一个字段用于id,另一个字段用于整数.我想在id等于我传入的特定数字的整数字段中获取值.id字段称为"id",而整数字段称为"loglevel".这是我得到的代码,但是没有给我想要的结果.

I've got a table in my SQL Database, and the table has two fields, one for an id, and the other for an integer. I want to grab the value in the integer field where the id is equal to a specific number I pass in. The id field is called "id" and the integer field is called "loglevel". This is the code I've got, but it doesn't give me the desired result.

$result = mysql_query("SELECT loglevel FROM Logs WHERE id='$number'");
echo "Pulled from SQL: " . $result;

此输出为

Pulled from SQL: Resource id #2

如果SQL表中的值为2,您能帮我使输出为"2"吗?

Can you help me so the output is "2" if the value in the SQL table is 2?

谢谢

推荐答案

您需要使用 mysql_fetch_array() (或其他). mysql_query()返回的$result是结果资源,而不是实际的行集:

You need to fetch your result using mysql_fetch_assoc(), or mysql_fetch_array() (or others). $result as returned by mysql_query(), is a result resource, not an actual rowset:

$result = mysql_query("SELECT loglevel FROM Logs WHERE id='$number'");

// If the query completed without errors, fetch a result
if ($result) {
  $row = mysql_fetch_assoc($result);
  echo $row['loglevel'];
}
// Otherwise display the error
else echo "An error occurred: " . mysql_error();

当期望返回多行而不是仅返回一行时,请在while循环内获取它们. mysql_fetch_assoc()文档中有很多示例.

When you are expecting multiple rows returned rather than just one, fetch them inside a while loop. There are many examples of this in the mysql_fetch_assoc() documentation .

这篇关于SQL从特定行中选择特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆