0

I'm trying to get out data in a page with a prepared statement. Is this code wrong or:`

$conn = mysqli_connect("localhost", "root", "123456", "test");
if (mysqli_connect_errno()) {
    die("error".mysqli_connect_error());
    exit();
}

$query=" SELECT * FROM persons WHERE name=? AND last=? ;";
$stm=mysqli_stmt_init($conn);
if(! mysqli_stmt_prepare($stm, $query)){
    echo "statment  failed";
}
else{
    mysqli_stmt_bind_param( $stm , "ss", $name , $last);
};

mysqli_stmt_execute($stm);

`

9
  • Have you tried to run in before asking? Commented Jun 28, 2018 at 0:55
  • yes i tryed pavel , modified the code but it doesn't work ! I can't echo the code out , or i make a mistake in code writing or in can't output the code in the index page Commented Jun 28, 2018 at 20:00
  • What are values oof $name and $last? Commented Jun 30, 2018 at 3:08
  • @vinay thoese actually should be the values of the variables wich i get from the Database. Commented Jul 1, 2018 at 14:47
  • @Vinay yes the variable that i want to get from the DB Commented Jul 12, 2018 at 17:45

2 Answers 2

1

Try this:

    $conn = mysqli_connect("localhost", "root", "123456", "test");
    if (mysqli_connect_errno()) {
    die("error".mysqli_connect_error());
    exit();
    }

    $name = 'eliot';
    $last = 'carbone';

    $query=" SELECT * FROM persons WHERE name=? AND last=? ;"

    if($stmt1 = mysqli_prepare($conn, $query)){
        mysqli_stmt_bind_param($stmt1,"ss", $name , $last);

        if(mysqli_stmt_execute($stmt1)){    

            $result = mysqli_stmt_get_result($stmt1);
            if(mysqli_num_rows($result) > 0){
            $foundnum = mysqli_num_rows($result);

            while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

            echo $name = $row['year'];

                }
            }
        }
    }
    mysqli_stmt_close($stmt1);
Sign up to request clarification or add additional context in comments.

Comments

0
mysqli_stmt_execute($stm);    
$result = mysqli_stmt_get_result($stm);
while ($row = mysqli_fetch_array($result, MYSQLI_NUM))
{
    var_dump($row); # your result
}
mysqli_stmt_close($stm);
mysqli_close($conn);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.