警告:mysql_result()期望参数1是资源,给定对象

时间:2015-02-26 05:07:39

标签: php html mysql sql

如何使用wampserver解决此错误,警告:mysql_result()期望参数1为资源,给定对象

<?php
  session_start();
?>
<html>
<link rel="stylesheet" href="my_layout.css"  type="text/css" />
<title>Phone Book Project</title>
<body class="my_body">
        <div id="my_divition">  

      <?php
       $connect=mysqli_connect("localhost","root","","phonebook");

                $username=$_POST['username'];
                $password=$_POST['password'];
                $sql="select * from members where username='$username' AND password='$password'";
                 if($result=mysqli_query($connect,$sql)){  
                 $number=mysqli_num_rows($result);
                 }
                  if($number>0)
                 {


                   $id=mysql_result($result,0,"id");            
                    $_SESSION['id']=$id;
                    session_register('id');

                    include('Control.php');
                }
                else 
                 echo"<h1> <br /><br />Sorry : invalid entery <br /><br /></h1><a href=index.php >
                 go  back </a>";

           ?>

1 个答案:

答案 0 :(得分:3)

正如Fred -ii-所说,不要将mysql与mysqli混合

mysqli中没有直接等效的mysql_result(),但您可以使用下面代码中实现的mysqli_result函数。这个函数模拟mysql_result()所做的,但是对于mysqli扩展。

将您的代码更改为:

    <?php
  session_start();
?>
<html>
<link rel="stylesheet" href="my_layout.css"  type="text/css" />
<title>Phone Book Project</title>
<body class="my_body">
        <div id="my_divition">  

      <?php

      function mysqli_result($result , $offset , $field = 0){
    $result->data_seek($offset);
    $row = $result->fetch_array();
    return $row[$field];
}

       $connect=mysqli_connect("localhost","root","","phonebook");

                $username=$_POST['username'];
                $password=$_POST['password'];
                $sql="select * from members where username='$username' AND password='$password'";
                 if($result=mysqli_query($connect,$sql)){  
                 $number=mysqli_num_rows($result);
                 }
                  if($number>0)
                 {


                   $id=mysqli_result($result,0,"id");
                    $_SESSION['id']=$id;
                    session_register('id');

                    include('Control.php');
                }
                else 
                 echo"<h1> <br /><br />Sorry : invalid entery <br /><br /></h1><a href=index.php >
                 go  back </a>";

           ?>