Easy To Learning
  • Login
  • Sign Up
  • Q&A
  • Home
  • Php Tutorial
  • MySql Tutorial
  • Object-Oriented Programming
  • Java Tutorial
  • HTML Tutorial
  • React JS Tutorials
  • Free Projects
  • Blogs & Articles
  • Others
    • About Us
    • Contact Us
MySQL Select Using PHP
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. MySql Tutorials
  3. MySQL Select Using PHP

MySql Tutorial

Basic MySql Tutorials

  • MySql Table Content
  • MySQL Introduction
  • MySQL Constraints
  • MySQL Connection In PHP
  • MySQL Create Database In PHP
  • MySQL Create Table using PHP
  • MySQL Insert Using PHP
  • MySQL Update Using PHP
  • MySQL Delete Using PHP
  • MySQL Select Using PHP
  • MySQL Order By Using PHP
  • MySQL Where Clause Using PHP
  • MySQL Fetch Array Using PHP
  • MySQL Fetch Assoc Using PHP
  • MySQL Fetch Row Using PHP
  • MySQL Fetch Object Using PHP
  • MySQL Basic Queries

Basic MySqli Tutorials

  • MySQLi Connection In PHP
  • MySQLi Create Database In PHP
  • MySQLi Create Table using PHP
  • MySQLi Insert Using PHP
  • MySQLi Update Using PHP
  • MySQLi Delete Using PHP
  • MySQLi Select Using PHP
  • MySQLi Order By Using PHP
  • MySQLi Where Clause Using PHP
  • MySQLi Fetch Array Using PHP
  • MySQLi Fetch Assoc Using PHP
  • MySQLi Fetch Row Using PHP
  • MySQLi Fetch Object Using PHP
  • MySQLi Predefined Function In PHP

MySQL Select Using PHP

MySQL Select Query Using PHP

The SELECT statement is used to display data from a table. To display all data from a table using select(*) statement and to display particular field's data use select filed1,filed2 at the place of (*).

Syntax

The SELECT statement is used to select data from one or more tables:

SELECT column_name(s) FROM table_name

Or we can use the * character to select ALL columns from a table:

SELECT * FROM table_name

MySQL functions used in select query:

  • mysql_num_rows(mysql_result $result): returns number of rows.
  • mysql_fetch_assoc(mysql_result $result): returns row as an associative array. Each key of the array represents the column name of the table. It returns NULL if there are no more rows.

PHP MySQL Select Query Example

<?php  
	$host = "localhost";  
	$user = "";  
	$pass = "";  
	$dbname = "users";  
	  
	$conn = mysql_connect($host,$user,$pass) or die(mysql_error());  
	if(!$conn){  
	  die("Could not connect:".mysql_connect_error());  
	}
	// select database 
	mysql_select_db($dbname,$conn);  
	$sql = "SELECT * FROM users";  
	$retval=mysql_query($sql);  
	  
	if(mysql_num_rows($retval) > 0){  
	 while($row = mysql_fetch_assoc($retval)){  
	    echo "EMP ID :$row['id']<br> ".  
	         "EMP NAME :$row['name']<br> ".  
	         "EMP SALARY : $row['salary']  <br> ".  
	         "--------------------------------<br>";  
	 } //end of while
	}else{  
		echo "0 results";  
	}  
	mysqli_close($conn);  
?>  

Output:

Connected successfully
User ID :1 
User Name: name1
User Salary: 90000 
--------------------------------
User ID :2
User Name: name2
User Salary: 50000 
--------------------------------
User ID :3 
User Name: name3
User Salary: 900000

 

Display the Records inside Table:

<?php  
	$host = "localhost";  
	$user = "";  
	$pass = "";  
	$dbname = "users";  
	  
	$conn = mysql_connect($host,$user,$pass) or die(mysql_error());  
	if(!$conn){  
	  die("Could not connect:".mysql_connect_error());  
	}
	// select database 
	mysql_select_db($dbname,$conn);  
	$sql = "SELECT * FROM users";  
	$retval=mysql_query($sql);  
	  
	if(mysql_num_rows($retval) > 0){  
		echo "<table border='1'>";
		echo "<tr><th>ID</th><th>Name</th><th>Salary</th></tr>";
		while($row= mysql_fetch_array($val))
		{
			echo "<tr>";	
			echo "<td>".$row['id']."</td>";
			echo "<td>".$row['name']."</td>";
			echo "<td>".$row['salary']."</td>";
			echo "</tr>";
		}		
		echo "</table>";
	}else{  
		echo "0 results";  
	}  
	mysql_close();  
?> 

Output:

Display Data with table: Users

id name salary
1 Name1 55000
2 Name2 70000

 

Series: MySql Tutorials Hub
All Topics
Free Download Projects
Previous Chapter
Next Chapter

Ask a Question

Have a coding query or doubt? Ask here and get help from our community.

500 characters remaining
Submitting...
Free Download Projects
Join Telegram Group Join WhatsApp Group
Related Topics:
how to retrieve data from database in php using mysql select query in php mysql with example select query in php with where clause mysql_query in php php mysqli query how to fetch data from database in php and display how to fetch data from database in php using mysqli how to fetch data from database in php one by one

Member Sign In

Forgot?
Signing in...

Don't have an account? Register here

Create Free Account

Register Captcha
Registering...

Already have an account? Sign In

Ask a Question

500 characters remaining
Submitting...

About Easy To Learning

Easy To Learning is a comprehensive programming learning portal offering free, practical, developer-driven tutorials for PHP, Java, Python, Spring, MySQL, CSS, Bootstrap, CakePHP, and more.

Explore community-contributed questions and answers, and download ready-to-run source code projects.

Quick Links

  • Home
  • Questions & Answers
  • Download Projects
  • Blog & Articles
  • Member Login

Community

  • Telegram Group
  • WhatsApp Community
  • Ask a Question
  • Join as Member

Connect With Us

Stay updated with our newest programming tutorials and free open source projects.

© 2026 Easy To Learning. All Rights Reserved.