Bluehost.com Web Hosting $6.95

mysql_fetch_assoc($result) Question

This is a discussion on mysql_fetch_assoc($result) Question within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi all I have this problem: Ive made a select which selected "id" and "name" from ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-26-2006
Chris Grossbe
 
Posts: n/a
Default mysql_fetch_assoc($result) Question

Hi all

I have this problem:

Ive made a select which selected "id" and "name" from my database and i
get the right result(i used mysql_fetch_assoc).

now i want to get only the name with the id=4 for example.

I tried, thinking of the mysql_fetch_assoc array as a normal array, to
get this like that:

mysql_select_db($database_h, $h);
$query_partner = "SELECT id, name FROM waagenfirma";
$partner = mysql_query($query_partner, $h) or die(mysql_error());
$row_partner = mysql_fetch_assoc($partner);
$totalRows_partner = mysql_num_rows($partner);

echo $row_partner["4"]["name"];

but that only returns the 4th char from the first partner, not the name
of the partner with the id 4.

how can i do this? okay i can change the select to a "where id=4" but i
use that select for something else too(later on, so there is no pointer
prob).

any help or explanation to the mysql_fetch_assoc array appricated



Reply With Quote
  #2 (permalink)  
Old 01-26-2006
ZeldorBlat
 
Posts: n/a
Default Re: mysql_fetch_assoc($result) Question


Chris Grossbe wrote:
> Hi all
>
> I have this problem:
>
> Ive made a select which selected "id" and "name" from my database and i
> get the right result(i used mysql_fetch_assoc).
>
> now i want to get only the name with the id=4 for example.
>
> I tried, thinking of the mysql_fetch_assoc array as a normal array, to
> get this like that:
>
> mysql_select_db($database_h, $h);
> $query_partner = "SELECT id, name FROM waagenfirma";
> $partner = mysql_query($query_partner, $h) or die(mysql_error());
> $row_partner = mysql_fetch_assoc($partner);
> $totalRows_partner = mysql_num_rows($partner);
>
> echo $row_partner["4"]["name"];
>
> but that only returns the 4th char from the first partner, not the name
> of the partner with the id 4.
>
> how can i do this? okay i can change the select to a "where id=4" but i
> use that select for something else too(later on, so there is no pointer
> prob).
>
> any help or explanation to the mysql_fetch_assoc array appricated


mysql_fetch_assoc() will grab the "current" row and then advance the
"row pointer" to the next row so that the next call to
mysql_fetch_assoc() will grab the next row. In other words, you only
get one row at a time. While the best way would be to add something to
the where clause of the query, you can alternatively do something like
this:

mysql_select_db($database_h, $h);
$query_partner = "SELECT id, name FROM waagenfirma";
$partner = mysql_query($query_partner, $h) or die(mysql_error());

while(($row = mysql_fetch_assoc($partner)) !== false) {
if($row['id'] == 4) {
echo $row['name'];
break;
}
}

Which basically starts at the beginning of the result set, walking
through each row until it finds one where id = 4. Then it echos and
breaks out of the loop. It's worth noting that you may end up looking
through the whole result this way, though.

Reply With Quote
  #3 (permalink)  
Old 01-26-2006
Michael Austin
 
Posts: n/a
Default Re: mysql_fetch_assoc($result) Question

ZeldorBlat wrote:

> Chris Grossbe wrote:
>
>>Hi all
>>
>>I have this problem:
>>
>>Ive made a select which selected "id" and "name" from my database and i
>>get the right result(i used mysql_fetch_assoc).
>>
>>now i want to get only the name with the id=4 for example.
>>
>>I tried, thinking of the mysql_fetch_assoc array as a normal array, to
>>get this like that:
>>
>>mysql_select_db($database_h, $h);
>>$query_partner = "SELECT id, name FROM waagenfirma";
>>$partner = mysql_query($query_partner, $h) or die(mysql_error());
>>$row_partner = mysql_fetch_assoc($partner);
>>$totalRows_partner = mysql_num_rows($partner);
>>
>>echo $row_partner["4"]["name"];
>>
>>but that only returns the 4th char from the first partner, not the name
>>of the partner with the id 4.
>>
>>how can i do this? okay i can change the select to a "where id=4" but i
>>use that select for something else too(later on, so there is no pointer
>>prob).
>>
>>any help or explanation to the mysql_fetch_assoc array appricated

>
>
> mysql_fetch_assoc() will grab the "current" row and then advance the
> "row pointer" to the next row so that the next call to
> mysql_fetch_assoc() will grab the next row. In other words, you only
> get one row at a time. While the best way would be to add something to
> the where clause of the query, you can alternatively do something like
> this:
>
> mysql_select_db($database_h, $h);
> $query_partner = "SELECT id, name FROM waagenfirma";
> $partner = mysql_query($query_partner, $h) or die(mysql_error());
>
> while(($row = mysql_fetch_assoc($partner)) !== false) {
> if($row['id'] == 4) {
> echo $row['name'];
> break;
> }
> }
>
> Which basically starts at the beginning of the result set, walking
> through each row until it finds one where id = 4. Then it echos and
> breaks out of the loop. It's worth noting that you may end up looking
> through the whole result this way, though.
>


$id = 4;
$query_partner = "SELECT id, name FROM waagenfirma where id=".$id;

if the id field is a char/varchar then it needs to look like:

$id = "4";
$query_partner = "SELECT id, name FROM waagenfirma where id='".$id"'";


Use the database to select the row - don't return all rows and parse
through them. What happens when you have 100K names/id's... the
returned data will be significant.

Learn SQL and it will serve you well.

Michael Austin.
Reply With Quote
  #4 (permalink)  
Old 01-27-2006
Chris Grossbe
 
Posts: n/a
Default Re: mysql_fetch_assoc($result) Question

Thx for the answers and the explanation. Seems like creating the select
with the where is the only thing that makes sense.

I thought at it as a normal array, so i figured that a new select
wouldnt make lots of sense.

thx and greez
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:45 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0