Displaying a single record

In cold fusion I can display mysql data as employee.firstname but every where that I have look…they use a while loop or another loop. Is it really necessary to use a while loop to display one record from the database? I understand using a while loop if I wanted to display a bunch of records but using a while loop to display one record seems a bit of an overkill. So my question, is there an easy way to display one record in php? If yes, can you point me in the right direction, examples etc.
Thanks,
Eva

Both Mysqli and PDO have fetchAll functions where you (as the name implies) fetch all records at once instead of writing a loop. Look through my PDO tutorial as this is used there :slight_smile:

I looked through your tutorials in your sig. I don’t see a fetchall but I don’t know what I am looking for so if you could let me know where I can find the fetchall tutorial that would help. I will go bing fetchall in the meantime.
Eva

I did several searches on fetchall. Every single example that I saw used either a while or foreach loop. If that is the case then there is no point in using fetchall since it uses a loop also. I might as well just use a while loop for the single record.

Eva

Then they are doing it incorrectly
http://php.net/manual/en/pdostatement.fetchall.php

If you later on want to print/echo something for each result row then you will of course have to loop through the result array. But you do not have to loop to fetch data.

The fact you have to loop through it doesnt mean its not worth it. Good coding standards say you should split the logic and the presentation. So you should fetch the data from stores (databases/apis/other), build variables and arrays and afterwards present it in a view.

Sponsor our Newsletter | Privacy Policy | Terms of Service