mysql_fetch_assoc()中的错误;

时间:2013-05-01 16:53:40

标签: php mysql

我在mysql_fetch_(assoc,array,row)中遇到错误我找不到问题,当我尝试使用echo计算结果的行时,结果为1

  

警告:mysql_fetch_assoc():提供的参数不是第88行C:\ wamp \ www \ Jocales \ login.php中的有效MySQL结果资源   在查询SELECT * FROM users WHERE uName ='nuha'和uPassword ='123'

 <?php 

   $login= $_POST['login']; 
   $password= $_POST['password'];

      if($login && $password){
       $con = mysql_connect("localhost", "root", "")or die ('no connection');
       mysql_select_db("jocales",$con) or die ('no');
       $query= "SELECT * FROM users WHERE uName ='$login' AND uPassword = '$password'";
       $result = mysql_query($query)or die(mysql_error()." in query $query");

       $record=mysql_fetch_assoc($query) or die(mysql_error()." in query $query"); 
  ?>

3 个答案:

答案 0 :(得分:1)

更改

$record=mysql_fetch_assoc($query)

$record=mysql_fetch_assoc($result)   

注意: Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial

警告:您的代码容易受到 SQL注入的攻击。<​​/ p>

答案 1 :(得分:0)

函数mysql_fetch_assoc()需要一个参数,它应该是resource类型。你提供一个字符串。

$query= "SELECT * FROM users WHERE uName ='$login' AND uPassword = '$password'";
$result = mysql_query($query)or die(mysql_error()." in query $query");

//this is the issue!
$record = mysql_fetch_assoc($query) or die(mysql_error()." in query $query"); 

最后一个陈述应该是

$record = mysql_fetch_assoc($result) or die(mysql_error()." in query $query"); 

注意(直接来自php.net关于mysql _ *)

警告

  

自PHP 5.5.0起,此扩展程序已弃用,将来会被删除。相反,应该使用 MySQLi或PDO_MySQL扩展。另请参阅MySQL:choosing an API guiderelated FAQ以获取更多信息。

答案 2 :(得分:0)

使用此代码(将$query替换为$result

<?php 

   $login= $_POST['login']; 
   $password= $_POST['password'];

      if($login && $password){
       $con = mysql_connect("localhost", "root", "")or die ('no connection');
       mysql_select_db("jocales",$con) or die ('no');
       $query= "SELECT * FROM users WHERE uName ='$login' AND uPassword = '$password'";
       $result = mysql_query($query)or die(mysql_error()." in query $query");

       $record=mysql_fetch_assoc($result) or die(mysql_error()." in query $query"); 
  ?>