Forum Moderators: coopster

Message Too Old, No Replies

Extract Multiple but access only the first result

Identify only the first row of result

         

mvaz

10:38 pm on Dec 2, 2019 (gmt 0)

10+ Year Member



Hey guys, it's been a while since I've been here, and a lot has changed, php has moved from 5 to 7 and with it, has given me new challenges and I am stuck on one such challenge.
I'm trying to run a query my MySql table, with the following code to get 7 random records:

$conx = mysqli_connect('localhost', 'xxxxxxx', 'xxxxxxxx', 'xxxxxxxxx');
$sql = "SELECT * FROM `messages` ORDER by rand() LIMIT 7;";
$result = mysqli_query($conx, $sql);
$c = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)){....and so on.

And, so far so good. Now, how am I to go about if I am to identify what the first record in this query is?

I've tried and looked for answers, but couldn't get any results.

I was wondering if the learned and experienced in here would assist or guide me on this new learning path.

Cheers.

phranque

11:35 pm on Dec 2, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



/* seek to row no. 1 */
mysqli_data_seek($result, 0);

/* fetch row */
$row = mysqli_fetch_row($result);

topr8

12:13 am on Dec 3, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



to me, the question isn't clear. what do you mean by identify what the first record in the query is?

did you mean, return the first row (of the seven random rows)? in which case phranque answered you.

or did you mean something else?

mvaz

12:16 am on Dec 3, 2019 (gmt 0)

10+ Year Member



Hi phranque, thanks for your swift response. Hi topr8 and Phranque, It appears that I wasn't clear with my original problem.

So, I extract 7 rows from the database and then I want to display them one after another (I've achieved this with no issues). But now, on the 8th row, I want the first row to be displayed again. I was wondering how can I achieve this?

phranque

7:08 am on Dec 3, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



on the 8th row, I want the first row to be displayed again. I was wondering how can I achieve this?

i haven't tried it but i'm pretty sure that's the answer i gave above.

mvaz

8:21 am on Dec 3, 2019 (gmt 0)

10+ Year Member



Thank you very phranque, that worked as I intended it to. Your help is very much appreciated. Cheers!

topr8

11:03 am on Dec 3, 2019 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



... great, you got it working!