如何将sql查询中的值存储到变量中?
$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'");
}
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.
基本上我在用户表中有一个列作为一个标志,告诉我是否为该特定用户创建了一个不同的表。如果没有则为0,如果有则为0。但是,似乎我不能将查询存储到变量中并检查它是否为0,因为查询中的实际值不存储在var中。当我回应var时,它只是说资源ID#3。我对php和SQL的研究让我失望了。有谁知道一种方法将结果从sql查询存储到php中的变量?或者至少可以指出我正确的方向?提前致谢。
2 个解决方案
#1
The problem is that the result returned by mysql_query is not usable in this way. Instead, you have to call
问题是mysql_query返回的结果不能以这种方式使用。相反,你必须打电话
$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.
这将为您提供表中该字段的值。在这里看一个例子。
更多相关文章
- MySql查询脚本,每月统计活动用户。
- MySQL用户管理(5.7.20-winx64)
- Laravel - 如何为用户赋予多重角色?
- Linux下修改MySQL用户(root)密码
- MySQL:ERROR 1227(42000):访问被拒绝 - 无法创建用户
- 在数据库设计中使用用户名作为主键是不是很糟糕?
- 为mysql数据备份建立最小权限的用户
- MySQL查询返回用户的收件人列表
- c语言把mysql数据库语句和变量封装为一个语句