Page 1 of 1
two dimensional array
Posted: Wed Sep 20, 2006 7:12 am
by speedy33417
Is it possible to fetch data into a two dimensional array where one of the dimesion is the name of the table column?
I'm trying to retreive my data from a photo album db. Let's say I have the following:
picture_id
picture_filename
picture_description
With 10 entries.
I'd like to get a
$result[picture_id][0-9]
$result[picture_filename][0-9]
$result[picture_description][0-9]
This is my first database driven site and I'm a little overwhelmed.

Would someone provide me with a sample code for doing this?
Thanks.
Posted: Wed Sep 20, 2006 9:07 am
by kaszu
Code: Select all
$result = Array();
while($row = mysql_fetch_assoc($mysql_result))
{
$result['picture_id'][] = $row['picture_id'];
$result['picture_filename'][] = $row['picture_filename'];
$result['picture_description'][] = $row['picture_description'];
}
Posted: Wed Sep 20, 2006 12:30 pm
by speedy33417
Thanks for your help. This is what I have so far:
Code: Select all
<?php
include("inc/connectdb.inc");
$result = Array()
$sql = "SELECT picture_pathid, picture_filename, picture_description
FROM pictures
WHERE picture_albumid = 1
ORDER BY picture_number";
while($row = mysql_fetch_assoc($sql))
{
$result['picture_pathid'][] = $row['picture_pathid'];
$result['picture_filename'][] = $row['picture_filename'];
$result['picture_description'][] = $row['picture_description'];
}
$numpicsleft = 29;
$counter = 1;
$numrows = ceil($numpicsleft / 4);
?>
I'm getting this error message right now: Parse error: parse error, unexpected T_VARIABLE in /var/www/html/some.php on line 6
Line 6 is the SELECT statement, which means it never even got to the part that I wanted try out.

I'm trying to select all pictures that is labeled to belong to album 1, orderd by picture_number that is an integer 1, 2, 3...
Thanks.
Posted: Wed Sep 20, 2006 12:34 pm
by n00b Saibot
this
should be this
and this
Code: Select all
$sql = "SELECT picture_pathid, picture_filename, picture_description
FROM pictures
WHERE picture_albumid = 1"
ORDER BY picture_number;
should be this
Code: Select all
$sql = "SELECT picture_pathid, picture_filename, picture_description
FROM pictures
WHERE picture_albumid = 1
ORDER BY picture_number";
you moved double-quotes from line 8 to 7
edit | it looks like you corrected the second bug
Posted: Wed Sep 20, 2006 12:43 pm
by speedy33417
Yes, I quickly fixed the second one. Thanks to your post I fixed the first one, too.
Now the page runs, but I still get an error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/html/some.php on line 11
Code: Select all
<?php
include("inc/dbconnect.inc");
$result = Array();
$sql = "SELECT picture_pathid, picture_filename, picture_description
FROM pictures
WHERE picture_albumid = 1
ORDER BY picture_number";
while($row = mysql_fetch_assoc($sql))
{
$result['picture_id'][] = $row['picture_pathid'];
$result['picture_filename'][] = $row['picture_filename'];
$result['picture_description'][] = $row['picture_description'];
}
$numpicsleft = 29;
$counter = 1;
$numrows = ceil($numpicsleft / 4);
?>
Posted: Wed Sep 20, 2006 12:47 pm
by n00b Saibot
you forgot to query (
mysql_query()) the database... you in hurry for results?

Posted: Wed Sep 20, 2006 12:54 pm
by speedy33417
Argh, this is all very new to me. Last time I programmed something that could be called a database was on Commodore 64. Like AGES ago! That's why I can deal more easily with arrays, even if they're multidimensional. Here, once it makes sense to you I'm sure you can achieve a lot more with it, but I wish I could just pass those variables to my multidimensional array right away with my SELECT statement.
I just don't get the whole process of this.
Anyway, whining is over. What is a querying?

Posted: Wed Sep 20, 2006 1:01 pm
by speedy33417
Changed my code to this:
Code: Select all
<?php
include("inc/dbconnect.inc");
$result = Array();
$sql = "SELECT picture_pathid, picture_filename, picture_description
FROM pictures
WHERE picture_albumid = 1
ORDER BY picture_number";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
$result['picture_id'][] = $row['picture_pathid'];
$result['picture_filename'][] = $row['picture_filename'];
$result['picture_description'][] = $row['picture_description'];
}
$numpicsleft = 29;
$counter = 1;
$numrows = ceil($numpicsleft / 4);
?>
Now I get this error:
Warning: Cannot use a scalar value as an array in /var/www/html/some.php on line 15
Warning: Cannot use a scalar value as an array in /var/www/html/some.php on line 16
Warning: Cannot use a scalar value as an array in /var/www/html/some.php on line 17
And I get this like 29 times

