PHP MySQL get Data | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

PHP MySQL get Data

I do fetch data like this <?php $conn = new mysqli(data for host); $query = "SELECT text FROM table WHERE id=1"; $query1 = "SELECT text FROM table WHERE id=2"; $result = $conn->query($query) or die ($conn->error); $result1 = $conn->query($query1) or die ($conn->error); $row = mysqli_fetch_assoc($result); $row1 = mysqli_fetch_assoc($result1); echo $row['text']; echo $row1['text']; ?> I need each row on a different spot in the html page for getting data automaticly when changed in the database wehre I get with the ID. If i've 20 entries I nee to do this 20 times. Is there any better solution? If yes would be amazing for some help im kinda new.

14th Sep 2020, 8:13 AM
Marco Müller
Marco Müller - avatar
6 Answers
+ 1
Have you used a loop before? $conn = new mysqli(data for host); $query = "SELECT text FROM table"; // just don't filter down to only 1 record. $result = $conn->query($query) or die ($conn->error); while($row = mysqli_fetch_assoc($result)) { // loop through all records of result. echo $row['text']; }
14th Sep 2020, 9:00 AM
Josh Greig
Josh Greig - avatar
+ 1
ok so you have some very specific id's associated with these different parts of the page? It sounds like your main goal would be a minimum amount of duplicated code around each hard-coded ID then. For that, you could do something like: <?php $conn = new mysqli(data for host); $query = "SELECT id, text FROM table"; // just don't filter down to only 1 record. $result = $conn->query($query) or die ($conn->error); // Copy all the database data into a data structure that is more efficient to look up for individual records. This should reduce the number of MySQL connections and queries you make while rendering the page. $idToText = array(); while($row = mysqli_fetch_assoc($result)) { // loop through all records of result. $idToText[$row['id']] = $row['text']; } function printTextFor($id) { echo $idToText[$id]; } ?> <html> <head> </head> <body> <header> <nav></nav> <?php printTextFor(1); ?> </header> <main> <h1><?php printTextFor(3); ?></h1> </main> <footer> <?php printTextFor(2); ?> </footer> </body> </html> The most valuable part of this is you have printTextFor(x) which is very short for the many calls you make to it. Within the function you could requery the database for the parameterized id too. That would be a little less efficient but there are many ways to do it. If you really have no other PHP snippets in the page and these database-driven pieces of text are really all you have, you could do something else too. You could add template symbols throughout your page's HTML and fill them in using your database query. For example, <h1>#1#</h1> <footer>#2#</footer> and then loop through all those symbols and replace. The benefit here would be that your repeated references to each id get even smaller. It is a little extreme because your flexibility to use PHP for other kinds of content also drops.
15th Sep 2020, 12:49 AM
Josh Greig
Josh Greig - avatar
0
I did, but the problem i need each ID seperated. Thats why I did WHERE id=number. While loop is good for tables, but i dont use table just a single text on the frontpage on different spots.
14th Sep 2020, 11:14 PM
Marco Müller
Marco Müller - avatar
0
Thanks for this answer. This helped me alot.
15th Sep 2020, 2:34 AM
Marco Müller
Marco Müller - avatar
0
I tested the code now, but gives me the result undefined variable "idToText" in the printTextFor function.
16th Sep 2020, 3:12 PM
Marco Müller
Marco Müller - avatar
0
You commented: "I tested the code now, but gives me the result undefined variable "idToText" in the printTextFor function." Answer: Try using global to make the global $idToText variable available in the function. I didn't test my previous answer. $idToText = array(); while($row = mysqli_fetch_assoc($result)) { // loop through all records of result. $idToText[$row['id']] = $row['text']; } function printTextFor($id) { global $idToText; // THIS IS THE NEW LINE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11 echo $idToText[$id]; }
17th Sep 2020, 4:55 AM
Josh Greig
Josh Greig - avatar