-4

In my database, there is a table named CAT with a column named cat_id.

I need to fetch the cat_id field from the CAT table row by row without using a while loop.

This is what I have so far...

$catid = mysqli_query($con,"select * from cat");
$fetchcat = mysqli_fetch_assoc($catid);

Now I need data one by one like below...

<li><a href="<?php echo $fetchcat['cat_id']; ?>"></a>Men</li>
<li><a href="<? what can i type here to get 2nd row??? ?>"></a>Men</li>

Can someone suggest the next step? Thanks

Rahul Kumar
  • 1
  • 1
  • 3
  • 1
    It is unclear why you don’t want to use a loop. There are several rows to display so you will have to loop, one way or another (either by fetching all results into an array and then looping over the array, or by fetching rows 1 by 1) – GMB Dec 02 '18 at 16:01
  • 1
    There is a reason for me to not to use loop... But it is a big point to tell you.. But i got you answer can you please suggest a example code how can i get rows in array n then i can print one by one.... I am just a beginner in php i cant learn without any example n i really appreciate that you answered my question....... It will help me if you'll show me some code example please...... Thanks in advance – Rahul Kumar Dec 02 '18 at 17:19
  • Ok I added a simple example as an answer – GMB Dec 02 '18 at 20:59
  • _"There is a reason for me to not to use loop"_ so what is it? This question does not make any sense without it. Also, are you forbidden from using any sort of iterative statement or only `while` loops specifically? – Phil Dec 03 '18 at 01:29
  • Reason is why i don't want to use any loop is... That i have a critical design wrote in html if i use the loop that design will no longer work, i do not want to change my design...... I really need a solution alternative of loop.... Cause there should be any code to fetch row one by one, there may be some logic we can put to use fetch query without loop..... – Rahul Kumar Dec 03 '18 at 03:29
  • Gmb sir this is still a loop i don't want to use loop this is the only way for me....... Please let me know if you can help me using it without loop – Rahul Kumar Dec 03 '18 at 03:35
  • What **exactly** is not working with the given code? What have you tried to make it work? – Nico Haase Aug 19 '22 at 13:16

2 Answers2

0

One way or another, you have to loop... The most efficient option is usually to run the query, then to fetch rows one by one.

Here is a code sample to achieve that. Please note that since the only column you need to output is cat_id, it’s more efficient to select only that column in you sql statement rather than all available columns with select *.

$result = mysqli_query($con,"select cat_id from cat");
while ($row = mysql_fetch_assoc($result)) {
    ?><li><a href="<?php echo $row['cat_id']; ?>"></a>Men</li><?php
}
GMB
  • 216,147
  • 25
  • 84
  • 135
  • Excuse my imprudence, but if we say without using while is because is important to do it. The example is not exactly the code we are trying – Mister JJ Mar 27 '22 at 21:50
-2

That is very easy, take out the while, like below

$row = mysqli_fetch_assoc($result); echo $row['cat_id'];

Noe
  • 1
  • 1