使用旧的 mysql_* API 从特定行中选择特定列 [英] Select specific column from specific row using old mysql_* API
问题描述
我的 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
你能帮我把输出变成2"吗?如果 SQL 表中的值为 2?
Can you help me so the output is "2" if the value in the SQL table is 2?
推荐答案
您需要使用 mysql_fetch_assoc()
或 mysql_fetch_array()
(或其他).$result
由 mysql_query()
返回,是结果资源,而不是实际的行集:
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 .
这篇关于使用旧的 mysql_* API 从特定行中选择特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!