PHP and MySQL SELECT 
Author Message
 PHP and MySQL SELECT

I've been hammering at this for hours and can't find a mistake, but it's not
working :(

I have a table called    friends
friends   has 3 fields   f_name   l_name   town

Here's the sample info that I have in the table at the moment (fields
seperated by ;)

Bob;Davies;Mansfield
Alan;Davies;Mansfield
David;Bryant;Nottingingham
Sean;Baird;Mansfield
Iris;Evans;Mansfield
Josie;Wales;Mansfield

When I do the following with PHP...

  $mytown = "Mansfield";
  $res = mysql_query("SELECT count(f_name) FROM friends WHERE
town='$mytown'");
  $get_friends = mysql_fetch_array($res);

I get $get_friends with the expected value of 5 (5 of them live in
Mansfield)

When I do the following with PHP...

  $mytown = "Mansfield";
  $res = mysql_query("SELECT f_name FROM friends WHERE town='$mytown'");
  $get_friends = mysql_fetch_array($res);

I get $get_friends[0] = Bob

but no other elements in the array?

I expected to get      $get_friends=(Bob,Alan,Sean,Iris,Josie)

Using phpMyAdmin on my host and entering this query

SQL-query : SELECT `f_name` FROM `friends` WHERE town='Mansfield'

It does indeed give me the correct 5 f_name's

Please could someone point out my silly mistake, if there is one, or give me
some idea as to what the problem could be?

Many thanks in advance,

Terry



Sun, 22 May 2005 06:03:00 GMT  
 PHP and MySQL SELECT
Hi,

You can use while loop for fetching the rows you need. Try this:

$mytown = "Mansfield";
$res = mysql_query("SELECT f_name FROM friends WHERE town='$mytown'");
while($get_friends = mysql_fetch_row($res))
{
    $f_name_array[]=$get_friends[0];

Quote:
}

after that you have a $f_name_array including all the names from Mansfield.

--
jarno


Quote:
> I've been hammering at this for hours and can't find a mistake, but it's
not
> working :(

> I have a table called    friends
> friends   has 3 fields   f_name   l_name   town

> Here's the sample info that I have in the table at the moment (fields
> seperated by ;)

> Bob;Davies;Mansfield
> Alan;Davies;Mansfield
> David;Bryant;Nottingingham
> Sean;Baird;Mansfield
> Iris;Evans;Mansfield
> Josie;Wales;Mansfield

> When I do the following with PHP...

>   $mytown = "Mansfield";
>   $res = mysql_query("SELECT count(f_name) FROM friends WHERE
> town='$mytown'");
>   $get_friends = mysql_fetch_array($res);

> I get $get_friends with the expected value of 5 (5 of them live in
> Mansfield)

> When I do the following with PHP...

>   $mytown = "Mansfield";
>   $res = mysql_query("SELECT f_name FROM friends WHERE town='$mytown'");
>   $get_friends = mysql_fetch_array($res);

> I get $get_friends[0] = Bob

> but no other elements in the array?

> I expected to get      $get_friends=(Bob,Alan,Sean,Iris,Josie)

> Using phpMyAdmin on my host and entering this query

> SQL-query : SELECT `f_name` FROM `friends` WHERE town='Mansfield'

> It does indeed give me the correct 5 f_name's

> Please could someone point out my silly mistake, if there is one, or give
me
> some idea as to what the problem could be?

> Many thanks in advance,

> Terry



Sun, 22 May 2005 06:10:37 GMT  
 PHP and MySQL SELECT
Amazing how you can come across the answer as soon as you ask the question
LOL

Apparently I should have been doing this

while ($get_friends = mysql_fetch_row($res))
{
// and here they all are ;-)

Quote:
}

If anyone has any comments on whether this is right or not I'll gladly
accept them. I know it works but I also know that doesn't necessarily make
it the best way.

Terry


Quote:
> I've been hammering at this for hours and can't find a mistake, but it's
not
> working :(

> I have a table called    friends
> friends   has 3 fields   f_name   l_name   town

> Here's the sample info that I have in the table at the moment (fields
> seperated by ;)

> Bob;Davies;Mansfield
> Alan;Davies;Mansfield
> David;Bryant;Nottingingham
> Sean;Baird;Mansfield
> Iris;Evans;Mansfield
> Josie;Wales;Mansfield

> When I do the following with PHP...

>   $mytown = "Mansfield";
>   $res = mysql_query("SELECT count(f_name) FROM friends WHERE
> town='$mytown'");
>   $get_friends = mysql_fetch_array($res);

> I get $get_friends with the expected value of 5 (5 of them live in
> Mansfield)

> When I do the following with PHP...

>   $mytown = "Mansfield";
>   $res = mysql_query("SELECT f_name FROM friends WHERE town='$mytown'");
>   $get_friends = mysql_fetch_array($res);

> I get $get_friends[0] = Bob

> but no other elements in the array?

> I expected to get      $get_friends=(Bob,Alan,Sean,Iris,Josie)

> Using phpMyAdmin on my host and entering this query

> SQL-query : SELECT `f_name` FROM `friends` WHERE town='Mansfield'

> It does indeed give me the correct 5 f_name's

> Please could someone point out my silly mistake, if there is one, or give
me
> some idea as to what the problem could be?

> Many thanks in advance,

> Terry



Sun, 22 May 2005 06:18:51 GMT  
 PHP and MySQL SELECT
Thanks jarno, your reply didn't come up until after I made my second post
(bl00dy news servers) but it's good to know that I was heading in the right
direction ;-)

Thanks again

Terry


Quote:
> Hi,

> You can use while loop for fetching the rows you need. Try this:

> $mytown = "Mansfield";
> $res = mysql_query("SELECT f_name FROM friends WHERE town='$mytown'");
> while($get_friends = mysql_fetch_row($res))
> {
>     $f_name_array[]=$get_friends[0];
> }

> after that you have a $f_name_array including all the names from
Mansfield.

> --
> jarno



> > I've been hammering at this for hours and can't find a mistake, but it's
> not
> > working :(

> > I have a table called    friends
> > friends   has 3 fields   f_name   l_name   town

> > Here's the sample info that I have in the table at the moment (fields
> > seperated by ;)

> > Bob;Davies;Mansfield
> > Alan;Davies;Mansfield
> > David;Bryant;Nottingingham
> > Sean;Baird;Mansfield
> > Iris;Evans;Mansfield
> > Josie;Wales;Mansfield

> > When I do the following with PHP...

> >   $mytown = "Mansfield";
> >   $res = mysql_query("SELECT count(f_name) FROM friends WHERE
> > town='$mytown'");
> >   $get_friends = mysql_fetch_array($res);

> > I get $get_friends with the expected value of 5 (5 of them live in
> > Mansfield)

> > When I do the following with PHP...

> >   $mytown = "Mansfield";
> >   $res = mysql_query("SELECT f_name FROM friends WHERE town='$mytown'");
> >   $get_friends = mysql_fetch_array($res);

> > I get $get_friends[0] = Bob

> > but no other elements in the array?

> > I expected to get      $get_friends=(Bob,Alan,Sean,Iris,Josie)

> > Using phpMyAdmin on my host and entering this query

> > SQL-query : SELECT `f_name` FROM `friends` WHERE town='Mansfield'

> > It does indeed give me the correct 5 f_name's

> > Please could someone point out my silly mistake, if there is one, or
give
> me
> > some idea as to what the problem could be?

> > Many thanks in advance,

> > Terry



Sun, 22 May 2005 06:29:18 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. help with mysql/php selecting record

2. problem with select output with PHP/MySQL

3. PHP, MySQL & Date Select

4. php mysql sample code php shareware like KB knowledgebase

5. PHP+MySQL vs. PHP+PostgreSQL

6. php + mysql or php + postgresql?

7. RexxSQL 2.4 and MySQL fetch/select problems

8. Option Select link to MySQL

9. selecting files from MySQL

10. Performance Problems when selecting HUGE amounts of data from MySQL dbs

11. Performance Problems when selecting HUGE amounts of data from MySQL dbs

12. Performance Problems when selecting HUGE amounts of data from MySQL dbs

 

 
Powered by phpBB® Forum Software