
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to write PHP script to fetch data, based on some conditions, from MySQL table?
If we want to fetch conditional data from MySQL table then we can write WHERE clause in SQL statement and use it with a PHP script. While writing the PHP script we can use PHP function mysql_query(). This function is used to execute the SQL command and later another PHP function mysql_fetch_array() can be used to fetch all the selected data. This function returns a row as an associative array, a numeric array, or both. This function returns FALSE if there are no more rows. To illustrate it we are having the following example −
Example
In this example we are writing a PHP script that will return all the records from the table named ‘tutorial_tbl’ for which the author name is Sanjay −
<?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'SELECT tutorial_id, tutorial_title, tutorial_author, submission_date FROM tutorials_tbl WHERE tutorial_author = "Sanjay"'; mysql_select_db('TUTORIALS'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { echo "Tutorial ID :{$row['tutorial_id']} <br> ". "Title: {$row['tutorial_title']} <br> ". "Author: {$row['tutorial_author']} <br> ". "Submission Date : {$row['submission_date']} <br> ". "--------------------------------<br>"; } echo "Fetched data successfully
"; mysql_close($conn); ?>
- Related Articles
- How to write PHP script to delete data from an existing MySQL table?
- Which PHP functions are used in the PHP script to fetch data from an existing MySQL table?
- How can I export values based on some conditions from MySQL table into a file?
- How to write PHP script by using LIKE clause inside it to match the data from MySQL table?
- How to write PHP script to update an existing MySQL table?
- How can you select data from a table based on some criteria using MySQL in Python?
- How to write PHP script by using ORDER BY clause inside it to sort the data of MySQL table?
- How can we fetch all the data from MySQL table by using mysql_fetch_array() function, returning an array with the numeric index, in PHP script?
- How can I search data from MySQL table based on similar sound values?
- Selecting data from a MySQL table based on a specific month?
- Delete only some rows from a table based on a condition in MySQL
- How can I create a stored procedure to select values on the basis of some conditions from a MySQL table?
- How can we insert data into an existing MySQL table by using PHP script?
- Replace records based on conditions in MySQL?
- How to write PHP script by using MySQL JOINS inside it to join two MySQL tables?

Advertisements