Mysqli_fetch_row() expects Parameter 1 to be mysqli_result

Asked

Viewed 1,609 times

0

No, I understand why I’m making that mistake, 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 Pupil Alteraraluno.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");
        }
    ?>
  • which error displayed?

  • Warning: mysqli_fetch_row() expects Parameter 1 to be mysqli_result, Object Given in C: xampp htdocs project2 Pupil Alteraraluno.php on line 17

4 answers

9


In a UPDATE, no value is returned, such as would be in a SELECT. It is therefore not necessary to use the function mysqli_fetch_row() to find out whether or not the operation was successful with a if.

To know the number of lines affected by a INSERT/UPDATE/DELETE, use the function mysqli_affected_rows().

Another detail important: your UPDATE does not have a clase WHERE that is all rows in this table will be changed with the same value in the column email.

Change:

$result0=mysqli_query($conn,$inst3);
$result1=mysqli_fetch_row($conn);

To:

if(!mysqli_query($conn, $inst3)){ 
   echo 'erro: '. mysqli_error($conn);
}else{
   echo 'linhas alteradas: '. mysqli_affected_rows($conn);
}
  • https://gyazo.com/5ccdd6388ae48febb50e4aed23e0951c :(

  • @Paul can’t open this image, which error appears?

  • Warning: mysqli_query() expects Parameter 2 to be string

  • @Paul fixed, I should have passed the update string, by mistake passed the wrong variable, see the answer edition.

  • valeeu ! fixed my problem !

  • 2

    @Paul Watch out for update NO WHERE

  • 1

    Ok ! I will be more attentive

Show 2 more comments

3

There are two errors:

1. Variable $conn:

The mysqli_fetch_row() awaits the outcome of mysqli_query(), mysqli_store_result() or mysqli_use_result(), see here in the documentation, and not the mysqli_connect().

Basically:

$con = mysqli_connect(....);

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

$fetch = mysqli_fetch_row($query);

In your case you’re 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:

The mysqli_fetch_row only works in SELECT, you must use in its place the mysqli_affected_rows, which is the closest you can do. It will get the number of affected lines but not their content. This function supports INSERT, UPDATE, REPLACE or DELETE, but can’t stand SELECT.

  • I always wore the connection, much lack of attention

3

In the example below I put the clause WHERE id = 1, you must put the clause WHERE according to your need, if not put, will change all 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");
        }
    }
?>

2

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

I think what you need is to pass the $result0 in the method mysqli_fetch_row().

would look like this:

$result1=mysqli_fetch_row($result0);

More information about the mysqli_fetch_row method()

  • The error remains unfortunately, I have never had so much difficulty doing an update

  • Do the following add this and put it here, below $result0: dd($result0);

  • I’ve got it figured out, thanks Jedson :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.