如何将sql查询中的值存储到变量中? [英] How do I store a value from a sql query into a variable?
问题描述
$resourcesbuilt = mysql_query("SELECT resourcesbuilt FROM user WHERE username = '$username' LIMIT 1");
if ($resourcesbuilt<=0 )
{
mysql_query("INSERT INTO resources (username, dollars) VALUES ( '$username', '1000')") or die (mysql_error());
mysql_query("UPDATE user SET resourcesbuilt = '1' WHERE username = '$username'");
}
基本上,我在用户表中有一列,用作标志,告诉我是否已为该特定用户创建了不同的表.如果没有,则为0;如果没有,则为1.但是,似乎不能将查询存储到变量中并检查其是否为0,因为查询的实际值未存储在var中.当我回显var时,它只是说Resource ID#3.我对php和SQL的研究使我失败了.有谁知道一种将结果从SQL查询存储到php变量中的方法吗?还是至少可以指出我正确的方向?预先感谢.
Basically I have a column in a user table that acts as a flag that tells me whether a different table has been made for that particular user. Its 0 if it hasn't, and 1 if it has. However it seems I can't just store the query into a variable and check if its 0 or not, because the actual value from the query isn't stored in the var. When I echo the var it just says Resource id #3. My research into php and SQL has failed me. Does anyone know of a way to store the result form a sql query into a variable in php? Or can at least point me in the right direction? Thanks in advance.
推荐答案
问题是mysql_query返回的结果不能以这种方式使用.相反,您必须致电
The problem is that the result returned by mysql_query is not usable in this way. Instead, you have to call
$value = mysql_fetch_array($resourcesbuilt, MYSQL_ASSOC)
$value ["resourcesbuilt"]
这将为您提供表中该字段的值. 在此处中查找.
This will give you the value of that field in the table. Look here for an example.
这篇关于如何将sql查询中的值存储到变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!