简体   繁体   中英

PHP variable in MySQL [ Select OR ] Statement not executing

i'm coding a search engine i wrote a form with text input and a button:-

<form class="form-inline my-2 my-lg-0" action="" method="post" name="SearchEngine" enctype="multipart/form-data">
        <div class="form-group">
      <input class="form-control mr-sm-2" name="searchinput" type="search" placeholder="البحث" aria-label="Search">
      </div>
      <div class="form-group">
      <button class="btn btn-outline-Dark my-2 my-sm-0" type="submit" style="direction:RTL;">إبحث</button>
      </div>
    </form>

the back end side [ php code ]

require_once '..\Config.php';

$searchon = $_POST['searchinput'];
$dbCon = "mysql:host=$host;dbname=$db_name";

$PDOCon = new PDO($dbCon, $username, $password);

$stmt = $PDOCon->prepare("SELECT * FROM items WHERE name='$searchon' OR chemicalcom= '$searchon'");
$stmt->execute(); 
$rows = $stmt->fetchall();

echo $rows['name'];

the problem is when is submit a value into the text nothing is echoing

// to get the EXCEPTIONS
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

try{
  $stmt = $dbh->prepare("SELECT * FROM items WHERE name=:name OR chemicalcom= :chemicalcom");
$stmt->bindParam(':name', $searchon);
$stmt->bindParam(':chemicalcom', $searchon);
$stmt->execute(); 
$rows = $stmt->fetchAll(); // not fetchall && fetchAll you will get an array so you need to loop over it to get the values like

   foreach($rows as $r){
     echo $r['name'];
    }
 } catch (PDOException $ex) {
    echo  $ex->getMessage();
  }

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