[Solved] Trouble getting data, returning "Array"

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

[Solved] Trouble getting data, returning "Array"

Post by Steveo31 »

'Sup guys. Having some problems with this structure. I have a JS function that passes a variable to a pic.php page consisting of the image name, like so:

Code: Select all

<td><a href="#" onclick="openWindow('pic.php?image=seashells')"><img src="images/thumbs/seashells.jpg" alt="" border="0" /></a></td>
This works fine and dandy, the variables are passed and recieved with no probs.

Then I run into a DB problem. Using PhpMyAdmin, I have a mySQL db named "images", with a table called "description" with 2 columns. Then in there, one is a LONGTEXT and the other is a varchar(20). I then add info to it...

Code: Select all

INSERT INTO `description` ( `txtdesc` , `imageID` )
VALUES (
'This is a description. It has useful info in it on the image, so on and so forth.', 'seashells'
);
Is what PhpMyAdmin comes back with. Great... its there. Now, what I need to do is get it into the "pic.php" page. Here's the page:

Code: Select all

<?php

$image=$_GET['image'];
$urlToPic="images/thumbs/".$image.".jpg";

$db=mysql_connect("localhost", "root");
mysql_select_db("images", $db) or die(mysql_error());
$sql="SELECT txtdesc, imageID FROM description WHERE imageID='".$image."'";
$result=mysql_query($sql, $db) or die(mysql_error());

while($row=mysql_fetch_assoc($result)){
	echo "$row";
}
?>
<table>
	<tr>
		<td><img src="<?php echo $urlToPic; ?>" alt="" /></td>
		<td valign="top">Blurb:
		<p>&nbsp;</p></td>
	</tr>
</table>
(The style is simple, I need to get it to work before I start implementing CSS and whatnot to make it look good)

With this code, mySQL is returning "Array" as the $row value. Not what I'm after.. :( ;)

Do you see anything or know of a function that I don't where I can return the values I have entered? I thought it was an easy task, but I guess I'm doing something wrong... I have a feeling it is in the set up of the DB.

Thanks.

-S
Last edited by Steveo31 on Fri Mar 19, 2004 9:18 pm, edited 1 time in total.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Ah there she is. Got it.. changed

Code: Select all

while($row=mysql_fetch_assoc($result)){
   echo "$row";
}
to...

Code: Select all

while($row=mysql_fetch_assoc($result)){
   echo $row['txtdesc'];
}
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Steveo31 wrote:

Code: Select all

while($row=mysql_fetch_assoc($result)){
   echo "$row";
}
Indeed that is a wrong method, I'll explain why.

The function [php_man]mysql_fetch_assoc[/php_man]() is extracting an array from a database query. So by defining the variable $row, you are seeding that variable with an array. And to echo an array you need to use [php_man]print_r[/php_man](). But in this situation, you should not print_r() within a loop. So using an array key is best, numbers or letters. :)
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

just to tamper with samis post.

mysql_fetch_assoc will only retain text information, not numbers.

mysql_fetch_array is what you should use instead b/c it not only does associative arrays (text) but also numbers.

:D
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

tim wrote:mysql_fetch_assoc will only retain text information, not numbers.
Indeed.
PHP Manual wrote:mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array. This is the way mysql_fetch_array() originally worked. If you need the numeric indices as well as the associative, use mysql_fetch_array().
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

:)
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Thanks for the explanations on the different functions. After I posted this I went through and realized I screwed up and fixed it.

:)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

tim wrote:mysql_fetch_array is what you should use instead b/c it not only does associative arrays (text) but also numbers.

:D
Just making note; this is an personal opinion. I find it (often) much more readable using the $row['fieldname'] aproach rather than $row['29'].

But, a sidenote is that you can also use the mysql_fetch_array() with the optional argument stated. Either MYSQL_ASSOC (as mysql_fetch_assoc), MYSQL_NUM (as mysql_fetch_row) or MYSQL_BOTH (default).
Post Reply