简体   繁体   中英

SQL result doesn't work when reassigned

The code:

  $msr = db_query("SELECT * FROM users WHERE username='$username'");
  if (db_num_rows($msr) == 0)
      return null;

When

function db_query($query) { return mysql_query($query) or die(mysql_error() . " when querying: $query"); }
function db_num_rows($queres) { return mysql_num_rows($queres) or die(mysql_error()); }

Shows error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

When I replace "db_" with "mysql_" everything works perfectly. Is there a way to fix this?

See " Creating a php function to return mysql results " here on SO.

function db_query($query) { 
  $result = mysql_query($query) or die(mysql_error()." when querying: $query"); 
  return $result;
}

// etc

Apart from that, you should absolutely not do

$msr = db_query("SELECT * FROM users WHERE username='$username'");

for security reasons. This is wide open for SQL injection attacks, see XKCD 327 . Use parametrized SQL statements instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM