Jump to content

[SOLVED] retrieving info from database


php_beginner_83

Recommended Posts

Hi Everyone

 

I'm trying to get info from a table, 'pictures', in my database.  The fields in my table that I want to retrieve are 'Description', 'Path', 'Thumbnail'.  However, the only info that is being saved in the array is 'Path'.  I don't understand why this is happening and have been trying to get it to work.  Can anyone see what the problem is??

 

Thanks

 

$result = mysql_query("SELECT * FROM pictures");

//arrays to hold the titles, descriptions and paths separately
$descriptions = array();
$paths = array();
$thumbnails = array();


$counter = 0;

//fetch tha data from the database
while ($row = mysql_fetch_assoc($result)) 
{
$descriptions[$counter] = $row['Description'];
$paths[$counter] = substr(strrchr($row['Path'],92),1);
$thumbnail[$counter] = $row['Thumbnail'];

       $counter++;
}

Link to comment
https://forums.phpfreaks.com/topic/176126-solved-retrieving-info-from-database/
Share on other sites

Hi Everyone

 

I'm trying to get info from a table, 'pictures', in my database.  The fields in my table that I want to retrieve are 'Description', 'Path', 'Thumbnail'.  However, the only info that is being saved in the array is 'Path'.  I don't understand why this is happening and have been trying to get it to work.  Can anyone see what the problem is??

 

Thanks

 

$result = mysql_query("SELECT * FROM pictures");

//arrays to hold the titles, descriptions and paths separately
$descriptions = array();
$paths = array();
$thumbnails = array();


$counter = 0;

//fetch tha data from the database
while ($row = mysql_fetch_assoc($result)) 
{
$descriptions[$counter] = $row['Description'];
$paths[$counter] = substr(strrchr($row['Path'],92),1);
$thumbnail[$counter] = $row['Thumbnail'];

       $counter++;
}

 

Try a print_r($row) and see what comes up.

I've been echoing the values to the screen to see if the arrays contain anything and only $paths does.  When I do this this is what I get....

 

path: alcatraz1.jpg

description:

thumbnail:

 

Thanks for the suggestion leafer...This was the output I got..

 

Array ( [iD] => 15 [Description] => Welcome to Alcatraz. [Path] => images\alcatraz1.jpg [Thumbnail] => thumbs\alcatraz1.jpg )

 

So I see that the values are there but why isn't my code working  :confused: :confused:

 

 

I've been echoing the values to the screen to see if the arrays contain anything and only $paths does.  When I do this this is what I get....

 

path: alcatraz1.jpg

description:

thumbnail:

 

Thanks for the suggestion leafer...This was the output I got..

 

Array ( [iD] => 15 [Description] => Welcome to Alcatraz. [Path] => images\alcatraz1.jpg [Thumbnail] => thumbs\alcatraz1.jpg )

 

So I see that the values are there but why isn't my code working  :confused: :confused:

 

The values names look correct.

 

Try feeding the values into a variable like this:

 

$descriptions[] = $row['Description'];
   $paths[] = substr(strrchr($row['Path'],92),1);
   $thumbnail[] = $row['Thumbnail'];

 

Remove the counter altogether.

try

<?

$result = mysql_query("SELECT Description, Path, Thumbnail FROM pictures");

//arrays to hold the titles, descriptions and paths separately
$desc = array();
$paths = array();
$thumbs = array();

$cnt = 0;
//fetch tha data from the database
while ($row = mysql_fetch_array($result , MYSQL_NUM ))
{
   $desc[$cnt] = $row[0];
   $paths[$cnt] = substr(strrchr($row[1],92),1);
   $thumbs[$cnt] = $row[2];
   $cnt++;
}
   
?>

Thanks for the help.

 

mvfreelance...your suggestion worked a treat  :D

 

Though now I've tried to use a new sql statement..

 

$result = mysql_query("SELECT ID, Description, Path, Thumbnail FROM pictures 
INNER JOIN pics_in_albums 
ON pictures.ID = pics_in_albums.PicID 
INNER JOIN albums 
ON pics_in_albums.AlbumID = albums.ID 
WHERE albums.ID = 1
GROUP BY pictures.ID") or die(mysql_error());

 

and this is what I get, using the same code just this sql is different,.....

 

"Column 'ID' in field list is ambiguous"

 

Do you know what the problem is with this??

Thanks for the help.

 

mvfreelance...your suggestion worked a treat  :D

 

Though now I've tried to use a new sql statement..

 

$result = mysql_query("SELECT ID, Description, Path, Thumbnail FROM pictures 
INNER JOIN pics_in_albums 
ON pictures.ID = pics_in_albums.PicID 
INNER JOIN albums 
ON pics_in_albums.AlbumID = albums.ID 
WHERE albums.ID = 1
GROUP BY pictures.ID") or die(mysql_error());

 

and this is what I get, using the same code just this sql is different,.....

 

"Column 'ID' in field list is ambiguous"

 

Do you know what the problem is with this??

 

IIRC it means the the column name ID exists in both tables.

Archived

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

×
×
  • Create New...

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.