Jump to content

Loading items from SQL database in HTML / PHP

Pierre Dabrunst

So, I am trying to do a "dynamic" loading part of my website (hosting business related) where it displays total clients, total tickets resolved etc.
The data is to be fetched from SQL where it displays the highest number.  Each SQL table has a ID to it (separate from client ID) and it is that field I want to load.

The field is called "id" and I want the code to load the highest number.  So if there are an ID number "10", that number will be displayed, and if a new client registers, the new ID will become "11" and then that number should be displayed.

 

 

How can I make this work as efficient as possible?

 

CPU: Intel Core i3 4150 3.5GHz Socket 1150 GPU: MSI GeForce GTX 760 OC RAM: Corsair Vengeance 2x4GB Motherboard: Gigabyte Z97MX-Gaming 5

Link to comment
Share on other sites

Link to post
Share on other sites

SELECT * FROM table_name ORDER BY ID DESC LIMIT 10;

Select full records (all columns) from the table table_name, order the table by column ID, in DESC -ending order,  limit results to the first 10.

If you want next 10 results, you could say   WHERE id < last_id_in_previous_query ORDER BY ID DESC LIMIT 10   ,  or you could use LIMIT 10, 10   (start from 10th result, return 10 results)

Add filters  ex  WHERE user_id=100 before the ORDER keyword.

 

 

use mysqli_fetch_assoc or whatever   : https://www.php.net/manual/en/mysqli-result.fetch-assoc.php

 

For a more advanced stuff... you could make a small REST API where your API returns results as JSON objects, then you could also dynamically retrieve records using Javascript in your pages, and refresh tables and results without having to reload the page.  

 

Tip:  Use the supplied HeidiSQL  (or download it, it's freeware) that comes with MariaDB (open source MySQL) and browse your tables and look down at the queries HeidiSQL builds. You can learn the syntax and some tricks faster that way.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×