Need the simplest PHP search with mysqli

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fokeiro
Forum Newbie
Posts: 2
Joined: Tue Mar 10, 2015 2:00 pm

Need the simplest PHP search with mysqli

Post by fokeiro »

hello, i wonder of anyone here can help me, im trying to create a simple php search , just the bar and a button and populate a table with the info i need. I create this table:


Image

and you can see is been hosted on 1and1.com , and i added a user.

now now on dreamweaver im create this simple php, and it show fine but when i try to look for the name "sigfredo" it doesnt find it

this is my code

Code: Select all

<?php

$host_name  = "db568599787.db.1and1.com";
    $database  = "db568599787";
    $user_name  = "dbo568599787";
    $password  = "removed password";

    $connect = mysqli_connect($host_name, $user_name, $password, $database);
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
//collect

if(isset($_POST['search']))

{
$searchq = $_POST['search'];
        $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);        
        $query - mysql_query("SELECT * FROM register WHERE name LIKE '%$searchq%' OR profile LIKE '%$searchq%'") or die("cant find user in database");
        $count = mysql_num_rows($query);
        
if($count == 0){
        $output = 'There was no search results !!';
}
else
{
        while($row = mysql_fetch_array($query)){
                
        $fname = $row['name'];
        $lname = $row['profile'];
        $uname = $row['information'];
        
        $output .= '<div>'.$fname.' '.$lname.' '.$uname.'</div>';        
                }
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JTAG/RGH/PS3 Database</title>
</head>

<body>
<form action="index.php" method="post">
<input type="text" name="search" placeholder="search the database..." />
<input type='submit' value="Search" />

<?php print("$output");?>

</body>
</html>
my questions are what am i doing wrong? and is there a way to not have to use my pass for the database ?
this is what i get:
Image
Image



thank you for anyone providing any help.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need the simplest PHP search with mysqli

Post by Celauran »

You've got a mix of mysql_ and mysqli_ functions in there. Has to be one or the other.
fokeiro
Forum Newbie
Posts: 2
Joined: Tue Mar 10, 2015 2:00 pm

Re: Need the simplest PHP search with mysqli

Post by fokeiro »

Celauran wrote:You've got a mix of mysql_ and mysqli_ functions in there. Has to be one or the other.
fixed it now it works but is there a way to extra the data without using the password ? i want to put it online but dont want the code to have my pass
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need the simplest PHP search with mysqli

Post by Celauran »

Not really. You need a password to connect to the DB, there's no way around that. It's not visible to the outside and if someone has access to the files on your server, it's already game over. Just make sure you keep it outside of version control.
Post Reply