MYSQL - SELECT Query

MYSQL SELECT Query is used to fetch existing records from database.

  • You can fetch one or more data from table.
  • If You specify star (*) in place of fields, SELECT will return all the fields.
  • You can use any condition using the WHERE clause.
  • You can limit the number of returns record using the LIMIT attribute.
  • You can display records in asceding and descending order by ORDER BY Clause.
  • The GROUP BY Clause is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result set by one or more columns.


MYSQL Syntax :

SELECT column1, column2,  column3, .....
FROM table_name
[WHERE Clause] [GROUP BY FIELD][ORDER BY FIELD][LIMIT 0,N];

MYSQL Query Example :
SELECT * FROM tblstudent

Shell command script:
Also you can run select query using mysql command Prompt.
[root@host]# mysql -u root -p
Enter password:********

mysql> use aryatechno;
Database changed

mysql> SELECT * FROM tblstudent
Query OK, 1 row affected (0.006 sec)



PHP SCRIPT :
You can fetch records from table using php code as per as below.

<?php
$hostname="localhost";
$username="root";
$password="********";
$link= mysqli_connect($hostname, $username, $password);
mysqli_select_db($link,DATABASE);
$rs = mysqli_query($link,"SELECT * FROM tblstudent");
while($raw=mysqli_fetch_array($rs)) {
          echo "<br>Student Name : ".$raw['stud_name'];
          echo "<br>Student Mobile : ".$raw['stud_mobile'];
          echo "<br>Student Address: ".$raw['stud_address'];
          echo "<br>Student Birthdate: ".$raw['Stud_birthdate'];
          echo "<br>----------------------------------------------- ";

}
mysqli_close($link);
?>

Explain :
We can use below four php function to fetch records from table.
1. mysqli_fetch_array(resultset); - It returns the row as a numeric array and as an associative array
2. mysqli_fetch_assoc(resultset); - It returns the row as an associative array.
3. mysqli_fetch_object(resultset); - It returns the row as an object.
4. mysqli_fetch_row(resultset); - it returns the row as an enumerated array.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

97650