Jump to content

Archived

This topic is now archived and is closed to further replies.

complex05

mysql_fetch_array, extracting variables

Recommended Posts

hello,

I've seen a function before that does this but forgot what it was. Right now whenever I use $data = mysql_fetch_array($query) i do the following after:

$name = $data["name"];
$email = $data["email"];
$phone = $data["phone"];

etc etc etc... isn't there a function that will do all of this for me?

Share this post


Link to post
Share on other sites
Are you talking about storing values into an array?  From what it looks like, you're taking the values from an array and storing them inside of a variable.  Are you trying to put them back in an array?

Share this post


Link to post
Share on other sites
If you use the function mysql_fetch_assoc() instead, you can then use the [url=http://www.php.net/]extract()[/url] function to do that.  Why don't you just use the array references, which makes the code much more self-commenting.

Ken

Share this post


Link to post
Share on other sites
or --> list() --> http://no.php.net/manual/fi/function.list.php

or another old fashion way
[code]
<?php

$col = mysql_fetch_array($select, MYSQL_ASSOC);
foreach($col as $fieldname => $value)
{
${$fieldname} = $value;
}

?>
[/code]

Share this post


Link to post
Share on other sites
[quote author=kenrbnsn link=topic=111674.msg452711#msg452711 date=1161020142]
If you use the function mysql_fetch_assoc() instead, you can then use the [url=http://www.php.net/]extract()[/url] function to do that.  Why don't you just use the array references, which makes the code much more self-commenting.

Ken
[/quote]

I've always wondered how people came to use mysql_fetch_array() instead of mysql_fetch_assoc(), seems like everyone does it..

Share this post


Link to post
Share on other sites
The mysql_fetch_array() function came first. The myslq_fetch_assoc() function didn't come out until after version 4.0.3

Ken

Share this post


Link to post
Share on other sites
i use fetch_array, simply because it's all encompassing. I mean, fetch_assoc returns an associative array.  fetch_row returns a numerical array.  but fetch_array returns both by default.  Or i can specify one or the other if i [i]really[/i] want to, with an optional argument.  But i usually don't.  I can simply use fetch_array and just use the numerical or associative version as i see fit.  I feel it is less hassle.  Maybe that's just me. 

Share this post


Link to post
Share on other sites
yeah, but that doubles the amount of ram being used by the array..

i can't imagine that'd be a good rule to go by if you're using any sizable amount of data..  text.. blobs.. ?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.