Implode Function

A

Anonymous

Guest
The following code places a series of strings all in one cell. I want to change it so that I use the implode command to place all of the names in the same cell separated by commas.

Code:
$result1 = mysql_query($query1);
           echo "<td>";
           while ($row1 = mysql_fetch_array($result1))
           {
           extract($row1);
           echo "$Name ";
           }
           echo "</td></tr>";

My new code looks like follows, but I get an error in the cell. I know I must have some syntax wrong but I have checked all over the net but cannot discover the problem.

Code:
$result1 = mysql_query($query1);
          echo "<td>";
          echo implode(", ",$result1);
          echo "</td></tr>";
[/code]
 
If you're getting an error, it would be extraordinarily useful for you to actually tell us what error you're getting.

Anyway, I can tell you almost without looking what the trouble is. The implode() function takes an array as an argument. But you're giving it $result1, which is the value returned by mysql_query(). The trouble is, mysql_query() doesn't return an array, $result1 isn't an array, and you can't pass that to implode().

In your first chunk of code, you used mysql_fetch_array(), which does return an array. That would be a good place to start, though I prefer mysql_fetch_assoc().

By the way, don't use extract(). It's a poor practice.
 
Sorry, I am just learning how to do this stuff. I really appreciate your help. Here is the error that I am getting.

Warning: implode(): Bad arguments. in /home2/chengrob/public_html/php/photo2.php on line 48

I think that makes sense you state that $result is not an array. A dumb question: If it is not an array, what is it? I tried using mysql_fetch_array and mysql_fetch_assoc, but the problem is that it only returns one row. I really need to change $result1 into an array somehow. Is there a straight forward way of doing that?

Thanks again.
 
As described in the documentation, mysql_query() returns a "resource identifier", which is a link to your result set. Also as described in the documentation, mysql_fetch_assoc() and the like return the next result from the result set in the form of an array. So if you have ten results, you have to call it ten times. The solution? A loop. use a while loop to keep returning the next result until there are no more left.

It sounds complicated, and I wouldn't expect you to know it, except that it's all explained in the documentation. Read the documentation, read the examples, and read the comments at the bottom of each page.
 
Thanks for all your help!

I have everything working great with your advice. I am working on some other things now, but you made an earlier comment that is haunting me.

You said "Don't use extract", it is a poor practice. What should I be using instead? Why is it a poor practice?

Thanks.
 
chengrob said:
You said "Don't use extract", it is a poor practice. What should I be using instead? Why is it a poor practice?

Okay, this was a broad generalization on my part, but I think it holds true more often than not. The reason I say it is because it makes your code easier to comprehend. Think of it this way: Say you query the database and get an associative array of the values using mysql_fetch_assoc(). This is usually called something like $row (though using more verbose names is a good habit, especially if you have more than one query on your page). So in the rest of your script you refer to values like $row['name'], $row['email'], $row['birthday'], etc. Every time you see one of these variables in your script you immediately know that it got its value from the database query, because it starts with $row.

Now say you use extract instead. Suddenly, like magic, you have $name, $email, $birthday, and so on. From then on, there's no way to tell at a glance that these are the values from your query. The only way to know this is by reading your code, checking the row names in the query, and lord help you if the query is in a different file, or you used SELECT * FROM ... (another practice you should avoid).

This is the same reason I get a nervous twich when I see newbies doing this:

Code:
<?php
$name = $_GET['name'];
$email = $_GET['email'];
$birthday = $_GET['birthday'];
// ... and so on
?>

Because by taking the variables out of their context, you're making the code more difficult to read (if not right now, then definitely after you haven't looked at it for a few months). Not to mention it's a big waste of memory and processor cycles (each of those values that you duplicate takes up memory).
 
Back
Top