使用旧的 mysql_* API 从特定行中选择特定列
我的 SQL 数据库中有一张表,该表有两个字段,一个用于 id,另一个用于整数。我想获取整数字段中的值,其中 id 等于我传入的特定数字。id 字段称为“id”,整数字段称为“loglevel”。这是我得到的代码,但它没有给我想要的结果。
$result = mysql_query("SELECT loglevel FROM Logs WHERE id='$number'");
echo "Pulled from SQL: " . $result;
其输出为
Pulled from SQL: Resource id #2
Can you help me,如果 SQL 表中的值为 2,则输出为“2”?
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;
The output for this is
Pulled from SQL: Resource id #2
Can you help me so the output is "2" if the value in the SQL table is 2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用
mysql_fetch_assoc()
获取结果,或mysql_fetch_array()
< /a>(或其他)。由mysql_query()
返回的$result
是一种结果资源,而不是实际的行集:当您期望返回多行而不是一行时,请在 <代码> while循环。
mysql_fetch_assoc()
文档。You need to fetch your result using
mysql_fetch_assoc()
, ormysql_fetch_array()
(or others).$result
as returned bymysql_query()
, is a result resource, not an actual rowset:When you are expecting multiple rows returned rather than just one, fetch them inside a
while
loop. There are many examples of this in themysql_fetch_assoc()
documentation .$result = mysql_query("从日志中选择日志级别,其中 id='$number'");
这里$result是一个记录集/mysql资源而不是php数组。您需要使用 mysql_fetch_assoc() 或 mysql_fetch_array() 来访问 $result。
这将打印查询中的所有数据。
$result = mysql_query("SELECT loglevel FROM Logs WHERE id='$number'");
here $result is a record set/ mysql resource not php array. you need to use mysql_fetch_assoc(), or mysql_fetch_array() to access $result.
this will print all data from the query.