$ query不是在PHP中执行,而是在phpMyAdmin中执行

时间:2012-12-04 18:15:55

标签: php mysql

所以,这是我的代码:

$query = "SELECT `id` FROM `users` WHERE `username`='$username' AND `password`='$password_hash'";
echo $query;
echo mysql_result(mysql_query($query), 0);

当我在PHP中运行此代码时,我执行查询,并在phpMyAdmin中运行它,它显示id 1.但它不输出任何内容,当完整代码运行时,我得到此错误:< / p>

Warning: mysql_result() expects parameter 1 to be resource

4 个答案:

答案 0 :(得分:2)

从php手册:

mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.

您确定已连接到数据库并拥有良好的权利吗?

答案 1 :(得分:1)

确保正确设置连接和查询

 $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
 if (!$link) {
     die('Could not connect: ' . mysql_error());
 }
 if (!mysql_select_db('database_name')) {
    die('Could not select database: ' . mysql_error());
 }
 $result = mysql_query('SELECT * FROM MyTable');
 if (!$result) {
    die('Could not query:' . mysql_error());
}
echo mysql_result($result, 0); // outputs first row

mysql_close($link);

答案 2 :(得分:0)

        $query = "SELECT `id` FROM `users`
                  WHERE `username`='$username'
                  AND `password`='$password_hash'";
        echo $query;
        $sql_result=mysql_query($query) or die(mysql_error());
        echo mysql_result($sql_result, 0);

答案 3 :(得分:0)

使用此代码并相应地编辑它的要求,并检查数据库的连接到utf8_general_ci,并将表格保存为innodB ...

  <?php
   $mysqli_hostname = "localhost";
   $mysqli_user = "root";
   $mysqli_password = "krunal";
   $mysqli_database = "krunal";
   $bd = mysqli_connect($mysqli_hostname, $mysqli_user,$mysqli_password,$mysqli_database);  
   if(mysqli_connect_errno()){die("database connection  failed");}
   ?>


   <?php 
   $sql= "SELECT * FROM `done`;";
   $result=mysqli_query($bd,$sql);
   if(!$result){
   die("database query failed". mysqli_connect_error()."(".mysqli_connect_errno().")");}?>



   <?php while($row=mysqli_fetch_row($result)){
   var_dump($row); }?>

   <?php mysqli_close($bd);?>