Hallo,

I am trying to make this search box works. This is my code:

navigation.php

<? 
 //This is only displayed if they have submitted the form 
 if ($searching =="yes") 
 { 
 echo "<h2>Results</h2><p>"; 

 //If they did not enter a search term we give them an error 
 if ($find == "") 
 { 
 echo "<p>You forgot to enter a search term"; 
 exit; 
 } 

 // Otherwise we connect to our Database 
 include('con_database.php');

 // We preform a bit of filtering 
 $find = strtoupper($find); 
 $find = strip_tags($find); 
 $find = trim($find); 

 //Now we search for our search term, in the field the user specified 
 // $data = mysql_query("SELECT * FROM static_content WHERE upper($field) LIKE'%$find%'"); 

 $data = mysql_query("SELECT * FROM static_content WHERE upper(image) LIKE '%$find%' or upper(title) LIKE'%$find%' or upper(content) LIKE'%$find%'"); 

 //And we display the results 
 while($result = mysql_fetch_array($data)) 
 { 
 echo '<img src="images/events/thumb/' . $result['images'] . '">'; 
 echo " "; 
 echo $result['title']; 
 echo "<br>"; 
 echo $result['content']; 
 echo "<br>"; 
 echo "<br>"; 
 } 

 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
 $anymatches=mysql_num_rows($data); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 

 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find; 
 } 
 ?> 

<div id="search">
<form name="contact" action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<input type="text" name="find" placeholder="search.." value="" width="80"></div>
<input type="hidden" name="searching" value="yes" />

<div id="sbutton">
<input src="images/search button.jpg" name="submit" value="Search" type="image" width="30"></div>
</form>
</div>

My mysql:

table

static_content

column

content
image
title

I wonder after I input search text in the search box and press enter, the search result does not appears in the website.

Recommended Answers

All 23 Replies

Try "mysql_fetch_assoc" instead of "mysql_fetch_array".. not entirely sure that's THE PROBLEM but at a first glance that looks wrong.

I try using: mysql_fetch_assoc and still nothing appears on the website after I press enter on the search box.

I have not yet got an answer, as I have not read much of your code.
I have though, noticed you are using mysql_* functions, which are as of this point... deprecated. Have you checked out PDO? It's much more secure and I might be wrong here... but I also think it's faster connecting. Also, it's fun to develop with since it's OOP. :)

mysql_* are deprecated. PDO is worth learning, and it's more secure than mysql_*.

commented: +1 +13

hi,

this

  if ($searching =="yes")

don't make sense at all. Even-after looking at the original codes at about.com, Angela Bradley did not make her points very clear. How can she possibly evaluate $searching in a True or False context ,if searching is indeed part of the form vars.

To make it work, you will have make minor adjustments to the codes. There are many ways of doing this. You can either remove it or define a new variable $searching. It can be something like this

    if(isset ($_POST['submit']) && ($_POST['searching'] === 'yes')){

    ## put all of your codes here

    ## or define the variable $searching

    $searching = true;

    }

We can change the orginal code to something like this

  if ($searching){
  ## rest of codes here

  }

I prefer completely eliminating the $searching in if statement as originally coded.

matrixdevuk is precisely correct, PDO is worth learnig and It won't take your entire day to learn it. From PHP 5.5.0 version, the old MySQL extension is no longer supported and we can either use MySQLI or PDO extension.

