简体   繁体   中英

How to use SELECT IFNULL in PDO?

i was looking for a way to return an error if no results were found in a MySql query, at first i declared a variable value false, and if the fetch() function is true it will set the value of that boolean to true, then i check if it is true or false, but then i searched for that in internet cause i didn't like my solution that much, so I found the function IFNULL(query, 'error message'); I tried it but I had an error, can you tell me what's wrong in my code?

if(isset($_POST['tosearchfor']))
        {
            $query = $db->query('SELECT IFNULL( (SELECT * FROM searchfor WHERE title LIKE \'%'.$_POST['tosearchfor'].'%) , \'Sorry, no resluts found for : <strong>'.$_POST['tosearchfor'].'strong');
            while($result = $query->fetch())
            {
                echo '<div class="result"><a class="title" href="#">'.$result['title'].'</a><span class="link"><span style="font-size:15px;position:relative;top:0.8px;padding-right:2px;">&#8227;</span>https://www.qsoft.com/'.$result['link'].'</span><span class="details">'.$result['details'].'</span></div>';
            }
        }
        else
        {
            echo 'Error : empty search';
        }

Thank you.

Hey i used a different method not in youre query but with $query->rowCount() . Let's use the IF outside of the query :) And the $db->prepare protect youre query, because never put a $_POST directly inside a query without protection.

if(isset($_POST['tosearchfor'])){
    //We prepare the query
    $query = $db->prepare("SELECT * FROM searchfor WHERE title LIKE '%:tosearch%'");
    //We had parameters
    $query->bindParam(':tosearch',$_POST['tosearchfor'], PDO::PARAM_STR);
    //We execute the query
    $query->execute();

    //We retrieve the results in array of objects
    $results = $query->fetchAll(PDO::FETCH_OBJ);

    if (count($results) > 0) {
        foreach ($results as $result){
            echo '<div class="result">
                    <a class="title" href="#">'.$result->title.'</a>
                    <span class="link">
                        <span style="font-size:15px;position:relative;top:0.8px;padding-right:2px;">&#8227;</span>
                        https://www.qsoft.com/'.$result->link.'
                    </span>
                    <span class="details">'.$result->details.'</span>
                </div>';
        }
    } else {
       echo 'Sorry, no resluts found for : <strong>'.$_POST['tosearchfor'].'</strong>';
    }
}else{
    echo 'Error : empty search';
}

The default result value of IFNULL(expr1,expr2) is the more “general” of the two expressions, in the order STRING, REAL, or INTEGER.

You cannot evaluate a group of records with IFNULL. Something like below

Also instead of using direct substitution values, you could use below methods to avoid sql injection.

$stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name');

$stmt->execute(array(':name' => $name));

if(empty($stmt)){
  return false;
}

foreach ($stmt as $row) {
    // do something with $row
}

return result;

If this method return false then there is no search result.

Finally, this is my code, i also added max number of results 20.

$db = new PDO('mysql:host=localhost;dbname=search','root','');
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        if(isset($_POST['tosearchfor']))
        {
            $query = $db->query('SELECT * FROM searchfor WHERE title LIKE \'%'.$_POST['tosearchfor'].'%\'');

                    for($i=0; $i<20;  $i++)
                    {
                        if($result = $query->fetch())
                        {
                            echo '<div class="result">
                            <a class="title" href="#">'.$result['title'].'</a>
                            <span class="link">
                                <span style="font-size:15px;position:relative;top:0.8px;padding-right:2px;">&#8227;</span>
                                https://www.qsoft.com/'.$result['link'].'
                            </span>
                            <span class="details">'.$result['details'].'</span>
                            </div>';
                        }
                        else
                        {
                            if($i==0)
                            {
                                echo 'Sorry, no resluts found for : <strong>'.$_POST['tosearchfor'].'</strong>';
                            }
                        }
                    }   
        }
        else
        {
            echo 'Error : empty search';
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM