I’m wondering how do I add more row’s on my search script to be searched.
Here’s my php script:
<?php $con = new PDO("mysql:host=localhost;dbname=db",'user',''); if (isset($_POST["submit"])) { $str = $_POST["search"]; $sth = $con->prepare(" SELECT * FROM `players` WHERE region = '$str' "); $sth->setFetchMode(PDO:: FETCH_OBJ); $sth -> execute(); if($row = $sth->fetch()) { ?>
As you can see this:
SELECT * FROM `players` WHERE region = '$str'
I want to it to search on region and rank rows. I tried:
SELECT * FROM `players` WHERE region, rank = '$str'
…. it’s showing 0 results.
Thank you in advance.
Advertisement
Answer
Is this what you want?
SELECT * FROM players WHERE ? IN (region, `rank`)
This searches for the parameter in both columns region
and rank
.
Side notes:
For this to properly and consistently work, both columns must be of the same datatype
Use prepared statements! Do not concatenate variables in the query string; this is inefficient, and opens up your code to SQL injection. See How can I prevent SQL injection in PHP?
Starting version 8.0.2,
rank
is a reserved word in MySQL, hence not a good choice for a column name