Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

How to retreive required data

hey here is my query . It allow user to retreive date by searching by student name or class but when i make textbox with name empty and select the class the complete data shown to user instead of data with respective class.

function search_details($name = null,$class = null){
    //database connected through connection.php
     include "connection.php";
      try{
        $search = $db->prepare("
                   Select first_Name,last_Name,class,city 
                   FROM details 
                   WHERE first_name LIKE ? || class = ?"
                );     
        $search->bindValue(1,$name."%",PDO::PARAM_STR);
        $search->bindValue(2,$class,PDO::PARAM_INT);
     }catch(Exception $e){
        echo "Not Retreived";
     }
       $search->execute();
       $search = $search->fetchAll(PDO::FETCH_ASSOC);
       return $search;
  }  
   if(isset($_GET['s'])|| isset($_GET['class'])){
      $name = filter_input(INPUT_GET,"s",FILTER_SANITIZE_STRING);
      $class = filter_input(INPUT_GET,"class",FILTER_SANITIZE_NUMBER_INT);
      if(empty($name) && empty($class)){
         header("location:index.php");
         exit;
      }
      $search = search_details($name,$class);
   }

I have solution by using if/else inside try block. If you have any solution with less code inform me. Thank you

Simon Coates
Simon Coates
28,694 Points

it may be flawed. currently, execute and fetchAll will run even if you hit an exception, since the exception code doesn't force a termination (eg. use of exit). so it might be more normal to do something like

<?php
function search_details($name = null,$class = null){
    //database connected through connection.php
     include "connection.php";
      try{
        $search = $db->prepare("
                   Select first_Name,last_Name,class,city 
                   FROM details 
                   WHERE first_name LIKE ? || class = ?"
                );     
        $search->bindValue(1,$name."%",PDO::PARAM_STR);
        $search->bindValue(2,$class,PDO::PARAM_INT);
        $search->execute();
       return $search->fetchAll(PDO::FETCH_ASSOC);
     }catch(Exception $e){
        exit("Not Retreived");
     }
  }

nb: i haven't tested this code.

Simon Coates
Simon Coates
28,694 Points

I haven't tested it, but maybe something like

    $name = (isset($_GET['s']))? filter_input(INPUT_GET,"s",FILTER_SANITIZE_STRING): null;
      $class = (isset($_GET['class']))? filter_input(INPUT_GET,"class",FILTER_SANITIZE_NUMBER_INT): null;
      if(empty($name) && empty($class)){
         header("location:index.php");
         exit;
      }
      $search = search_details($name,$class);

Not exiting on the empty line implicitly tests that one of the values is set.