I replace the top line with:

 <? 
 //This is only displayed if they have submitted the form 
 if ($searching)
 { 

still nothing happens if I press enter after I input the search term. I already read the PDO thing. I might try to use it for next time. As of now the sql_* thing still works.

Maybe PHP is trolling you, try if (isset($searching) && $searching==true)

Well, I get it to work after adding:

 $find = strip_tags(@$_POST['find']);
 $searching = strip_tags(@$_POST['searching']);

This is my new error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in E:\xampp\xampp\htdocs\IndonusaCMS\includes\navigation.php on line 97

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in E:\xampp\xampp\htdocs\IndonusaCMS\includes\navigation.php on line 109
Sorry, but we can not find an entry to match your query

Searched For: SUNVONE

-------------------------

line 97: while($result = mysql_fetch_array($data))

line 109: $anymatches=mysql_num_rows($data);

I wonder why?

This is my new error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in E:\xampp\xampp\htdocs\IndonusaCMS\includes\navigation.php on line 97

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in E:\xampp\xampp\htdocs\IndonusaCMS\includes\navigation.php on line 109
Sorry, but we can not find an entry to match your query

Provide us with lines:
90-115

navigation.php

<? 
 //This is only displayed if they have submitted the form 

 $find = strip_tags(@$_POST['find']);
 $searching = strip_tags(@$_POST['searching']);

if (isset($searching) && $searching==yes)
 { 
 echo "<h2>Results</h2><p>"; 

 //If they did not enter a search term we give them an error 
 if ($find == "") 
 { 
 echo "<p>You forgot to enter a search term"; 
 exit; 
 } 

 // Otherwise we connect to our Database 
 include('./include/con_database.php');

 // We preform a bit of filtering 
 $find = strtoupper($find); 
 $find = strip_tags($find); 
 $find = trim($find); 

 //Now we search for our search term, in the field the user specified 
 // $data = mysql_query("SELECT * FROM static_content WHERE upper($field) LIKE'%$find%'"); 

 $data = mysql_query("SELECT * FROM static_content WHERE upper(image) LIKE '%$find%' or upper(title) LIKE'%$find%' or upper(content) LIKE'%$find%'"); 

 //And we display the results 
 while($result = mysql_fetch_array($data)) 
 { 
 echo '<img src="images/events/thumb/' . $result['images'] . '">'; 
 echo " "; 
 echo $result['title']; 
 echo "<br>"; 
 echo $result['content']; 
 echo "<br>"; 
 echo "<br>"; 
 } 

 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
 $anymatches=mysql_num_rows($data); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 

 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find; 
 } 
 ?> 

<div id="search">
<form name="contact" action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<input type="text" name="find" placeholder="search.." value="" width="80"></div>
<input type="hidden" name="searching" value="yes" />

<div id="sbutton">
<input src="images/search button.jpg" name="submit" value="Search" type="image" width="30"></div>
</form>

I am trying to convert my mysql_* to PDO. Here is what I did so far:

searchPDO.php

<link href= "./css/style.css" rel="stylesheet" type="text/css" media="screen">

 <?php

 // PDO start here

 //This is only displayed if they have submitted the form 

 $find = strip_tags(@$_POST['find']);
 $searching = strip_tags(@$_POST['searching']);

if (isset($searching) && $searching==yes)
 { 
 echo "<h2>Results</h2><p>"; 

 //If they did not enter a search term we give them an error 
 if ($find == "") 
 { 
 echo "<p>You forgot to enter a search term"; 
 exit; 
 } 

 // Otherwise we connect to our Database 
 // #

 // We preform a bit of filtering 
 $find = strtoupper($find); 
 $find = strip_tags($find); 
 $find = trim($find); 


$user = 'indonusa';
$pass = '*****'; 

try {
    $dbh = new PDO('mysql:host=localhost;dbname=indonusacms', $user, $pass);

    foreach($dbh->query("SELECT * FROM static_content WHERE upper(image) LIKE '%$find%' or upper(title) LIKE'%$find%' or upper(content) LIKE'%$find%'") as $data) 
    {
    echo '<img src="images/events/thumb/' . $data['images'] . '">'; 
    echo " "; 
    echo $data['title']; 
    echo "<br>"; 
    echo $data['content']; 
    echo "<br>"; 
    echo "<br>"; 
    }

    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

 // PDO end here


 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
 $anymatches=mysql_num_rows($data); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 

 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find; 
 } 
 ?> 

<form name="contact" action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<div id="search">
<input type="text" name="find" placeholder="search.." value="" width="80">
<input type="hidden" name="searching" value="yes" />
</div>

<div id="sbutton">
<input src="images/search button.jpg" name="submit" value="Search" type="image" width="30"></div>
</form>

Results

Warning: Invalid argument supplied for foreach() in E:\xampp\xampp\htdocs\IndonusaCMS\includes\searchPDO.php on line 39

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in E:\xampp\xampp\htdocs\IndonusaCMS\includes\searchPDO.php on line 60
Sorry, but we can not find an entry to match your query

Searched For: HALLO

-------------------------------------

line 39: foreach($dbh->query("SELECT * FROM static_content WHERE upper(image) LIKE '%$find%' or upper(title) LIKE'%$find%' or upper(content) LIKE'%$find%'") as $data)

line 60: $anymatches=mysql_num_rows($data);

Let me know which codes do I need to fix.

In addition to pritaeas' reply...
Try using foreach on mysql_fetch_array();

Try using foreach on mysql_fetch_array();

He's trying PDO.

He's trying PDO.

I was going based on the OP. :)

searchPDO.php

 // PDO start here

 //This is only displayed if they have submitted the form 

 $find = strip_tags(@$_POST['find']);
 $searching = strip_tags(@$_POST['searching']);

if (isset($searching) && $searching==yes)
 { 
 echo "<h2>Results</h2><p>"; 

 //If they did not enter a search term we give them an error 
 if ($find == "") 
 { 
 echo "<p>You forgot to enter a search term"; 
 exit; 
 } 

 // Otherwise we connect to our Database 
 // #

 // We preform a bit of filtering 
 $find = strtoupper($find); 
 $find = strip_tags($find); 
 $find = trim($find); 


$user = 'indonusa';
$pass = '*****'; 

try {
    $dbh = new PDO('mysql:host=localhost;dbname=indonusacms', $user, $pass);
    }

catch (PDOException $exception) {
    // unlike mysql/mysqli, pdo throws an exception when it is unable to connect
    echo '<p>There was an error connecting to the database!</p>';
    if ($pdoDebug) {
    // pdo's exception provides more information than just a message
    // including getFile() and getLine()
        echo $exception->getMessage();
    }
}

        $query = "SELECT * FROM static_content WHERE upper(image) LIKE '%$find%' or upper(title) LIKE'%$find%' or upper(content) LIKE'%$find%'";

        try {
            $pdoStatement = $pdo->query($query);
            }
        catch (PDOException $exception) {
        // the query failed and debugging is enabled

        echo "<p>There was an error in query: $query</p>";
        echo $exception->getMessage();

        $pdoStatement = false;
        }

        if ($pdoStatement) {
        // perhaps you want to check if there are any rows available

        if ($pdoStatement->rowCount() == 0) {
        echo '<p>No records found.</p>';
        }
        else {
        while ($recordObj = $pdoStatement->fetchObject()) {
        echo $recordObj->mycolumn;
        }
        }
    /*
    foreach($dbh->query("SELECT * FROM static_content WHERE upper(image) LIKE '%$find%' or upper(title) LIKE'%$find%' or upper(content) LIKE'%$find%'") as $data) 
    {
    echo '<img src="images/events/thumb/' . $data['images'] . '">'; 
    echo " "; 
    echo $data['title']; 
    echo "<br>"; 
    echo $data['content']; 
    echo "<br>"; 
    echo "<br>"; 
    }

    */

    $dbh = null;
} 

Results

Fatal error: Call to a member function query() on a non-object in E:\xampp\xampp\htdocs\IndonusaCMS\includes\searchPDO.php on line 53

line 53: $pdoStatement = $pdo->query($query);

Why is it?

That's because your PDO connection is vairable $dbh - change this:
$dbh = new PDO to $pdo = new PDO

Hello,

This is my code:

searchPDO.php

 <?php

 // PDO start here

 //This is only displayed if they have submitted the form 

 $find = strip_tags(@$_POST['find']);
 $searching = strip_tags(@$_POST['searching']);

if (isset($searching) && $searching==yes)
 { 
 echo "<h2>Results</h2><p>"; 

 //If they did not enter a search term we give them an error 
 if ($find == "") 
 { 
 echo "<p>You forgot to enter a search term"; 
 exit; 
 } 

 // Otherwise we connect to our Database 
 // #

 // We preform a bit of filtering 
 $find = strtoupper($find); 
 $find = strip_tags($find); 
 $find = trim($find); 


$user = 'indonusa';
$pass = '12345'; 

try {
    $pdo = new PDO('mysql:host=localhost;dbname=indonusacms', $user, $pass);
    }

catch (PDOException $exception) {
    // unlike mysql/mysqli, pdo throws an exception when it is unable to connect
    echo '<p>There was an error connecting to the database!</p>';
    if ($pdoDebug) {
    // pdo's exception provides more information than just a message
    // including getFile() and getLine()
        echo $exception->getMessage();
    }
}

        $query = "SELECT * FROM static_content AND dynamic_content WHERE upper(images) LIKE '%$find%' or upper(title) LIKE'%$find%' or upper(content) LIKE'%$find%'";


        try {
            $pdoStatement = $pdo->query($query);
            }
        catch (PDOException $exception) {
        // the query failed and debugging is enabled

        echo "<p>There was an error in query: $query</p>";
        echo $exception->getMessage();

        $pdoStatement = false;
        }

        if ($pdoStatement) {
        // perhaps you want to check if there are any rows available

        if ($pdoStatement->rowCount() == 0) {
        echo '<p>No records found.</p>';
        }
        else {
        while ($recordObj = $pdoStatement->fetchObject()) {
        echo $recordObj->mycolumn;
        }
        }
    /*
    foreach($dbh->query("SELECT * FROM static_content WHERE upper(image) LIKE '%$find%' or upper(title) LIKE'%$find%' or upper(content) LIKE'%$find%'") as $data) 
    {
    echo '<img src="images/events/thumb/' . $data['images'] . '">'; 
    echo " "; 
    echo $data['title']; 
    echo "<br>"; 
    echo $data['content']; 
    echo "<br>"; 
    echo "<br>"; 
    }

    */

    $pdoStatement->closeCursor();
}

// clean up any objects
unset($pdoStatement);
unset($pdo);

 // PDO end here


 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
 $anymatches=mysql_num_rows($data); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 

 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find; 
 } 
 ?> 

and this is the last error that I have:

Results

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in E:\xampp\xampp\htdocs\IndonusaCMS\includes\searchPDO.php on line 96
Sorry, but we can not find an entry to match your query

Searched For: TANGGAL

---------------------------

How to replace mysql_num_rows ?

Also, this is strange, I test to search for "tanggal" a word which exist in the content. Why there the program cannot find an entry to match my query?

At the bottom, you have this:

//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
 $anymatches=mysql_num_rows($data); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 
 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find; 
 } 

Change your mysql_num_rows($data) to $data->rowCount()

Then, I get this error:

Fatal error: Call to a member function rowCount() on a non-object in E:\xampp\xampp\htdocs\IndonusaCMS\includes\searchPDO.php on line 103

line 103: $anymatches=$data->rowCount();

You need to make sure $data is your query().

Well, this is my code:

searchPDO.php

 <?php

 // PDO start here

 //This is only displayed if they have submitted the form 

 $find = strip_tags(@$_POST['find']);
 $searching = strip_tags(@$_POST['searching']);

if (isset($searching) && $searching==yes)
 { 
 echo "<h2>Results</h2><p>"; 

 //If they did not enter a search term we give them an error 
 if ($find == "") 
 { 
 echo "<p>You forgot to enter a search term"; 
 exit; 
 } 

 // Otherwise we connect to our Database 
 // #

 // We preform a bit of filtering 
 $find = strtoupper($find); 
 $find = strip_tags($find); 
 $find = trim($find); 


$user = 'indonusa';
$pass = '12345'; 

try {
    $pdo = new PDO('mysql:host=localhost;dbname=indonusacms', $user, $pass);
    }

catch (PDOException $exception) {
    // unlike mysql/mysqli, pdo throws an exception when it is unable to connect
    echo '<p>There was an error connecting to the database!</p>';
    if ($pdoDebug) {
    // pdo's exception provides more information than just a message
    // including getFile() and getLine()
        echo $exception->getMessage();
    }
}

        $query = "SELECT * FROM static_content AND dynamic_content WHERE upper(images) LIKE '%$find%' or upper(title) LIKE'%$find%' or upper(content) LIKE'%$find%'";

        try {
            $pdoStatement = $pdo->query($query);
            }
        catch (PDOException $exception) {
        // the query failed and debugging is enabled

        echo "<p>There was an error in query: $query</p>";
        echo $exception->getMessage();

        $pdoStatement = false;
        }

        if ($pdoStatement) {
        // perhaps you want to check if there are any rows available

        if ($pdoStatement->rowCount() == 0) {
        echo '<p>No records found.</p>';
        }
        else {
        while ($recordObj = $pdoStatement->fetchObject()) {
        echo $recordObj->mycolumn;
        }
        }
    /*
    foreach($dbh->query("SELECT * FROM static_content WHERE upper(image) LIKE '%$find%' or upper(title) LIKE'%$find%' or upper(content) LIKE'%$find%'") as $data) 
    {
    echo '<img src="images/events/thumb/' . $data['images'] . '">'; 
    echo " "; 
    echo $data['title']; 
    echo "<br>"; 
    echo $data['content']; 
    echo "<br>"; 
    echo "<br>"; 
    }

    */

    $pdoStatement->closeCursor();
}

    // clean up any objects
    unset($pdoStatement);
    unset($pdo);


 // PDO end here


 //This counts the number or results - and if there wasn't any it gives them a little message explaining that 


 $anymatches=$pdoStatement->rowCount(); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query<br><br>"; 
 } 


 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$find; 
 } 
 ?> 

I already try replacing $data with $pdoStatement or $query (I think one of these two represent my query) and I still have this error:

Fatal error: Call to a member function rowCount() on a non-object in E:\xampp\xampp\htdocs\IndonusaCMS\includes\searchPDO.php on line 103

line 103: $anymatches=$pdoStatement->rowCount();

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.