2

I've written the code below:

$release_id = intval(filter_var($_GET["rid"],FILTER_SANITIZE_STRING));
$query = "select * from press_releases where id = {$release_id}";

$result = $db->query($query);
$row = $result->fetch_assoc();

list($id, $short_description, $description, $created_date) = $row;

$db->close();

and I am using the variables such as $description, $short_description inside of the html tags but nothing shows. If I use the code below which is same except for the list() function:

$release_id = intval(filter_var($_GET["rid"],FILTER_SANITIZE_STRING));
$query = "select * from press_releases where id = {$release_id}";

$result = $db->query($query);
$row = $result->fetch_assoc();

$id = $row["id"];
$short_description = $row["short_description"];
$description = stripslashes(html_entity_decode($row["description"]));
$created_date = $row["created_date"];

$db->close();

it works perfectly. Basically list() function cannot assign the values coming from the $row array.

I don't understand why?

9 Answers 9

5

According to the PHP docs for list():

list() only works on numerical arrays and assumes the numerical indices start at 0.

Sign up to request clarification or add additional context in comments.

2 Comments

This makes sense now. It is already said on the documentation but I couldn't see it: php.net/manual/en/function.list.php
@Braveyard: In addition, list() assigns the values in reverse order (see the warning further below).
4

I don't believe the list() function works with associative arrays. Try fetching your query results normally (numerical indices) and see if that resolves the issue for you.

Comments

3

You want to use extract() instead of list().

This is the one that's appropriate for associative results. And you shouldn't use it with SELECT * but a concrete list of SELECT varname1,varname2,varname3 so you don't get unexpected local variables.

It's also possible to use extract(array_intersect_key(..)) to get a filtered list of variables to be extracted, but that's overkill for database result sets that you can control anyway.

1 Comment

I really like this version as well.. Thanks a lot. extract and array_values would be great solutions...
2

A rather easy solution to this is as follows:

list($id, $short_description, $description, $created_date) = array_values($row);

Or simply don't fetch as assoc, but as a normal array.

I must say I don't really recommend doing this. Since your SQL query is fetching with *, it will break very easily - Let's say for whatever reason one of the columns gets shuffled around so it appears at some other position in the table. Bam.

Assuming you were to define each column separately in your query, then it would be fine.

1 Comment

In addition, you have to reverse the list of variables because list assignes the values from right to left.
1

My guess is that list() expects a numerically indexed array. Try using $row = $result->fetch_row(); instead

Comments

1

list() expects an indexed array, not an associative one. Try using fetch_row().

Comments

1

Use array_values like this:

list($id, $short_description, $description, $created_date) = array_values($row);

Read the comment by kevin in here on php.net.

Comments

1

list does only work with numerically indexed arrays. Switch fetch_assoc to fetch_row or use array_values on $row before the assignment:

$row = $result->fetch_row();

or

list($id, $short_description, $description, $created_date) = array_values($row);

Comments

1

Use fetch_row rather than fetch_assoc, see the example. AssociativeHash arrays have no well-defined element order!

4 Comments

@Felix: Sorry, I meant "hash containers". Of course ordered associative containers like C++'s std::set do have an order -- but the order is not that of insertion, rather than of key value. I'm not sure about PHP, but I thought that associative arrays were hash-based, like std::unordered_map...
Uhm, I don't know about C++. But in PHP there is actually only one type of array and the elements are ordered (insertion order), no matter whether the keys are numeric or strings.
@Felix: I see -- is this actually guaranteed? (What an expensive data structure that must be...) If it were, then the list construction should really work for those arrays, too.
Yes it is. And yes it should work, but apparently it does not, for whatever reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.