Author Topic: mysql_fetch_array() a way to dry out some of the repeat text  (Read 610 times)

Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Ok since mysql_fetch_array($result,type) is being used for my while is it possible to do something like...
Code: [Select]
$result = mysql($query)
$row = mysql_fetch_array($result,MYSQL_BOTH);
while($row=true){
//do stuff here
}
instead of actually putting in the actual
while($row = mysql_fetch_array($result,MYSQL_BOTH)){
//do stuff here
}
and then just reuse that $row variable around the rest of the script instead of having to repeat it as i'm going to be pulling a few tables of data from the database andthen well using them.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: mysql_fetch_array() a way to dry out some of the repeat text
« Reply #1 on: May 10, 2010, 03:22:42 AM »
Query gets data from DB (2D array).
Fetch returns the desired row (1D array).
Therefore, there is no need fo the thing you proposed, fetch does not connect to database but gets the data from a buffer/memory/whatever and can be considered as fast as it can be.
After querry the $result data is stored until unset($result) (or auto cleaned at thde end of the script).

When you optimize SQL you want to reduce number of querries, not fetches.

If you are not doing for purpose of optimization but for some other crazy reasons take a look at data seek, it allows you to manipulate the pointer (so with this you should be able to treat it similar to PHP 1D array).
http://www.php.net/manual/en/function.mysql-data-seek.php
Note: if you need that function for not specialized SQL tools, then you might be doing something wrong. Or not :)

Offline Marek

  • Level 18
  • *
  • Posts: 177
  • Reputation: +7/-0
  • XHTML, CSS, JS, PHP and MySQL are my pantheon.
    • View Profile
Re: mysql_fetch_array() a way to dry out some of the repeat text
« Reply #2 on: May 10, 2010, 08:22:27 AM »
As Chris said, it won't work. The reason is that when you use the following:
Code: [Select]
while($row = mysql_fetch_array($result,MYSQL_BOTH))Two things happen on every iteration: the mysql_fetch_array gets called and returns one row from the database. (That's the purpose of the function, it only returns one row at once). Then the value of the row gets immediately assigned to $row. Note that the assignment operator (=) is used, not the equality operator (==). That's a crucial difference. Then, the while loop checks the value of the expression as a whole. As long as there are still rows remaining, the result of mysel_fetch_array (and, consequently, of the value of $row, and of the value of the whole assignment expression) evaluates to true. That's what the while loop condition checks. When mysql_fetch_array returns false, it signals the end of the data set and thus the while loop ends.


Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Re: mysql_fetch_array() a way to dry out some of the repeat text
« Reply #3 on: May 11, 2010, 12:57:31 AM »
Query gets data from DB (2D array).
Fetch returns the desired row (1D array).
Therefore, there is no need fo the thing you proposed, fetch does not connect to database but gets the data from a buffer/memory/whatever and can be considered as fast as it can be.
After querry the $result data is stored until unset($result) (or auto cleaned at thde end of the script).

When you optimize SQL you want to reduce number of querries, not fetches.

If you are not doing for purpose of optimization but for some other crazy reasons take a look at data seek, it allows you to manipulate the pointer (so with this you should be able to treat it similar to PHP 1D array).
http://www.php.net/manual/en/function.mysql-data-seek.php
Note: if you need that function for not specialized SQL tools, then you might be doing something wrong. Or not :)

I was using it for a "get alot of data thing" only to be used at the developer level/on the developer sever.  So i was just trying to well speed it up a bit since i don't like slow queries and there's probably not an easy way to reduce it to down to any less queries than i'm currently doing which is just well basically pulling all fo the data out of the databases for whatever part is bieng used, then putting it on the page nicely and neatly. It's basically for a "lets' see what all is here" kind of thing wherein the data from the table(s) is(are) pulled out and put on the page so that i can visualize it easier and then maybe do a quick edit on something or tweak an item if need be without having to go into phpmyadmin and leave the testing site/server. And ok then i'll just leave it be then for now. :P

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal