When the database has some records in it, you can retrieve the stored information with the SELECT statement.
A SELECT query returns rows selected from one or more tables.
SELECT column_name(s) FROM table_name- "column_name(s)" - is the name of the column (or columns) you want to select.
<?php // connect to the "tests" database $conn = new mysqli('localhost', 'root', 'pass', 'tests'); // check connection if (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); } // SELECT sql query $sql = "SELECT `id`, `name`, `pass` FROM `users`"; // perform the query and store the result $result = $conn->query($sql); // if the $result contains at least one row if ($result->num_rows > 0) { // output data of each row from $result while($row = $result->fetch_assoc()) { echo '<br /> id: '. $row['id']. ' - name: '. $row['name']. ' - pass: '. $row['pass']; } } else { echo '0 results'; } $conn->close(); ?>- This example stores the data (the result object) returned by the query() method in the $result variable, uses the "num_rows" method to check if $result contains at least one row. Then, with a while() loop loops through all the records in the result object, using the "fetch_assoc()" method to store the data of each row in an Array in the $row variable.
The asterisk (*) can be used to select all column: "SELECT * FROM table_name", but is better to be explicit about which columns are selected. The selecting process can be faster if only the columns you will use are fetched.
SELECT column_names FROM table_name LIMIT skip_count, show_count
"skip_count" is optional, it tells the database how many rows to skip from results.
<?php
// connect to the "tests" database
$conn = new mysqli('localhost', 'root', 'pass', 'tests');
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// SELECT sql query
$sql = "SELECT `id`, `name`, `pass` FROM `users` LIMIT 2";
// perform the query and store the result
$result = $conn->query($sql);
// if the $result contains at least one row
if ($result->num_rows > 0) {
// output data of each row from $result
while($row = $result->fetch_assoc()) {
echo '<br /> id: '. $row['id']. ' - name: '. $row['name']. ' - pass: '. $row['pass'];
}
}
else {
echo '0 results';
}
$conn->close();
?>
The "LIMIT 2" option tells MySQL to return only the firs 2 rows in result.SELECT column_names FROM table_name ORDER BY col_name ASC|DESC"col_name" can be a single column, a comma separated list of columns, or an expression such as RAND(), which randomizes the order.
<?php
// connect to the "tests" database
$conn = new mysqli('localhost', 'root', 'pass', 'tests');
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// SELECT sql query
$sql = "SELECT `id`, `name`, `pass` FROM `users` ORDER BY `name`";
// perform the query and store the result
$result = $conn->query($sql);
// if the $result contains at least one row
if ($result->num_rows > 0) {
// output data of each row from $result
while($row = $result->fetch_assoc()) {
echo '<br /> id: '. $row['id']. ' - name: '. $row['name']. ' - pass: '. $row['pass'];
}
}
else {
echo '0 results';
}
$conn->close();
?>
- This code selects all the data stored in the "users" table, and sorts the result in alphabetical order by the "name" column.SELECT DISTINCT column_names FROM table_name
<table><tr> <th>Title 1</th> <th>Title 2</th> </tr></table>
.some_class { line-height: 150%; }
document.getElementById("id_button").onclick = function(){ window.open("http://coursesweb.net/"); }
$ar_dir = scandir("dir_name"); var_export($ar_dir);