Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


boostr.in questions - Question:how to Select-query Data-base From a Data-base Table using php mysql?


Select Data From a Database Table

The SELECT statement is used to select data from a database.

Syntax

SELECT column_name(s)
FROM table_name

To get PHP to execute the statement above we must use the mysqli_query() function. This function is used to send a query or command to a MySQL connection.

Example

The following example selects all the data stored in the "Persons" table (The * character selects all the data in the table):

$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM Persons");

while($row = mysqli_fetch_array($result))
  {
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "
";
  }

mysqli_close($con);
?>

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

Next, we use the mysqli_fetch_array() function to return the first row from the recordset as an array. Each call to mysqli_fetch_array() returns the next row in the recordset. The while loop loops through all the records in the recordset. To print the value of each row, we use the PHP $row variable ($row['FirstName'] and $row['LastName']).

The output of the code above will be:

Peter Griffin
Glenn Quagmire



Display the Result in an HTML Table

The following example selects the same data as the example above, but will display the data in an HTML table:

$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM Persons");

echo "
<tr>


";

while($row = mysqli_fetch_array($result))
  {
  echo "";
  echo "";
  echo "";
  echo "";
  }
echo "
Firstname Lastname
" . $row['FirstName'] . "" . $row['LastName'] . "
";

mysqli_close($con);
?>

 


asked Sep 13, 2013 in PHP by rajesh
edited Sep 12, 2013
0 votes
25 views



Related Hot Questions



Walkin Jobs Opening



Government Jobs Opening


...