Cobox
Cobox

Reputation: 13

How to print retrieved data from a database

In the following code I am retrieving data from a MariaDB database, then it should be listing each relevant entry from the database with echo "Username: " . $row['username']; It does not seem to be printing, is there something I'm doing wrong.

(I know it's insecure)

<?php
$connection = @mysqli_connect("localhost","root","","jet")
OR die('Could not connect' .
      mysqli_connect_error());

if($connection->connect_error){
         $sql="SELECT username FROM userinfo WHERE username LIKE '%" . $_POST['search'] . "%';";

        $res=$con->query($sql);

        while($row=$res->fetch_assoc()){
          echo "Username: " . $row['username'];


            }
}

mysqli_close($connection);
?>
<form action="search.php" method="post">
<input type="text" name="search" id="bug">
<input type="submit" name="submit" value="search" id="rat">
</form>

I'm new to stack overflow and my English isn't great, if this is posted in the wrong place please warn me and I will delete. Thank you

Upvotes: 1

Views: 93

Answers (1)

Gyandeep Sharma
Gyandeep Sharma

Reputation: 2327

Error is in this line

$res=$con->query($sql);

Because your connection variable is $connection. Check This

Change this to

$res=$connection->query($sql);

it may work.

Code like below

<?php
    $connection = @mysqli_connect("localhost","root","","jet");
    if($connection->connect_error){//show error if not connection failed
        die("Connection failed: " . $connection->connect_error);
    }
    $sql="SELECT username FROM userinfo WHERE username LIKE '%" . $_POST['search'] . "%';";
    $res=$connection->query($sql);
    while($row=$res->fetch_assoc()){
        echo "Username: " . $row['username'];
    }
    mysqli_close($connection);
?>

Upvotes: 1

Related Questions