如何将sql查询中的值存储到变量中?

时间:2021-06-06 16:41:32
$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.

这将为您提供表中该字段的值。在这里看一个例子。

#2


$ rs = mysql_query("UPDATE user SET resourcesbuilt = '1' WHERE username = '$username'");

will store the result into a recordset named rs you can retrieve the values then by calling

将结果存储到名为rs的记录集中,您可以通过调用来检索值

$myrow = mysql_fetch_array($rs)

will put the whole row into an array named myrow and retrieve their values by $myrow[column_name]

将整行放入名为myrow的数组中,并通过$ myrow [column_name]检索它们的值

oh and another note as far as i remember you cannot store them since they are not select statements the only resource that you'll have is mysql_num_rows

哦,还有另一个注意据我记得你不能存储它们因为它们不是select语句你唯一拥有的资源就是mysql_num_rows

#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.

这将为您提供表中该字段的值。在这里看一个例子。

#2


$ rs = mysql_query("UPDATE user SET resourcesbuilt = '1' WHERE username = '$username'");

will store the result into a recordset named rs you can retrieve the values then by calling

将结果存储到名为rs的记录集中,您可以通过调用来检索值

$myrow = mysql_fetch_array($rs)

will put the whole row into an array named myrow and retrieve their values by $myrow[column_name]

将整行放入名为myrow的数组中,并通过$ myrow [column_name]检索它们的值

oh and another note as far as i remember you cannot store them since they are not select statements the only resource that you'll have is mysql_num_rows

哦,还有另一个注意据我记得你不能存储它们因为它们不是select语句你唯一拥有的资源就是mysql_num_rows