I know there are other post conserning this but they dont seem to help me. When I run the varaible $results through the function Mysql_num_rows() it returns as an error saying its not a valid result. Also same for the mysql_fetch_array().

function results(){
if ($_POST['search'] != "") {  
    $search = mysql_real_escape_string($_POST['search']);
    $search = filter_var($search, FILTER_SANITIZE_STRING); 
    //remove unwanted characters from number. 
    $search = ereg_replace("[^A-Za-z0-9]", "", $search);    
    $search = strtolower($search);
    $search = preg_replace('/\s+/','',$search);
else{echo '<div class="noresults">No Results</div>'; die();}
//start query for search
$query = "SELECT * FROM members WHERE number = '$search' ORDER BY user_id ASC";
//run query
$results = mysql_query($query);
if(mysql_num_rows($results) == 0){
    echo '<div class="noresults">No Results</div>';
while($row = mysql_fetch_array($results)){
    echo '<div class="comments_about_wrapper">
        <div class="comment_text_wrapper">
        <div class="comment_text"><p style="float:left;">Number:</p><p style="float:right; padding-right:100px;">Location:</p>
        <p style="float:left;">';
        echo $row['number'];
        echo'</p><p style="float:left; padding-left:108px;">';
        echo $row['location'];
    <div class="comments_about_small_box_wrapper">
    <div class="comments_about_picture">
    <a href=""><img src="" width="45px" height="45px"></a>

The table for members is

user_id       email           password   number  first_name  last_name  location
   1     example1@gmail.com     1234      123       Bob        Jones      xxx
   2     sample2@gmail.com      abcd      456       John       Smith      YYY

Edited by garyjohnson: forgot something

4 Years
Discussion Span
Last Post by garyjohnson
$search = preg_replace('/\s+/','',$search);

I may be wrong here, but doesn't \s remove all digits as well as word characters?

If you're trying to query a number with mysql, you may be inadvertently making $search empty.

$search = ereg_replace("[^A-Za-z0-9]", "", $search);   

You seem to be replacing numbers here too. You don't want to be removing digits, do you?


@martyn 86

$search = preg_replace('/\s+/','',$search);

This removes all white space.

$search = ereg_replace("[^A-Za-z0-9]", "", $search); 

This replaces all characters other than A-Z a-z 0-9 with nothing.

Thanks for the response, I looked into it by echoing the varaible $search after every conversion and it doesnt seem to be the issue.


before running the query:

echo "SELECT * FROM members WHERE number = '$search' ORDER BY user_id ASC";

You could do a 'or die(mysql_error());' on the query too to find what it thinks of it.

In general, you should be steering away from mysql_* functions as they are facing deprecation. Use mysqlI or PDO instead. Pritaeas has posted some nice bits of code in the snippets section.


My apologies. my PCRE is a little rusty. Missed that it was a lowercase s, and missed the caret in the second expression.

My only other thought was a problem with the query itself. You could try manually changing the query to include something you know it should match, like;

SELECT * FROM members WHERE number = 123 ORDER BY user_id ASC

Other than an issue with the query, I'm not sure what it could be.

Edited by Martyn_86


Thanks for all the responses! I'm embarrased to say this but I found out the problem... As it turns out I wasnt connected to the database. So my query wasnt even running. But thanks for all the suggestions!

Votes + Comments
for honesty +++
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.