paulmo Posted January 15, 2009 Share Posted January 15, 2009 error: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource $message_string =$_POST['message']; $pre_filter=trim($message_string); $get_search=explode(" ",$pre_filter); $sql = "SELECT * FROM beta"; foreach ($get_search as $final_string){ $sql .= "WHERE terms LIKE '%final_string%' or"; } $sql = substr_replace($sql,"",-2); //take off the last 'or' $posts =mysql_query($sql); //} $n=0; while($row=mysql_fetch_assoc($posts)){ $n++; echo $row['terms']; echo "<br>"; } i'm not looking to match my own LIKE words, but any user submitted word/words (or echo none if none found), matched against db table field named 'terms.' thanks for help. mysql 4.1 Quote Link to comment https://forums.phpfreaks.com/topic/140936-searchingechoing-any-unknown-single-or-multiple-word-search/ Share on other sites More sharing options...
paulmo Posted January 16, 2009 Author Share Posted January 16, 2009 any suggestions please? Quote Link to comment https://forums.phpfreaks.com/topic/140936-searchingechoing-any-unknown-single-or-multiple-word-search/#findComment-738641 Share on other sites More sharing options...
Maq Posted January 16, 2009 Share Posted January 16, 2009 You're concatenating multiple WHERE clauses onto $sql in your foreach loop, and essentially giving mysql_query an invalid statement. Put this in as well: $posts =mysql_query($sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/140936-searchingechoing-any-unknown-single-or-multiple-word-search/#findComment-738652 Share on other sites More sharing options...
paulmo Posted January 16, 2009 Author Share Posted January 16, 2009 thanks maq. could you please show me how to include both of these clauses in the foreach loop? Quote Link to comment https://forums.phpfreaks.com/topic/140936-searchingechoing-any-unknown-single-or-multiple-word-search/#findComment-738697 Share on other sites More sharing options...
Maq Posted January 16, 2009 Share Posted January 16, 2009 Try something like this: $message_string =$_POST['message']; $pre_filter=trim($message_string); $get_search=explode(" ",$pre_filter); foreach ($get_search as $final_string){ $sql = "SELECT * FROM beta WHERE terms LIKE '%{$final_string}%'"; $sql = substr_replace($sql,"",-2); //take off the last 'or' $posts =mysql_query($sql) or die(mysql_error()); $n=0; while($row=mysql_fetch_assoc($posts)){ $n++; echo $row['terms']; echo " "; } } Quote Link to comment https://forums.phpfreaks.com/topic/140936-searchingechoing-any-unknown-single-or-multiple-word-search/#findComment-738702 Share on other sites More sharing options...
paulmo Posted January 17, 2009 Author Share Posted January 17, 2009 did your edits; getting error message below: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE terms LIKE '% final_string%' orWHERE terms LIKE '%final_string%" at line 1 the line in question is line 140, not 1. the quotes are ' ' around final_string. i've checked a text and manual; the syntax seems right. any ideas? thanks $message_string =$_POST['message']; $pre_filter=trim($message_string); $get_search=explode(" ",$pre_filter); $sql = "SELECT * FROM beta"; foreach ($get_search as $final_string) { $sql = "WHERE terms LIKE '%final_string%' or"; $sql .= substr_replace($sql,"",-2); //take off the last 'or' $posts = mysql_query($sql) or die (mysql_error ()); $n=0; while($row=mysql_fetch_assoc($posts)) { $n++; echo $row['terms']; echo "<br>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/140936-searchingechoing-any-unknown-single-or-multiple-word-search/#findComment-738872 Share on other sites More sharing options...
fenway Posted January 18, 2009 Share Posted January 18, 2009 Please echo $sql and post it here. Quote Link to comment https://forums.phpfreaks.com/topic/140936-searchingechoing-any-unknown-single-or-multiple-word-search/#findComment-739728 Share on other sites More sharing options...
paulmo Posted January 25, 2009 Author Share Posted January 25, 2009 Please echo $sql and post it here. Resource id #2 query as of now: $message = strtoupper($message); $message = strip_tags($message); $message = trim ($message); $message = str_split($message); $name = trim ($name); $name = strip_tags($name); $name = mysql_real_escape_string($name); //$message = preg_split("/[\s,]+/")($message); $data = mysql_query("SELECT * FROM xxx WHERE terms LIKE'%$message%'"); echo $data; while($result = mysql_fetch_array($data)) { echo $result['terms']; echo " "; } Quote Link to comment https://forums.phpfreaks.com/topic/140936-searchingechoing-any-unknown-single-or-multiple-word-search/#findComment-746092 Share on other sites More sharing options...
Maq Posted January 26, 2009 Share Posted January 26, 2009 Instead of: $data = mysql_query("SELECT * FROM xxx WHERE terms LIKE'%$message%'"); echo $data; do this: $sql = "SELECT * FROM xxx WHERE terms LIKE '%$message%'"; echo $sql; mysql_query($sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/140936-searchingechoing-any-unknown-single-or-multiple-word-search/#findComment-746516 Share on other sites More sharing options...
fenway Posted January 27, 2009 Share Posted January 27, 2009 I thought I asked for that... Quote Link to comment https://forums.phpfreaks.com/topic/140936-searchingechoing-any-unknown-single-or-multiple-word-search/#findComment-747545 Share on other sites More sharing options...
Maq Posted January 27, 2009 Share Posted January 27, 2009 I thought I asked for that... You did, but obviously he didn't understand... Quote Link to comment https://forums.phpfreaks.com/topic/140936-searchingechoing-any-unknown-single-or-multiple-word-search/#findComment-747620 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.