Friday 15 July 2011

PHP mysqli search error -



PHP mysqli search error -

i'm trying homecoming results database using search bar , mysqli. however, code isn't working , doesn't create end.

here code:

<?php include("../dbconnect.php"); if (!isset($_post['search'])) { header ("location:tornado.php"); } $search_sql="select * `table 74` `col 4` '%$_post[search]%' or `col 3` '%$_post[search]%'"; $search_query=$mysqli->query($search_sql); if(mysqli_num_rows($search_query)!=0) { $search_rs=mysqli_fetch_assoc($search_query); } ?> <!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>results</title> </head> <body> <p>search results</p> <?php if(mysqli_num_rows($search_query) !=0) { do{ ?> <p><?php echo $search_rs['`col 4`']; ?></p> <?php } while ($search_rs=mysqli_fetch_assoc($search_query)); } else { echo "no results found"; } ?> </body> </html>

i'm confused because earlier, "no results found" come up. nil happens @ though have resorted code worked first time. there i'm missing? if possible please explain have done wrong.

you seem have forgotten include $ in mysqli in

$search_query= mysqli->query($search_sql); ^

$mysqli beingness db connection.

which should read as

$search_query=$mysqli->query($search_sql);

having checked errors in query, have signaled it.

or die('there error running query [' . $mysqli->error . ']');

edit: (tested)

this worked me:

<?php $db_host = "xxx"; $db_name = "xxx"; $db_user = "xxx"; $db_pass = "xxx"; $mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name); if($mysqli->connect_errno > 0) { die('connection failed [' . $mysqli->connect_error . ']'); } $search_sql="select * `table 74` `col 4` '%$_post[search]%' or `col 3` '%$_post[search]%'"; $search_query=$mysqli->query($search_sql); ?> <!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>results</title> </head> <body> <p>search results</p> <?php if(mysqli_num_rows($search_query) > 0) { while ($rows=mysqli_fetch_assoc($search_query)){ echo $rows['`col 4`']; } } else { echo "no results found."; } ?> </body> </html>

php search mysqli

No comments:

Post a Comment