Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Rookie PHP/MySQL Question

 
   Database Forums (Home) -> PHP RSS
Next:  why i can't login to a server using windows authe..  
Author Message
Ian Rutgers

External


Since: Nov 29, 2005
Posts: 4



(Msg. 1) Posted: Mon Nov 28, 2005 8:55 pm
Post subject: Rookie PHP/MySQL Question
Archived from groups: alt>php (more info?)

In querying a database

($result=query_db("SELECT photo_dir, photo_name FROM photograph_photo WHERE
photo_dir = '$dirToCheck'");

is $result an array (where I could used a "in_array() function)?

Thanks,

Ian

 >> Stay informed about: Rookie PHP/MySQL Question 
Back to top
Login to vote
Scott Johnson

External


Since: Nov 14, 2005
Posts: 2



(Msg. 2) Posted: Tue Nov 29, 2005 6:20 am
Post subject: Re: Rookie PHP/MySQL Question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Ian Rutgers wrote:
> In querying a database
>
> ($result=query_db("SELECT photo_dir, photo_name FROM photograph_photo WHERE
> photo_dir = '$dirToCheck'");
>
> is $result an array (where I could used a "in_array() function)?
>
> Thanks,
>
> Ian
>
>
No the result is basicly a pointer to an external resource.

You need to use an additional function to put that external resource
into a usable value.

The best way I find is to use (depending on the DB your using, there are
different usages for each DB supported) is mysql_fetch_array($result)

But with this you will need to step through the $result if more then one
row is found.

while($row = mysql_fetch_array($result)){
foreach($row as $key => $vlaue){
echo "Key: ".$key." (Value: ".$value.")";
}
}

Hope this helps. If not post back.
Scott

 >> Stay informed about: Rookie PHP/MySQL Question 
Back to top
Login to vote
Ian Rutgers

External


Since: Nov 29, 2005
Posts: 4



(Msg. 3) Posted: Wed Nov 30, 2005 1:55 pm
Post subject: Re: Rookie PHP/MySQL Question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Scott Johnson" wrote in message

> Ian Rutgers wrote:
>> In querying a database
>>
>> ($result=query_db("SELECT photo_dir, photo_name FROM photograph_photo
>> WHERE photo_dir = '$dirToCheck'");
>>
>> is $result an array (where I could used a "in_array() function)?
>>
>> Thanks,
>>
>> Ian
> No the result is basicly a pointer to an external resource.
>
> You need to use an additional function to put that external resource into
> a usable value.
>
> The best way I find is to use (depending on the DB your using, there are
> different usages for each DB supported) is mysql_fetch_array($result)
>
> But with this you will need to step through the $result if more then one
> row is found.
>
> while($row = mysql_fetch_array($result)){
> foreach($row as $key => $vlaue){
> echo "Key: ".$key." (Value: ".$value.")";
> }
> }
>
> Hope this helps. If not post back.
> Scott

I thought I had this problem licked until I found I wasn't getting all the
values stored in my array ....
If I use:
while($row = mysql_fetch_array($result))
{
printf ($row["photo_name"]);
foreach($row as $key => $value)
{
$my_array[$key] = $value;
}
} //array now full of results from previous query
print_r($my_array);
$numArrayItems = count($my_array);
print("{$numArrayItems}\r");

I discover the numer of array items is only 2 (versus the 12 it should be).
If I use:
foreach($row as $value)
{
$my_array[] = $value;
}
I discover the number of array items is 24(double the number it should be).

What am doing wrong????

Thanks
 >> Stay informed about: Rookie PHP/MySQL Question 
Back to top
Login to vote
Ian Rutgers

External


Since: Nov 29, 2005
Posts: 4



(Msg. 4) Posted: Wed Nov 30, 2005 2:55 pm
Post subject: Re: Rookie PHP/MySQL Question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Ian Rutgers" wrote in message

>
> "Scott Johnson" wrote in message
>
>> Ian Rutgers wrote:
>>> In querying a database
>>>
>>> ($result=query_db("SELECT photo_dir, photo_name FROM photograph_photo
>>> WHERE photo_dir = '$dirToCheck'");
>>>
>>> is $result an array (where I could used a "in_array() function)?
>>>
>>> Thanks,
>>>
>>> Ian
>> No the result is basicly a pointer to an external resource.
>>
>> You need to use an additional function to put that external resource into
>> a usable value.
>>
>> The best way I find is to use (depending on the DB your using, there are
>> different usages for each DB supported) is mysql_fetch_array($result)
>>
>> But with this you will need to step through the $result if more then one
>> row is found.
>>
>> while($row = mysql_fetch_array($result)){
>> foreach($row as $key => $vlaue){
>> echo "Key: ".$key." (Value: ".$value.")";
>> }
>> }
>>
>> Hope this helps. If not post back.
>> Scott
>
> I thought I had this problem licked until I found I wasn't getting all the
> values stored in my array ....
> If I use:
> while($row = mysql_fetch_array($result))
> {
> printf ($row["photo_name"]);
> foreach($row as $key => $value)
> {
> $my_array[$key] = $value;
> }
> } //array now full of results from previous query
> print_r($my_array);
> $numArrayItems = count($my_array);
> print("{$numArrayItems}\r");
>
> I discover the numer of array items is only 2 (versus the 12 it should
> be).
> If I use:
> foreach($row as $value)
> {
> $my_array[] = $value;
> }
> I discover the number of array items is 24(double the number it should
> be).
>
> What am doing wrong????
>
> Thanks
>
Update ...With a Question ... I changed this:
while($row = mysql_fetch_array($result))
{
printf ($row["photo_name"]);
foreach($row as $key => $value)
{
$my_array[$key] = $value;
}
} //array now full of results from previous query

to this ....

while($row = mysql_fetch_array($result))
{
$my_array[] = $row["photo_name"];
}

and $my_array[] now stores the corrent number of image names!! Why does the
foreach(....) duplicate the entries?
 >> Stay informed about: Rookie PHP/MySQL Question 
Back to top
Login to vote
Rik

External


Since: Oct 16, 2005
Posts: 92



(Msg. 5) Posted: Thu Dec 01, 2005 6:55 am
Post subject: Re: Rookie PHP/MySQL Question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Ian Rutgers wrote:
> and $my_array[] now stores the corrent number of image names!! Why
> does the foreach(....) duplicate the entries?

foreach isn't the problem.

mysql_fetch_array does this:

www.php.net:
mysql_fetch_array -- Fetch a result row as an associative array, a numeric
array, or _both_

So is returns both numeric and named arrayvalues.
(For example: [key] => value:
[0] => value1
[name1] =>value1
[1] => value2
[name2] => value2)

Use mysql_fetch_assoc, or mysql_fetch_array($bla, MYSQL_ASSOC) or
mysql_fetch_array($bla, MYSQL_NUM).

In the second example you're only checking for named values, so it returns
just the one set, in the first you're accessing all values, also the
numeric.

Grtz,

Rik
 >> Stay informed about: Rookie PHP/MySQL Question 
Back to top
Login to vote
Ian Rutgers

External


Since: Nov 29, 2005
Posts: 4



(Msg. 6) Posted: Thu Dec 01, 2005 1:55 pm
Post subject: Re: Rookie PHP/MySQL Question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Rik" wrote in message

> Ian Rutgers wrote:
>> and $my_array[] now stores the corrent number of image names!! Why
>> does the foreach(....) duplicate the entries?
>
> foreach isn't the problem.
>
> mysql_fetch_array does this:
>
> www.php.net:
> mysql_fetch_array -- Fetch a result row as an associative array, a numeric
> array, or _both_
>
> So is returns both numeric and named arrayvalues.
> (For example: [key] => value:
> [0] => value1
> [name1] =>value1
> [1] => value2
> [name2] => value2)
>
> Use mysql_fetch_assoc, or mysql_fetch_array($bla, MYSQL_ASSOC) or
> mysql_fetch_array($bla, MYSQL_NUM).
>
> In the second example you're only checking for named values, so it returns
> just the one set, in the first you're accessing all values, also the
> numeric.
>
> Grtz,
>
> Rik
>
>
Thanks for the education!
 >> Stay informed about: Rookie PHP/MySQL Question 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
mysql question - If I write an update script, so people can upgrade their software to the newest version I've written, and since the last version I've created a new table in the database, so I want to add it, if it does not already exist, is the syntax something like ..

MySQL question, here? - I have a question about the design of a specific mysql database. My newsserver hasn't got many specific mysql related newsgroups and those who are supported don't seem to be very active. Since many of the MySQL work goes hand in hand with PHP this..

connection to mysql in question. - Using XP2, Apache2, php5.0.4 and mysql 5.0.18. All seem to be working individually but I seem to be having a problem with php files not connecting to the database. Is there an easy way to check this? Some like <?php phpinfo(); ?> TIA

MySQL design question - I'm creating an application for multiple cities (about 20-50 cities). I'm not sure whether to use a single table to store for all cities' items or break each one out into a seperate table for each city. I know a seperate tables will be faster for..

MYSQL ORDER BY question, sorting numbers - hi i have the following ORDER BY `tabelno` which gives me 1 10 11 12 13 14 15 16 17 18 19 2 20 when i want 1 2 3 etc how can i achieve this? thanks
   Database Forums (Home) -> PHP All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]