|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
What is more efficient?
Hi all,
Let me try to explain this as best I can. Code:
while($result = @mysql_fetch_array($query)) {
echo($result['value'] . " (" . $result['value'] . ")<br />\n");
}
What I am trying to do is find out if after the loop has ended, if a value was ever assigned to '$result'. This brings me to my next question, which is more efficient? Should I assign the results of the loop to a variable, or should I assign a value to a variable inside the loop therefore stating that the loop has at least run once? For example: Code:
while($result = @mysql_fetch_array($query)) {
$variable .= $result['value'] . " (" . $result['value'] . ")<br />\n";
}
If the variable has any information, then I know that the loop executed at least once. However, is assigning information to a variable like that efficient? Would it be easier and more efficient (and proper) to do this? Code:
$variable = 0;
while($result = @mysql_fetch_array($query)) {
echo($result['value'] . " (" . $result['value'] . ")<br />\n");
$variable++;
}
That way, I could count the rows (which isn't really the most efficient way to do that nor something I need to keep track of) and also the variable would be greater than 0 if the loop executed once. Any input would be greatly appreciated. Best regards, Dustin J. Cox |
|
#2
|
|||
|
|||
|
Dustin,
are you just trying to figure out if there is at least one record available or are you trying to find out if there is a value in one of the records? Your details sort of lost me so I don't know what you really want to accomplish. I am pretty sure that there is a simple plan for you.
__________________
__________________________________________________ _ Wil Moore III, MCP | Integrations Specialist | Senior Consultant Are You Listed...? | DigitallySmooth Inc. |
|
#3
|
|||
|
|||
|
Hi,
I have a database structure like this: Table 1: Animal ID Table 2: Species lookup Table 3: Species When I display the species associated with an animal, I want to know if a species is assigned to an animal. I have to use a while loop because more than one species can be assigned to a single animal (hybrids). So, after the while loop runs, '$result' is always empty. I tried checking like this: Code:
if($result = @mysql_fetch_array($query)) {
// run the while loop
}
That didn't work though... I've been teaching myself PHP, so I haven't been taught proper techniques, etc... Thanks for any help; I hope this clears things up. Best regards, Dustin J. Cox |
|
#4
|
|||
|
|||
|
Dustin,
That is more information than I had before, however, I will probably need to see some more of the code before I can give you a recommendation. Your best bet would be to send me the database layout, and what you expect to get as far as results back from the database when you run your query. BTW, have you run your query through mysql first to make sure this query even returns results? |
|
#5
|
|||
|
|||
|
Quote:
How do I do that? Best regards, Dustin J. Cox |
|
#6
|
|||
|
|||
|
Well, I found another solution:
Code:
if(mysql_num_rows($query)) {
while($result = @mysql_fetch_array($query)) {
echo("<div>" . $result['value'] . " (" . $result['value'] . ")</div>\n");
}
} else {
echo("There are no species linked to this ID.\n");
}
Would this be the best of all three? Best regards, Dustin J. Cox |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > What is more efficient? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|