Hi gurus? I have problems with the search code. please help me to know why the code while($row=mysql_fetch_array($sql)) tells me that there is an invalid sql resource. please give other better search options.thanks

how to build a search query based on a POST-ed value:

<?php
include("mysql_conn.php");
if ( !empty( $_POST["searchword"] ) ) {
    $q = mysql_real_escape_string( $_POST["searchword"] );
    $sql = sprint( "select * from table where 
field1 LIKE '%%%1$s%%' OR 
field2 LIKE '%%%1$s%%'" , $q );
    $res = mysql_query( $sql ) or die("Error in query: ".mysql_error())
    if ( mysql_num_rows( $res ) > 0 ) {
        while ( $one = mysql_fetch_assoc( $res ) ) {
            echo $one["field1"] . "-" . $one["field2"] . "<br />";
        } 
    } else
        print "No result";
} 
/* form comes here...*/

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.