I want to write HTML code that displays the last five rows from the database,
I have code which shows the last row, but I want to have the last five rows.
My code:
<?php
$conn = mysqli_connect("localhost", "root", "", "refd-2");
$sql = mysqli_query($conn, "SELECT * FROM last ORDER BY no DESC LIMIT 1");
$print_data = mysqli_fetch_row($sql);
echo $print_data[1];
echo "\n";
You can get the last 5 results from DB if you specify LIMIT 5.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$con = new mysqli("localhost","root","","refd-2");
$con->set_charset('utf8mb4');
$result = $con->query("SELECT * FROM last ORDER BY no DESC LIMIT 5");
// Get all rows at once as an array of rows
$rows = $result->fetch_all();
print_r($rows);
// or use foreach loop
foreach($rows as $row) {
print_r($row);
}
The fetch_all() method can be skipped if not needed and you can loop directly on the results.
foreach($result as $row) {
print_r($row);
}
try this :
<?php
$conn = mysqli_connect("localhost", "root", "", "refd-2");
$sql = mysqli_query($conn, "SELECT * FROM last ORDER BY no DESC LIMIT 5");
while($print_data = mysqli_fetch_array($sql)){
echo $print_data["row"];
// here you will change the name "row" to your database row name
echo "\n";
}
?>
Change LIMIT 1 with LIMIT 5 and fetch your resultset.
<?php
//database connectivity
$con=mysqli_connect("localhost","root","","refd-2") or die(mysqli_error());
//select values from empInfo table
$sql = "SELECT * FROM last ORDER BY no DESC LIMIT 5'";
$result = mysqli_query($con,$sql);
print_r(mysqli_fetch_array($result));
mysqli_close($con);
?>
Related
I am trying to count the total number of rows in a single table. Just need to know how many rows total and nothing else. I am using Oracle database.
I have tried
$sql = "SELECT COUNT(*) AS homes FROM homes";
But I am unsure on how to display the result.
$sql = "SELECT COUNT(*) AS count FROM homes";
To count the rows from
$sql = "SELECT * FROM homes"; // fetch the rows
$sql1=mysql_query($sql);
$count=mysql_num_rows($sql1) // count number of rows in table
echo $count; //$count has the value of rows in table..
Use this code:
// Code goes here
<?php
$sql = "SELECT COUNT(*) AS homes FROM homes";
$result = mysqli_query($sql);
$row = mysqli_fetch_array($result);
print($row['homes']); // prints count
?>
<?php
$link = mysqli_connect("host", "username", "password");
mysqli_select_db("database", $link);
$result = mysqli_query("SELECT * FROM homes", $link);
$num_rows = mysqli_num_rows($result);
echo "$num_rows Rows\n";
?>
How to display only one row at random at the same time from DB. Everything works fine, but all rows are displayed. thanks
<?php
$sql = "SELECT id,name FROM table ";
$rows = array();
$result = $objCon->query($sql);
while($row = $result->fetch_assoc())
{
$rows[] = $row;
}
shuffle($rows);
echo '<ol>';
foreach($rows as $row)
{
echo '<li><h3>'.$row['id'].' = '.$row['name'].'</h3></li>';
}
echo '</ol>';
?>
Change your SQL request:
SELECT id,name FROM table ORDER BY RAND() LIMIT 1;
You can do it using PHP:
....
shuffle($rows);
$randomRow = reset($rows);
....
But the better way is to change your SQL query:
$query = "SELECT id, name FROM table ORDER BY RAND() LIMIT 1;"
<?php
$sql = "
SELECT id, name
FROM table
ORDER BY RAND()
LIMIT 1 ";
$result = mysql_query($sql);
// As you are only return a single row you do you require the while()
$row = mysql_fetch_array($result);
echo '<ol>';
echo '<li><h3>'.$row['id'].' = '.$row['name'].'</h3></li>';
echo '</ol>';
?>
By adding an ORDER BY RAND() in your sql query you are asking MySQL to randomly order the results then at a LIMIT to restrict the number of rows you would like returned.
The example code is written based on selecting a single row. If you would like more, e.g. 5, you will need to add a while loop.
i'm having a slight problem with this mysql query.
$sql = "SELECT name FROM videos ORDER BY counter DESC LIMIT 5";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$list = mysql_fetch_assoc($result);
while($list = mysql_fetch_assoc($result)){
echo $list['counter'];
echo "<br>";
}
mysql_free_result($result);
Want i'm wanting to achieve is it listing 5 entrys by order "Counter" But when listing it? it only shows 4 entrys like so :-
5648
4575
1595
35
So where is my 5th entry? and why isn't it posting it? NOTE that the 5th entry is also the highest with a value of
305355
Thanks in advance
You fetch before the loop which pops one record off the result set (i.e. 305355).
$list = mysql_fetch_assoc($result); // REMOVE THIS LINE
while($list = mysql_fetch_assoc($result)) {
// output code
}
Try to reduce the code and use mysqli_ functions
$sql = "SELECT name FROM videos ORDER BY counter DESC LIMIT 5";
$result = mysql_query($sql, $conn);
while($list = mysql_fetch_assoc($result)){
echo $list['counter']."<br>";
}
try out this code..
$sql = "SELECT name FROM videos ORDER BY counter DESC LIMIT 5";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
while($list = mysql_fetch_assoc($result)){
echo $list['counter'];
echo "<br>";
}
mysql_free_result($result);
you are calling mysql_fetch_assoc two time that is why your first row get by first time you called the mysql_fetch_assoc and remaining show in second calls
I need to simply Pull 4 table rows from MySQL table and display with PHP on a page? Can someone help me with this code... All I have is:
include_once ('db.php');
$link = mysql_connect($db,$user,$pass) or die("Can't connect to Database");
mysql_select_db($table,$link);
/* Main */
$query="SELECT * FROM $subtable ORDER BY id DESC LIMIT 1";
$result=mysql_query($query);
HTML
<?php echo mysql_result('tablerow'); ?>
Here is some code that should get you started!
$link = mysql_connect($host,$user,$pass,$table);
$loop = mysql_query($link,"SELECT * FROM $subtable ORDER BY id DESC LIMIT 4");
while ($row = mysql_fetch_array($loop))
{
echo $row['yourrow']."<br/>";
}
mysql_close($link);
Just replace yourrow with the name of the row that you want to print out! Don't forget to set the variables $host $user $pass and $table
How do I fetch only one value from a database using PHP?
I tried searching almost everywhere but don't seem to find solution for these
e.g., for what I am trying to do is
"SELECT name FROM TABLE
WHERE UNIQUE_ID=Some unique ID"
how about following php code:
$strSQL = "SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID";
$result = mysql_query($strSQL) or die('SQL Error :: '.mysql_error());
$row = mysql_fetch_assoc($result);
echo $row['name'];
I hope it give ur desired name.
Steps:
1.) Prepare SQL Statement.
2.) Query db and store the resultset in a variable
3.) fetch the first row of resultset in next variable
4.) print the desire column
Here's the basic idea from start to finish:
<?php
$db = mysql_connect("mysql.mysite.com", "username", "password");
mysql_select_db("database", $db);
$result = mysql_query("SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID");
$data = mysql_fetch_row($result);
echo $data["name"];
?>
You can fetch one value from the table using this query :
"SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID limit 1"
Notice the use of limit 1 in the query. I hope it helps!!
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["name"]."<br>";
}
} else {
echo "0 results";
}
$conn->close();