dcblogdev / dcblogcomments

2 stars 0 forks source link

searching-with-php-and-mysql-beyond-like #30

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Searching with PHP and MySQL beyond LIKE - DC Blog

Ever wanted to add a search feature to your php & MySQL driven web site? Then your in luck th...

https://dcblog.dev/searching-with-php-and-mysql-beyond-like

vo80900 commented 3 years ago

hi thnx for this post but it seems use old version of php can update it to new version of php? thnx

dcblogdev commented 3 years ago

yes, that was written in 2011.

Here's a quick example of how you might rewrite it now.

<?php  
include('config.php') //assuming $db is a database connection, quries can be done using $db->query()

//Post search words
$search = $_GET['search'];

// remove any code from the search term
$search = strip_tags($search);

//No keywords entered.
if ($search == "") {
   echo "<p>Opps! You forgot to enter a search term.</p>";
} else {

    // perform the search
    $result = $db->query("SELECT * FROM articles WHERE MATCH(articlesTitle) AGAINST(':search*' IN BOOLEAN MODE), ['search' => $search]";

    echo "<h3>Search Results</h3>";

    echo "<p>you searched for <b>$search</b> there are ".count($results)." matches.</p>";

    foreach($results as $row) { 
        echo "<h1>$row->title</h1>";
        echo "<p>$row->desc</p>";
    } // close while loop

} // close else
?>

<form action="search.php" method="get">
<input name="search" type="text" size="20" />
<input type="submit" name="submit" value="Search" />
</form>
vo80900 commented 3 years ago

thnk you so much😉

On Sat, Dec 5, 2020 at 7:22 AM David Carr notifications@github.com wrote:

yes, that was written in 2011.

Here's a quick example of how you might rewrite it now.

<?php include('config.php') //assuming $db is a database connection, quries can be done using $db->query() //Post search words$search = $_GET['search']; // remove any code from the search term$search = strip_tags($search); //No keywords entered.if ($search == "") { echo "

Opps! You forgot to enter a search term.

"; } else {

// perform the search
$result = $db->query("SELECT * FROM articles WHERE MATCH(articlesTitle) AGAINST(':search*' IN BOOLEAN MODE), ['search' => $search]";

echo "<h3>Search Results</h3>";

echo "<p>you searched for <b>$search</b> there are ".count($results)." matches.</p>";

foreach($results as $row) {
    echo "<h1>$row->title</h1>";
    echo "<p>$row->desc</p>";
} // close while loop

} // close else?>

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/dcblogdev/dcblogcomments/issues/30#issuecomment-739290504, or unsubscribe https://github.com/notifications/unsubscribe-auth/AP4MYCDJADY7NTLV33M6LDDSTJF2RANCNFSM4UOTMUQQ .

vo80900 commented 3 years ago

hi i am so sorry i tried your code but it show me error after run can i cant find what is its error i use this code <?php include('blog/includes/config.php'); //assuming $db is a database connection, quries can be done using $db->query()

//Post search words $search = $_GET['search'];

// remove any code from the search term $search = strip_tags($search);

//No keywords entered. if ($search == "") { echo "

Opps! You forgot to enter a search term.

"; } else {

// perform the search
$result = $db->query("SELECT * FROM blog_posts_seo WHERE

MATCH(postTitle) AGAINST(':search*' IN BOOLEAN MODE), ['search' => $search]");

echo "<h3>Search Results</h3>";

echo "<p>you searched for <b>$search</b> there are ".count($results)."

matches.

";

foreach($results as $row) {
    echo "<h1>$row->title</h1>";
    echo "<p>$row->desc</p>";
} // close while loop

} // close else ?>

and its eroror is :

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 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 ' ['search' => تونر]' at line 1 in /home/takashop/domains/ takashop.ir/public_html/search.php:16 Stack trace: #0 /home/takashop/domains/takashop.ir/public_html/search.php(16): PDO->query('SELECT FROM b...') #1 {main} thrown in /home/takashop/domains/takashop.ir/public_html/search.php http://takashop.ir/public_html/search.php on line 16*

thnan you

On Sat, Dec 5, 2020 at 7:55 AM Vahid Or vahid.or.1361@gmail.com wrote:

thnk you so much😉

On Sat, Dec 5, 2020 at 7:22 AM David Carr notifications@github.com wrote:

yes, that was written in 2011.

Here's a quick example of how you might rewrite it now.

<?php include('config.php') //assuming $db is a database connection, quries can be done using $db->query() //Post search words$search = $_GET['search']; // remove any code from the search term$search = strip_tags($search); //No keywords entered.if ($search == "") { echo "

Opps! You forgot to enter a search term.

"; } else {

// perform the search
$result = $db->query("SELECT * FROM articles WHERE MATCH(articlesTitle) AGAINST(':search*' IN BOOLEAN MODE), ['search' => $search]";

echo "<h3>Search Results</h3>";

echo "<p>you searched for <b>$search</b> there are ".count($results)." matches.</p>";

foreach($results as $row) {
    echo "<h1>$row->title</h1>";
    echo "<p>$row->desc</p>";
} // close while loop

} // close else?>

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/dcblogdev/dcblogcomments/issues/30#issuecomment-739290504, or unsubscribe https://github.com/notifications/unsubscribe-auth/AP4MYCDJADY7NTLV33M6LDDSTJF2RANCNFSM4UOTMUQQ .

vo80900 commented 3 years ago

hi i found out soloution with help of other user in stackowerflow this code work well : <?php include('blog/includes/config.php'); //assuming $db is a database connection, quries //Post search words // remove any code from the search term $search = trim(strip_tags($_GET['search']));

//No keywords entered. if (empty($search)) { echo "

Opps! You forgot to enter a search term.

"; } else {

// perform the search $stmt = $db->prepare(' SELECT * FROM blog_posts_seo WHERE Match(postTitle) AGAINST(:search IN BOOLEAN MODE)'); $stmt->execute(['search' => $search]); echo "

Search Results

";

echo "

you searched for $search there are ".$stmt->rowCount()." matches.

";

while ($row = $stmt->fetch(PDO::FETCH_OBJ)) { // you don't need the PDO::FETCH_OBJ if it's in your connect statement. echo "

$row->postTitle

"; echo "

$row->desc

"; } // close while loop

} // close else ?>

On Thu, Jan 14, 2021 at 12:44 PM Vahid Or vahid.or.1361@gmail.com wrote:

hi i am so sorry i tried your code but it show me error after run can i cant find what is its error i use this code <?php include('blog/includes/config.php'); //assuming $db is a database connection, quries can be done using $db->query()

//Post search words $search = $_GET['search'];

// remove any code from the search term $search = strip_tags($search);

//No keywords entered. if ($search == "") { echo "

Opps! You forgot to enter a search term.

"; } else {

// perform the search
$result = $db->query("SELECT * FROM blog_posts_seo WHERE

MATCH(postTitle) AGAINST(':search*' IN BOOLEAN MODE), ['search' => $search]");

echo "<h3>Search Results</h3>";

echo "<p>you searched for <b>$search</b> there are ".count($results)."

matches.

";

foreach($results as $row) {
    echo "<h1>$row->title</h1>";
    echo "<p>$row->desc</p>";
} // close while loop

} // close else ?>

and its eroror is :

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 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 ' ['search' => تونر]' at line 1 in /home/takashop/domains/ takashop.ir/public_html/search.php:16 Stack trace: #0 /home/takashop/domains/takashop.ir/public_html/search.php(16): PDO->query('SELECT FROM b...') #1 {main} thrown in /home/takashop/domains/takashop.ir/public_html/search.php http://takashop.ir/public_html/search.php on line 16*

thnan you

On Sat, Dec 5, 2020 at 7:55 AM Vahid Or vahid.or.1361@gmail.com wrote:

thnk you so much😉

On Sat, Dec 5, 2020 at 7:22 AM David Carr notifications@github.com wrote:

yes, that was written in 2011.

Here's a quick example of how you might rewrite it now.

<?php include('config.php') //assuming $db is a database connection, quries can be done using $db->query() //Post search words$search = $_GET['search']; // remove any code from the search term$search = strip_tags($search); //No keywords entered.if ($search == "") { echo "

Opps! You forgot to enter a search term.

"; } else {

// perform the search
$result = $db->query("SELECT * FROM articles WHERE MATCH(articlesTitle) AGAINST(':search*' IN BOOLEAN MODE), ['search' => $search]";

echo "<h3>Search Results</h3>";

echo "<p>you searched for <b>$search</b> there are ".count($results)." matches.</p>";

foreach($results as $row) {
    echo "<h1>$row->title</h1>";
    echo "<p>$row->desc</p>";
} // close while loop

} // close else?>

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/dcblogdev/dcblogcomments/issues/30#issuecomment-739290504, or unsubscribe https://github.com/notifications/unsubscribe-auth/AP4MYCDJADY7NTLV33M6LDDSTJF2RANCNFSM4UOTMUQQ .