Posted: Wed Sep 20, 2006 1:03 pm
by n00b Saibot
change to
Code: Select all
$results = new array();
while($row = mysql_fetch_assoc($result))
{
$results['picture_id'][] = $row['picture_pathid'];
$results['picture_filename'][] = $row['picture_filename'];
$results['picture_description'][] = $row['picture_description'];
}
you are using same variable ($result) concurrently at two different locations and in two different ways! one as a resource and another as array...
Posted: Wed Sep 20, 2006 1:12 pm
by volka
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
$result['picture_id'][] = $row['picture_pathid'];
You (re-)assigning an array element to your mysql result resource. That doesn't work in two ways.
a) php is raising the warnings you're seeing
b) $result wouldn't be a mysql resource anymore you can use with mysql_fetch_array.
Code: Select all
<?php
include("inc/dbconnect.inc");
$result = Array();
$sql = "SELECT
picture_pathid, picture_filename, picture_description
FROM
pictures
WHERE
picture_albumid = 1
ORDER BY
picture_number";
$result = mysql_query($sql) or die($sql);
$pictures = array(
'picture_id'=>array(),
'picture_filename'=>array(),
'picture_description'=>array()
);
while( $row=mysql_fetch_assoc($result) )
{
$pictures['picture_id'][] = $row['picture_pathid'];
$pictures['picture_filename'][] = $row['picture_filename'];
$pictures['picture_description'][] = $row['picture_description'];
}
$numpicsleft = 29;
$counter = 1;
$numrows = ceil($numpicsleft / 4);
?>
Posted: Wed Sep 20, 2006 1:16 pm
by RobertGonzalez
Your array set up is whacky. Try something like this:
Code: Select all
<?php
$myarray = array();
while ($row = mysql_fetch_array($result))
{
$myarray[] = $row;
}
?>
Posted: Wed Sep 20, 2006 1:20 pm
by speedy33417
Guys, I think I got it!!! Well, I'm not getting any error messages anymore, at least. I haven't echoed anything, but I assume all the info is there.
Here's the code right now:
Code: Select all
<?php
include("inc/dbconnect.inc");
$result = Array();
$sql = "SELECT picture_pathid, picture_filename, picture_description
FROM pictures
WHERE picture_albumid = 1
ORDER BY picture_number";
$results = mysql_query($sql);
while($row = mysql_fetch_assoc($results))
{
$result['picture_id'][] = $row['picture_pathid'];
$result['picture_filename'][] = $row['picture_filename'];
$result['picture_description'][] = $row['picture_description'];
}
$numpicsleft = 29;
$counter = 1;
$numrows = ceil($numpicsleft / 4);
?>
I think I'm starting to get what I'm doing here.
The line where I have the select statement doesn't do anything other than saving the SQL code in a variable. The query (the line I didn't have) is what actually calls that code in SQL. I get it.
Thanks a lot guys. Although, there's a chance I'll be back with more questions...

Posted: Wed Sep 20, 2006 1:44 pm
by RobertGonzalez
Lets see if I can help out with an explanation:
Code: Select all
<?php
// Include a file... probably DB credentials
include("inc/dbconnect.inc");
// Initialize an array var called $result
$result = Array();
// Read a SQL command into a var called $sql
$sql = "SELECT picture_pathid, picture_filename, picture_description
FROM pictures
WHERE picture_albumid = 1
ORDER BY picture_number";
// Execute the query
/*
FOR SECURITY YOU SHOULD DO ERROR CHECKING ON THIS!
*/
$results = mysql_query($sql);
// While reading the query result into an array called $row ...
while($row = mysql_fetch_assoc($results))
{
// Assign a few result array values into multi dimensional arrays
$result['picture_id'][] = $row['picture_pathid'];
$result['picture_filename'][] = $row['picture_filename'];
$result['picture_description'][] = $row['picture_description'];
}
// Assign values to vars and do some math
$numpicsleft = 29;
$counter = 1;
$numrows = ceil($numpicsleft / 4);
?>