RE: Using two colomns of mysql data as key/value pairs i n arrays

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



OK, I used the code Chris provided in his response below and ran it against
the test table below.

mysql> select * from userinfo;
+---------------+---------------+----------------+---------------+----------
----+
| Cust_username | Cust_password | Cust_firstname | Cust_lastname |
Cust_comment |
+---------------+---------------+----------------+---------------+----------
----+
| myusername    | mypassword    | rich           | hutchins      | NULL
|
| thisuser      | thispass      | albert         | einstein      | NULL
|
| ausername     | apassword     | Fozzy          | Bear          | NULL
|
| nother        | apassword     | Kermit         | The Frog      | NULL
|
+---------------+---------------+----------------+---------------+----------
----+

The results of running the code against this table are shown below:
Array
(
    [0] => myusername
    [Cust_username] => myusername
    [1] => mypassword
    [Cust_password] => mypassword
    [2] => rich
    [Cust_firstname] => rich
    [3] => hutchins
    [Cust_lastname] => hutchins
    [4] => 
    [Cust_comment] => 
)


Array
(
    [Cust_username] => myusername
    [Cust_password] => mypassword
    [Cust_firstname] => rich
    [Cust_lastname] => hutchins
    [Cust_comment] => 
)

Since I almost always use column names to reference the results from my
queries and not the index number in the array, it would seem to me that
using mysql_fetch_assoc() certainly returns what would be a more succinct
result set. Chris, you've made a believer out of me; I'll start using
mysql_fetch_assoc() for these kinds of queries from now on.

And in the spirit of only retrieving what you need from the database, I
would agree that mysql_fetch_assoc() is the "better" option. However, other
than maybe making my script run a little more slowly, I fail to see how
mysql_fetch_array() is a bad thing. From what I can see, all it does is give
me an additional numerically indexed reference to the data in my result set
and if I choose to ignore it, then that's my choice. If it causes a
significant slowdown in the performance of my script, then it's certainly a
candidate for optimization.

Am I missing something else?

> -----Original Message-----
> From: Chris Boget [mailto:chris@wild.net]
> Sent: Monday, October 20, 2003 9:56 AM
> To: Hutchins, Richard; php-db@lists.php.net
> Subject: Re:  Using two colomns of mysql data as 
> key/value pairs
> in arrays
> 
> 
> > Curious as to why you say you avoid mysql_fetch_array() 
> like the plague. I
> > use it frequently and am wondering if there is something 
> wrong/not secure
> > when using it or if there is a huge amount of overhead 
> associated with its
> > use.
> 
> It's not that it's not secure, just that it's bad to use any 
> time you aren't using
> specific key references.
> 
> Do the following to see the difference and you'll see why 
> you'll almost always
> want to use *_assoc() and not *_array():
> 
> <?
> /**
>  * Edit to suit your DB needs
>  **/
> $query = "SELECT * FROM blah";
> $result = mysql( $dbname, $query );
> 
> echo '<pre>';
> print_r( mysql_fetch_array( $result ));
> 
> echo "\n\n";
> mysql_data_seek( $result, 0 );
> print_r( mysql_fetch_assoc( $result ));
> 
> echo '</pre>';
> ?>
> 
> Especially if you use the foreach() construct, 
> mysql_fetch_array() is bad 
> news.
> 
> Chris
> 
> 

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux