473,516 Members | 3,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loops

Hi I have this this code
{while ($row = mysql_fetch_array($numresultsgal)) {
$table=$row{'table_gal'};
$numresultphotos=mysql_query("SELECT * FROM" .$table. "order by date desc");
$numphotos=mysql_num_rows($numresultphotos);

echo"<b>Gallery:</b> " .$row{'gallery'}. "<br>";
echo "<b>No. Of Photos:</b> " .$numphotos. "<br>" ;}

But it keeps failing to produce the number of photos in the gallery. But it
will display the table name if i want it to. So it's got the correct name,
so any ideas why it's failing and not telling me how many rows are in the
database.

Thanks

--
Kathryn (Fire Juggler)
http://www.firejugglers.34sp.com
http://www.cornwalljugglers.co.uk
Jul 17 '05 #1
2 1345
On Sat, 9 Jul 2005 16:42:17 +0100, Fire Juggler wrote:
{while ($row = mysql_fetch_array($numresultsgal)) {
What's that first { doing there?
$table=$row{'table_gal'};
Replace { and } with [ and ].
$numresultphotos=mysql_query("SELECT * FROM" .$table. "order by date desc");
The table name gets plastered against 'FROM' and 'order', the query will
not be valid. Also, if you only need the number of rows, don't get the
complete result set, but use an aggregate function (COUNT) to let de
database do the counting for you.
echo"<b>Gallery:</b> " .$row{'gallery'}. "<br>";


Replace { and } with [ and ].

--
Firefox Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #2
Fire Juggler wrote:
Hi I have this this code
{while ($row = mysql_fetch_array($numresultsgal)) {
$table=$row{'table_gal'};
$numresultphotos=mysql_query("SELECT * FROM" .$table. "order by date
desc"); $numphotos=mysql_num_rows($numresultphotos);

echo"<b>Gallery:</b> " .$row{'gallery'}. "<br>";
echo "<b>No. Of Photos:</b> " .$numphotos. "<br>" ;}

But it keeps failing to produce the number of photos in the gallery.
But it will display the table name if i want it to. So it's got the
correct name, so any ideas why it's failing and not telling me how
many rows are in the database.


Is that the EXACT code? If so, you have an error in your SQL syntax. That
error is resulting in a false value for $numresultphotos, which, in turn,
gives a false value for $numphotos.

The error that I see is in the lack of spaces: "SELECT * FROM" . $table .
"order by date desc" - if $table = 'MyTable', your query would be "SELECT *
FROMMyTableorder by date desc" - add spaces: ".. FROM " . $table . " order
by..."

If you also add some error reporting, you'll probably find errors faster -
and, BTW, you don't have to concatenate variables when using double-quotes:

$numresultphotos = mysql_query("SELECT * FROM $table order by date desc");
if (!$numresultphotos) { die (mysql_error()); }
$numphotos = mysql_num_rows($numresultphotos);
--
Tony Garcia
Web Right! Development
Riverside, CA
www.WebRightDevelopment.com
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5104
by: Oleg Leschov | last post by:
Could there be means of exiting nested loops in python? something similar to labelled loops in perl.. I consider it irrating to have to make a flag for sole purpose of checking it after loop if we need to break once more... So maybe there should be an argument to break that is an int which is a number of loops to break. So it would...
15
6557
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
4
2119
by: Dr. David Kirkby | last post by:
I have a program that loops through and changes all the elements on an array n times, so my code looks like this: for (n=1; n < n_max; ++n) for(i=imax; i >= 0; --i) { for(j=0 ; j < jmax; ++j) { /* main bulk of code goes here */ } }
46
9872
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are already in a loop." Then he goes on to portray a contrived example that doesn't tell me under what conditions a nested loop might be favoured as a...
6
1700
by: Scott Brady Drummonds | last post by:
Hi, everyone, I was in a code review a couple of days ago and noticed one of my coworkers never used for() loops. Instead, he would use while() loops such as the following: i = 0; while (i < n) { ...
17
3031
by: John Salerno | last post by:
I'm reading Text Processing in Python right now and I came across a comment that is helping me to see for loops in a new light. I think because I'm used to the C-style for loop where you create a counter within the loop declaration, for loops have always seemed to me to be about doing something a certain number of times, and not about...
10
3954
by: Putty | last post by:
In C and C++ and Java, the 'for' statement is a shortcut to make very concise loops. In python, 'for' iterates over elements in a sequence. Is there a way to do this in python that's more concise than 'while'? C: for(i=0; i<length; i++) python: while i < length:
2
2285
by: bitong | last post by:
I'm a little bit confuse with regard to our subject in C..We are now with the Loops..and I was just wondering if given a problem, can you use Do-while loops instead of a for loops or vise versa? are there instances that you must use a Do-while loops instead of a for loops or a while loop? or you can use any types of loops in any given problem?
3
2396
by: monomaniac21 | last post by:
hi all i have a script that retrieves rows from a single table, rows are related to eachother and are retrieved by doing a series of while loops within while loops. bcos each row contains a text field they are fairly large. the net result is that when 60 or so results are reitreved the page size is 400kb! which takes too long to load. is...
8
7235
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not <Boolean ExpressionThen Exit For Next If Not <Boolean ExpressionThen Exit For
0
7273
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7405
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7547
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3252
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.