LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-27-2016, 04:30 PM   #1
qajaq
Member
 
Registered: Sep 2008
Location: north-central Florida
Distribution: Lubuntu 19.04, Kubuntu 18.04
Posts: 120

Rep: Reputation: 15
PHP foreach () function on query result returns only the last record in the table.


I've used PHP's mysql and mysqli procedural code for years, but I'm trying to learn how to use OOP. One of the simple projects I'm working on needs to return records from a MySQL database, but so far I'm not getting the results I expected.

I have a database in place with several tables, one of which is MtgsCities. I have used this database and table successfully in the past, and I have checked to confirm that it still holds 43 records, with only one field (of varchar(24) type) in each record. That solitary field is mtgCity.

Having established a connection handler ($cnxn), I run the following four lines of code:
Code:
foreach ($cnxn->query("SELECT * FROM MtgsCities") as $row);
{
  echo "The city is " . $row["mtgCity"] . ".<br />";
}
The result prints out only one line:
Quote:
The city is Worthington Springs.
Clearly the routine is reading the table, but is displaying only the last record in the table. I'm obviously missing something, but all the online searching I've done has not revealed anything helpful.

Any ideas where I can go from here?
 
Old 10-27-2016, 06:47 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Try your code without the semicolon at the end of the foreach line.

Long ago, when first learning C++, I read Bruce Eckel's books and he recommends placing the opening brace on the same line as the loop statement.
Code:
foreach ($cnxn->query("SELECT * FROM MtgsCities") as $row);{
    echo "The city is " . $row["mtgCity"] . ".<br />";
}
Makes it pretty obvious that the semicolon doesn't belong, doesn't it?

It's heretical I know, but I have avoided this common (and sometimes hard to find) problem since.

Last edited by norobro; 10-27-2016 at 08:49 PM. Reason: typo
 
Old 10-27-2016, 08:13 PM   #3
qajaq
Member
 
Registered: Sep 2008
Location: north-central Florida
Distribution: Lubuntu 19.04, Kubuntu 18.04
Posts: 120

Original Poster
Rep: Reputation: 15
Thank you, norobro! I can't tell you how many times I re-read those four lines looking for some kind of simple typo - and missed that extraneous semi-colon every time!

You know, I used to put the opening brace on the same line of all my loop and conditional statements, but changed to this format after reading a book about PHP and OOP. You're right - it does make the meddlesome semi-color so obvious! I may just change back to the way I used to do things.

Thanks again!
 
  


Reply

Tags
foreach, mysql, php5



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP-7: #2 mysqli query returns "no results" unless #1 query is closed first sundialsvcs Programming 3 05-20-2016 06:44 AM
How do I output the result of a query in php? orfiyus Programming 0 07-19-2007 02:33 PM
select query thru php outputs no result suchi_sood Programming 1 04-11-2006 10:21 AM
PHP Mail and Foreach function Gerardoj Programming 6 10-08-2004 05:24 PM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration