Mysqli_fetch_row () expects parameter 1 to be mysqli_result [closed]

0

No, I'm wondering why I get this error, since I just want to do a simple update, and I have the necessary parameter in fetch

  

ERROR: Warning: mysqli_fetch_row () expects parameter 1 to be   mysqli_result, object given in   C: \ xampp \ htdocs \ project2 \ Student \ ChangeAlumni.php on line 17

 <?php
    if (!isset($_SESSION)){session_start();}
        $conn=mysqli_connect("localhost","root","","proj");
        echo $email=$_POST['email'];

        $inst0="Select Email from utilizador ";
        $result0=mysqli_query($conn,$inst0);
        echo $numlinhas1=mysqli_num_rows($result0);
        //echo $numlinhas1;
        //echo var_dump($_POST);
        if ($numlinhas1 > 0)
        {

            $inst3="Update utilizador set Email = '".$email."'";
            echo "xxx";
            $result0=mysqli_query($conn,$inst3);
            $result1=mysqli_fetch_row($conn);

            $_SESSION['mensagem'] ="Dado atualizado";
             //header("Location:IndexAluno.php");
        }
    ?>
    
asked by anonymous 13.01.2017 / 18:41

4 answers

8

In UPDATE , no value is returned, just as it would be in SELECT . Therefore, it is not necessary to use the mysqli_fetch_row() function to know whether the operation succeeded or not with a if .

For the number of rows affected by a INSERT / UPDATE / DELETE , use the

13.01.2017 / 18:49
3

There are two errors:

1. Variable $conn :

The mysqli_fetch_row() expects the result of mysqli_query() , mysqli_store_result() or mysqli_use_result() , see here in the documentation , not mysqli_connect() .

Basically:

$con = mysqli_connect(....);

$query = mysqli_query($con, ...);

$fetch = mysqli_fetch_row($query);

In your case you are using:

$fetch = mysqli_fetch_row($con);

This is the error because the variable $con is not a mysqli_query() , mysqli_store_result() or mysqli_use_result() , but a mysqli_connect() , which is not supported by the function.

2. Fetch in UPDATE:

mysqli_fetch_row only works in SELECT, you should instead use mysqli_affected_rows , which is the closest you can do. It will get the number of rows affected, but not its contents. This function supports INSERT , UPDATE , REPLACE or DELETE , but does not support SELECT .

    
13.01.2017 / 18:49
3

In the example below I put the WHERE id = 1 clause, you should put the WHERE clause according to your need, if not put, will change all the records.

Code:

<?php
    if (!isset($_SESSION)){session_start();}

    $conn  = mysqli_connect("localhost","root","","proj");
    $email = $_POST['email'];

    $inst0 = "SELECT Email FROM utilizador WHERE id = 1";
    $result0 = mysqli_query($conn, $inst0);
    $numlinhas1 = mysqli_num_rows($result0);
    //echo $numlinhas1;
    //echo var_dump($_POST);

    if ($numlinhas1 > 0)
    {
        $inst3 = "UPDATE utilizador SET Email = '".$email."' WHERE id=1";

        if($result0 = mysqli_query($conn, $inst3))
        {
            $_SESSION['mensagem'] = "Dado atualizado";
            //header("Location:IndexAluno.php");
        }
    }
?>
    
13.01.2017 / 19:01
2

The mysqli_fetch_row() method receives a query result, in which case you are entering a connection.

I think you need to pass $result0 to mysqli_fetch_row() method.

would look like this:

$result1=mysqli_fetch_row($result0);

More information about the mysqli_fetch_row () method

    
13.01.2017 / 18:50