0

我正在尝试获取其中存在的行数referral_inreferral_out作为单独的变量)。这是我的代码:

$username = $_SESSION['username'];

$connect = mysql_connect("xxxx", "xxxx", "xxxx!") or die("Couldnt Connect to Server");
mysql_select_db("xxxxx") or die("Couldnt find database");

$samecheck = mysql_query("SELECT `referral_in` FROM `users` WERE `username`=$username");
$same = mysql_num_rows($namecheck);

$leadcheck = mysql_query("SELECT `referral_out` FROM `users` WERE `username`=$username");
$leading = mysql_num_rows($namecheck);
echo "$leading / $same"

何时执行此操作,我收到此错误:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/50/8492150/html/buyarandom/member.php on line 23

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/50/8492150/html/buyarandom/member.php on line 25
4

6 回答 6

0

你忘记了周围的报价$username

$samecheck = mysql_query("SELECT `referral_in` FROM `users` WERE `username`='".$username."'");

也尝试逃避$username你的代码易受攻击SQL Injection

$samecheck = mysql_query("SELECT `referral_in` FROM `users` WERE `username`='".mysql_real_escape_string($username)."'");

旁注:不要使用mysql_query,而是使用mysqli

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
于 2013-07-11T22:58:38.230 回答
0

错误是说 $namecheck 不是 mysql_query。
$namecheck 未定义。

$namecheck 应该分别是 $namecheck 和 $leadcheck。

$samecheck = mysql_query("SELECT `referral_in` FROM `users` WERE `username`=$username");
$same = mysql_num_rows($samecheck);

$leadcheck = mysql_query("SELECT `referral_out` FROM `users` WERE `username`=$username");
$leading = mysql_num_rows($leadcheck);
于 2013-07-11T23:13:11.083 回答
0

$samecheck 而不是 $namecheck??? $namecheck 在哪里定义?

于 2013-07-11T22:59:03.200 回答
0

首先,mysql不推荐使用扩展。请考虑 MySqli 或 PDO。其次,出于安全原因,您可能需要清理会话变量。如果您使用 MySQLi 或 PDO 的参数化查询,则无需担心。否则,API 提供方法(mysqlmysqli)来转义您的字符串。

现在有一个错字WERE,应该是WHERE

如果username是字符串类型,则需要将值括在引号中。

$samecheck = mysql_query("SELECT `referral_in` FROM `users` WHERE `username`='$username'");
$same = mysql_num_rows($samecheck); //another typo; should be $samecheck

同样在你的第二个mysql_num_rows(),你可能打算通过$leadcheck,而不是$namecheck

于 2013-07-11T23:02:04.607 回答
0

这应该工作

$username = $_SESSION['username'];

$connect = mysql_connect("xxxx", "xxxx", "xxxx!") or die("Couldn't Connect to Server");
mysql_select_db("xxxxx") or die("Couldn't find database");

$samecheck = mysql_query("SELECT `referral_in` FROM `users` WHERE `username`=$username") or die (mysql_error());
$same = mysql_num_rows($samecheck);

$leadcheck = mysql_query("SELECT `referral_out` FROM `users` WHERE `username`=$username") or die (mysql_error());
$leading = mysql_num_rows($leadcheck);
echo "$leading / $same"

另外,请在某个时候考虑使用 mysqli

http://php.net/manual/en/book.mysqli.php

因为不推荐使用 mysql 扩展,对 SQL 注入开放,并且可能会在较新版本的 PHP 中删除

于 2013-07-11T23:02:45.200 回答
0

尝试这个

      $username = $_SESSION['username'];

   $connect = mysql_connect("xxxx", "xxxx", "xxxx!") or die("Couldnt Connect to Server");
   mysql_select_db("xxxxx") or die("Couldnt find database");

   $samecheck = mysql_query("SELECT `referral_in` ,`referral_out`  FROM `users` WHERE `username`=$username");
   $same = mysql_num_rows($namecheck);
   $row = mysql_fetch_array($samecheck);

  echo $row['referral_in'] / $row['referral_out'] ;

笔记:

  • mysql 已弃用,请改用 PDO 或 mysqli。
于 2013-07-11T23:05:00.693 回答