I'm using MySQL to show data on my website: http://www.onlinedealsindia.in. You can see the deals (deal boxes) there but I want to limit them. I want only 50 deals (deal boxes) to show up per page. Clicking the more button would show the next 50 deals.
Below is my code to get data from MySQL:
<?php
$host="localhost";
$username="username";
$password="pass";
$dbname="DB NAme";
$conn=new mysqli($host, $username, $password, $dbname);
if($conn->connect_error)
{
die("error".$conn->connect_error);
}
else { echo "";}
$sql= "SELECT * FROM table ORDER BY p_id DESC";
$results=$conn->query($sql);
if($results->num_rows > 0)
{ while($row=$results->fetch_assoc())
{ $pid=$row["p_id"];
?>
The mechanic you are looking for is called skip and take.
In mySQL you can use the LIMIT to accomplish this. With one parameter it returns that many rows. With two parameters, it will skip the first number in rows and return the number of rows for the second number.
This SQL would return 20 rows:
$sql= "SELECT * FROM table LIMIT 10 ORDER BY p_id DESC";
This SQL would return the ten rows skipping the first 10
$sql= "SELECT * FROM table LIMIT 20,10 ORDER BY p_id DESC";
Documentation and examples of LIMIT can be found here
To make this work for your website just pass a parameter that tells what page you are on and how many rows per page. You can then calculate the two numbers you need for your select statement.
$mysqli = new mysqli($host, $username, $password, $dbname);
if($mysqli->connect_error)
{
die("error".$mysqli->connect_error);
}
else {
echo "";
}
$datas = $mysqli->prepare('SELECT id FROM table LIMIT 50 ');
if($datas)
{
$datas->execute();
$datas->bind_result($id);
while($datas->fetch)
{
$store['id']= $id;
$results[] = $store;
}
return $results;
}
foreach($results as $result)
{
echo $result['id'];
}
Related
i am trying to display some values from database to my html calling the last inserted id
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * from registers where id= LAST_INSERT_ID()";
$result = $conn->query($sql);
if (!empty($result) && $result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo " $row[firstname] ";
}
} else {
echo "0 results";
}
But when I am using this, the result is not being displayed, is there any problem with my code, how do I retrieve the last inserted id from sql?
try this query
$sql = "SELECT * from registers ORDER BY ID DESC limit 1 ";
it will give you only one result and this result will be the last inserted ID.
But ID needs to be auto increment.
To retrieve the last id in the table you need:
SELECT id FROM registers ORDER BY ID DESC LIMIT 1
I'm trying to loop through each row of a table in a database, then once I'm on a particular row get the value of a certain column. Is this possible? I've done a couple Google searches but nothing really concrete. I try using the mysqli_fetch_array() function but when I do I get the results of a column. I want to target each row. The code I have so far gets me the "nid" column. That's not what I want. I want to iterate through each row.
<?php
$serverName = "localhost";
$username = "user1";
$password = "sp#99#1";
$databaseName = "developer_site";
// Connection
$connection = new mysqli($serverName, $username, $password, $databaseName);
// Check Connection
if ($connection->connect_error) {
die("Connection failed:" . $connection->connect_error);
} // line ends if statement
$queryNodeRevision = "SELECT nid FROM node_revision";
// line above creates variable $queryNodeRevision > selects column "nid" from table "node_revision"
$results = mysqli_query($connection, $queryNodeRevision) or die("Bad Query: $results");
while ($row = mysqli_fetch_array($results)) {
echo "NID: ";
echo $row['nid'];
echo "<br/>";
}
?>
You can select rows by a certain condition with an SQL query alone and also select all columns.
SELECT * FROM node_revision where condition;
condition could be anything. For example another_row = 'something'.
But if, for some reason, you need to process the nid inside your php script to know if that row is the "particular" one you're searching, then you just select all the columns, or the ones you need.
$queryNodeRevision = "SELECT nid, col1, col2 FROM node_revision";
$results = mysqli_query($connection, $queryNodeRevision) or die("Bad Query: $results");
while ($row = mysqli_fetch_array($results)) {
if (condiiton) {
echo "Particular: ".$row['nid']
." ".$row['col1']
." ".$row['col2'];
}
}
condition could be something like $row['nid'] == 123 for example.
Hope it helps.
I have a page that is displaying a list of all company names (45 of them to be exact).
The database table has 43,815 results currently, so I only want to display them without duplicates (which is why I'm using the DISTINCT when I select) but I would also like to count how many results per company has and echo them out.
I'm tried using the count() as but it removes all the company results and just places the total (43,815).
My question is how would I display the companies using DISTINCT (because I don't want to have duplicates on the page) and then also echo out the total results of each company?
Any help is appreciated!
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
// Connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT DISTINCT company FROM ads ORDER BY company ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
// Display the company name
echo $row["company"];
// I WOULD LIKE TO ECHO OUT HERE THE TOTAL OF EACH COMPANY HAS (COUNT) IN RESULTS!
}
} else {
echo "0 results";
}
$conn->close();
?>
I think that you should probably try to use the GROUP BY clause.
Without knowing your table markup try playing around with this:
$query = "SELECT
count(company) as count
FROM ads
GROUP BY company
ORDER BY company ASC"
Try:
SELECT
company,
COUNT(company)
FROM
ads
GROUP BY
company
ORDER BY
company
Use GROUP BY on your SELECT query, this should do what you are looking for, this will group all the same company's together and count them.
"SELECT COUNT(company) FROM ads GROUP BY company ORDER BY company ASC"
I have a page that displays entered data from a database in a table. I am wondering how I can reverse the order displayed from newest to oldest.
Here is the code for the fetch page that displays my results in the table, html has been removed as it is irrelevant to the question.
<?php
// Create connection in mysqli
$connection = new mysqli($server, $user, $pass, $dbname);
//Check connection in mysqli
if($connection->connect_error) {
die("Error on connection:" .$connection->connect_error);
}
//Display the informaion
$sql = "SELECT * FROM logs";
$res = $connection->query($sql);
if($res->num_rows > 0) {
// echo table row code
while($row = $res->fetch_assoc()) {
// echo table row code
}
}
else {
echo "No Record Found!";
}
$connection->close();
?>
Simply modify your query like this :
$sql = "SELECT * FROM logs ORDER BY `column` DESC";
Where column is your column with the primary key. Normaly it is ID or id...
I am trying to make simple page which will return values from MySQL table, but the problem is that if I want to use condotions in query then it doesn't work.
My PHP page:
<?php
$servername = "10.10.10.10";
$username = "username";
$password = "password";
$dbname = "GENERIC_TABLES";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT WO_NUM+1 from GENERIC_TABLES.WO_NUMBERS ORDER BY WO_NUM DESC limit 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> WO Number ". $row["WO_NUM"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
So, WO_NUM column has numbers like 1, 2, 3 etc.
I want to get the last one + 1
So if I do like:
$sql = "SELECT WO_NUM from GENERIC_TABLES.WO_NUMBERS ORDER BY WO_NUM DESC limit 1";
Then it works fine, but if I want to make it WO_NUM + 1 then it returns nothing.
Why it happens like that and is there any way to get what I want using MySQL?
I don't want to get WO_NUM and then using PHP make it + 1, since I also need INSERT to the table values and I would like to understand why it doesn't work.
As you realized, WO_NUM+1 changes the column name in the resulting array, so use an alias WO_NUM+1 as NEW_NUM. However I would not bother with the sorting and limit. Consider MAX():
SELECT max(WO_NUM)+1 as NEW_NUM from GENERIC_TABLES.WO_NUMBERS
As it has been pointed by AbraCadaver, I missed that if I am using WO_NUM + 1 then column name changing so that is why i didn't get any output, so using alias solved problem.