Page 2 of 2

Re: Determine if MySQL query is empty without mysql_fetch_array?

Posted: Sun Feb 22, 2009 8:53 pm
by JAB Creations
5K rows would be insane...plus once I finish the basic things I'm working on there will be multiple-page listings...this is just for a section index. Ultimately I don't think I'd load more then a hundred pages...or will even get to the point to consider it. I also didn't like the original method I was using as if I did have 5K rows then I'd be doing an if statement 5K times! :lol:

Re: Determine if MySQL query is empty without mysql_fetch_array?

Posted: Sun Feb 22, 2009 9:26 pm
by josh
pytrin wrote:what is the difference?
Side effect free functions. The whole point of encapsulation is to make less verbose code, if you want to find whether or not there are rows, you should not have to allocate memory to loading the rowsets or write the additional code. Using mysql_num_rows along with arrays for iteration are not mutually exclusive. If you're writing procedural code you probably can't tell the difference, but if you were to display, say for example, a different view script depending on if there are rows, your iteration logic should only be placed in the view script where the rows are actually being used. Duplicating code to determine which view script to use would be wrong. Solution, create a side effect free query method on the model to ask it if it has rows. Not using objects? No problem, PHP developers already created you a imperative solution, I bet you can already guess what that function is :wink:

Re: Determine if MySQL query is empty without mysql_fetch_array?

Posted: Sun Feb 22, 2009 9:43 pm
by Eran
Again, you are missing the point. You can take the actual process of fetching the rows and putting it into an array and encapsulate in a function or a class or whatever, since now the actual logic depends on an array that could come from any source / process and does not require the actual database resource. If you were using a view script you would most certainly not have a mysql_num_rows call inside it, but a check to see if the passed parameters are empty or not. How do you consider populating an array to be "logic"? I just can't come to grips with that. Do you use a while loop on a mysql resource in your view scripts?

And where did you see code duplication? I must have missed it.

Re: Determine if MySQL query is empty without mysql_fetch_array?

Posted: Sun Feb 22, 2009 11:46 pm
by Bill H
I think he said he wants to display them. He didn't say anything about adding them or otherwise manipulating them. Again, there might be some circumstances where putting the resultset into an array would be worthwhile, but simply for the purpose of display or to see if it is empty certainly does not meet that standard. So the following,

Code: Select all

 
$Query = "SELECT * FROM blah WHERE blah ORDER BY Name";
$Result = mysql_query($Query, $Link);
while ($row = mysql_fetch_array($Result))
     echo $row['id'],">&nbsp;",$row['Name'],"<br>\n";
 
is obviously more efficient and more compact than the following to achieve the same purpose.

Code: Select all

 
$Query = "SELECT * FROM blah WHERE blah ORDER BY Name";
$Result = mysql_query($Query, $Link);
$fib = array();
while ($row = mysql_fetch_array($Result))
     $fib[] = $row;
$cnt= count($fib);
for $i = 0; $ i < $cnt; $i++)
     echo $fib[$i]['id'],">&nbsp;",$fib[$i]['Name'],"<br>\n";
 
And if you want to check for zero data

Code: Select all

 
$Query = "SELECT * FROM blah WHERE blah ORDER BY Name";
$Result = mysql_query($Query, $Link);
if (mysql_num_rows($Result))
{     while ($row = mysql_fetch_array($Result))
          echo $row['id'],">&nbsp;",$row['Name'],"<br>\n";
}
else echo "Sorry, bub, no data";
 
This whole array thing, based on his statement that he simply wants to display the data, is just nonsense. And how is that first segment not maintainable?

Re: Determine if MySQL query is empty without mysql_fetch_array?

Posted: Mon Feb 23, 2009 7:11 am
by Eran
Yes, if you give a one-liner as an example there shouldn't be any maintainability problems...
View logic can get much more complex than that. When that happens, array data is much easier to handle than trying to do it inside a while loop. If you can't see that, I don't have much more to say. I think the OP got my intention and that what matters to me.

As an aside, you are missing opening/closing braces on some of your control structures. This could lead easily to hard to track logical bugs.

Re: Determine if MySQL query is empty without mysql_fetch_array?

Posted: Mon Feb 23, 2009 7:38 am
by johnworf
josh wrote:
pytrin wrote:He wants to do something with the array if it's not empty, it's not there just to check if rows were returned or not. This is common usage and quite useful since array access and functions is much robust than the while loop
The array is only empty if 0 rows get returned. I use mysql_num_rows

@johnworf way to hijack the man's thread.

Hi,

Sorry about that - i've started a new thread - i put my code there as i thought it was a similar issue and might be helpful - i'm sorry if i have offended.

john

Re: Determine if MySQL query is empty without mysql_fetch_array?

Posted: Mon Feb 23, 2009 9:00 am
by josh
pytrin wrote:Again, you are missing the point.
I find it humorous you know more about our understanding then we do! Besides did you read the title of the topic?

Re: Determine if MySQL query is empty without mysql_fetch_array?

Posted: Mon Feb 23, 2009 9:19 am
by Eran
I find it great that I can amuse you. Did you read the actual original post or just looked at the title?

Re: Determine if MySQL query is empty without mysql_fetch_array?

Posted: Mon Feb 23, 2009 9:24 am
by JAB Creations
Are you guys going to get a divorce? :(

Seriously though, there's more then one way to skin a cat...so to speak. The original issue I started the thread for has been answered to the point where things work...

I am not on the level to debate what you guys are debating...or to know if you're debating the same thing...whatever. I think if you guys continue you should both at least post some code. :P

Re: Determine if MySQL query is empty without mysql_fetch_ar

Posted: Mon Feb 23, 2009 9:34 am
by JAB Creations
...or since I to some extent started this I'll share what I've been doing of late...

Here is the index.php file that will eventually handle all the pages on my site. It's still early in development and there is a link to see the output to follow...

Code: Select all

<?php
// First templates here
echo '<div class="border">'."\n";
 
$row1 = array();
while($row = mysql_fetch_array($cms->index))
{
 $row1[] = $row;
}
 
if (!empty($row1))
{
 echo '<div class="margin">'."\n";
 $i = '0';
 while ($i < count($row1))
 {
  echo '<div><a href="'.$cms->section.'/'.$row1[$i]['url'].'" tabindex="3">'.$row1[$i]['meta_description'].'</a><span class="color4"> - </span><span>'.$row1[$i]['meta_description'].'</span></div>'."\n";
 $i++;
 }
 echo '</div>'."\n";
}
else
{
 if ($cms->meta['http'] == '200') {echo '<p>The section exists, however it is currently empty.</p>'."\n";}
 else if ($cms->meta['http'] == '404') {echo '<p>File not found...</p>'."\n";}
}
echo '</div><!-- /.border -->'."\n\n";
 
 
echo '<h2>Debugging Information</h2>'."\n";
echo '<div class="border">'."\n";
//if ($_SESSION['member_user_status'] == '9') {
 include_once("dBug.php");
 //new dBug($result1);
 //new dBug($shop);
 new dBug($cms);
 //new dBug($settings);
//}
echo "\n".'</div><!-- /.border -->'."\n\n";
// Last templates here
?>
You guys can see a very late build here (with some examples and the debugger you guys recommended earlier in another thread)...
http://www.jabcreations.com/

...keep in mind I'm in the middle of working on it. Right now index and empty section pages work to some extent though the index page that exists comes up as 404. I'm not interested in asking any further questions in this thread...just trying to give you guys a visual. I'm actually considering posting a critique review of the CMS though you can see later on. I keep putting off messing with Zend but I also keep talking too much with girls of late. :mrgreen: