Virtual Open Schooling Online courses available at Virtual Open Schooling (VOS), for more details kindly click here!
NIOS Silver Jubilee Year
NIOS

Select Statement

From NIOS
Jump to: navigation, search
Previous Page Home Page Next Page
ICT Applications


Select Data from a Database Table

The SELECT statement is used to selectdata from a database.


Syntax

SELECT column_name(s)

FROM table_name


To get PHP to execute the statementabove we must use the mysql_query() function. This function is usedto send a query or command to a MySQL connection.

Example



<?php

$con =mysql_connect("localhost","userid","abc123");

if (!$con)

{

die('Could not connect: ' .mysql_error());

}

mysql_select_db("my_db",$con);

$result = mysql_query("SELECT *FROM Contacts");

while($row =mysql_fetch_array($result))

{

echo $row['FirstName'] ." " . $row['LastName'];

echo "<br/>";

}

mysql_close($con);

?>


The following example selects all thedata stored in the "Contacts"

The example above stores the datareturned by the mysql_query() function in the $result variable.


Wecan use themysql_fetch_array() function to return the first row from therecordset as an array. The while loop loops through all the recordsin the recordset. To print the value of each row, we use the PHP $rowvariable ($row['FirstName'] and $row['LastName']).


The output of the code above will be:


Ajay Kumar

ABC XYZ

Display the Result in an HTML Table

The following example selects the samedata as the example above, but will display the data in an HTMLtable:


<?php

$con =mysql_connect("localhost","userid","abc123");

if (!$con)

{

die('Could not connect: ' .mysql_error());

}

mysql_select_db("my_db",$con);

$result = mysql_query("SELECT *FROM Contacts");

echo "<tableborder='1'>

<tr>

<th>Firstname</th>

<th>Lastname</th>

</tr>";

while($row =mysql_fetch_array($result))

{

echo "<tr>";

echo "<td>". $row['FirstName'] . "</td>";

echo "<td>". $row['LastName'] . "</td>";

echo "</tr>";

}

echo"</table>";

mysql_close($con);

?>


Output on page


FirstnameLastname

AjayKumar

ABCXYZ

Update Data in a Database

The UPDATE statement is used to updateexisting records in a table.


Syntax


UPDATE table_name

SET column1=value,column2=value2,..

WHERE some_column=some_value

Example


<?php

$con =mysql_connect("localhost","userid","abc123");

if (!$con)

{

die('Could not connect: ' .mysql_error());

}

mysql_select_db("my_db",$con);

mysql_query("UPDATE Contacts SETPhone=123456

WHERE FirstName='Ajay' ANDLastName='Kumar'");

mysql_close($con);

?>

Delete Data in a Database

The DELETE FROM statement is used todelete records from a database table.


Syntax


DELETE FROM table_name

WHERE some_column = some_value

Example


<?php

$con =mysql_connect("localhost","userid","abc123");

if (!$con)

{

die('Could not connect: ' .mysql_error());

}

mysql_select_db("my_db",$con);

mysql_query("DELETE FROM ContactsWHERE LastName='Kumar");

mysql_close($con);

?>


Previous Page Home Page Next Page
ICT Applications