SELECT FIELD('str','str1','str2','str3','str4','str');
Output is 5. SELECT FIELD('john',f_name,l_name) as output from student_name
output |
---|
1 |
0 |
0 |
0 |
0 |
0 |
SELECT * FROM `student_name` WHERE FIELD('john',f_name,l_name) >0
f_name | l_name | class |
---|---|---|
John | Deo | Four |
MySQLi database connection string
require "config.php";// Database connection
$query="SELECT * FROM `student_name` WHERE FIELD('john',f_name,l_name) >0";
if ($result_set = $connection->query($query)) {
while($row = $result_set->fetch_array(MYSQLI_ASSOC)){
echo $row['f_name'],$row['l_name'],$row['class'].."<br>";
}
$result_set->close();
}
Here is the sql dump of student_name table for your use.
CREATE TABLE IF NOT EXISTS `student_name` (
`f_name` varchar(20) DEFAULT NULL,
`l_name` varchar(20) DEFAULT NULL,
`class` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `student_name`
--
INSERT INTO `student_name` (`f_name`, `l_name`, `class`) VALUES
('John', 'Deo', 'Four'),
('Larry', NULL, 'Four'),
('Ronald', NULL, 'Five'),
('Garry', 'Miller', 'Five'),
(NULL, NULL, 'Five'),
(NULL, 'Ruller', NULL);
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.