p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: fetch_array


Message #1 by gareth.mccullough@v... on Thu, 21 Feb 2002 10:19:29
I am wanting to store different peoples training experiences within one 

table.  The table will have startdate, training and uid (userid).I want to 

show the training for a person that has been selected from a list.  I was 

thinking of using 

 while($query_data = mssql_fetch_array($result)){

but then realised that that would bring everyone's training into the table 

can more parameters be added to this while statement so that I could get 

it to just fetch the rows where a person's uid - $uid?



Thanks in advance,

GARETH 
Message #2 by "Hermawan Haryanto" <hermawan@h...> on Thu, 21 Feb 2002 18:43:56 +0700
Why you don't just do the correct sql statement, it will be harmfull if you

have plenty of data and do the sorting on the while, or PHP statement.

use this kinda of sql statement:

"SELECT * FROM yourtable WHERE uid='$uid'";

then when you execute that sql statement, the result is only the person's

training.



Thanks



Hermawan Haryanto

hermawan@h...

----- Original Message -----

From: <gareth.mccullough@v...>

To: "beginning php" <beginning_php@p...>

Sent: Thursday, February 21, 2002 10:19 AM

Subject: [beginning_php] fetch_array





> I am wanting to store different peoples training experiences within one

> table.  The table will have startdate, training and uid (userid).I want to

> show the training for a person that has been selected from a list.  I was

> thinking of using

>  while($query_data = mssql_fetch_array($result)){

> but then realised that that would bring everyone's training into the table

> can more parameters be added to this while statement so that I could get

> it to just fetch the rows where a person's uid - $uid?

>

> Thanks in advance,

> GARETH




$subst('Email.Unsub').

>



Message #3 by "Krister" <krister@m...> on Thu, 21 Feb 2002 17:10:37 +0100
Hi Gareth



This wil print the row from the first uid - uid number 1 - and your

tablename is people:



<?php



$query = mysql_query("SELECT uid, startdate, training FROM people WHERE uid

LIKE '1'");

$query_data = mysql_fetch_array( $query );



echo $query_data[0]; // the uid

echo $query_data[1]; // the startdate as it is, you may have to convert to

date()

echo $query_data[2]; // the training



?>



So, just drop the while.



Krister



----- Original Message -----

From: <gareth.mccullough@v...>

To: "beginning php" <beginning_php@p...>

Sent: Thursday, February 21, 2002 10:19 AM

Subject: [beginning_php] fetch_array





> I am wanting to store different peoples training experiences within one

> table.  The table will have startdate, training and uid (userid).I want to

> show the training for a person that has been selected from a list.  I was

> thinking of using

>  while($query_data = mssql_fetch_array($result)){

> but then realised that that would bring everyone's training into the table

> can more parameters be added to this while statement so that I could get

> it to just fetch the rows where a person's uid - $uid?

>

> Thanks in advance,

> GARETH




$subst('Email.Unsub').

>



Message #4 by "Krister" <krister@m...> on Thu, 21 Feb 2002 17:23:52 +0100
Hi, again. Sorry there is a litlle mistake.



...LIKE 'your_row'");  of course, not number 1



Krister



----- Original Message -----

From: "Krister" <krister@m...>

To: "beginning php" <beginning_php@p...>

Sent: Thursday, February 21, 2002 5:10 PM

Subject: [beginning_php] Re: fetch_array





> Hi Gareth

>

> This wil print the row from the first uid - uid number 1 - and your

> tablename is people:

>

> <?php

>

> $query = mysql_query("SELECT uid, startdate, training FROM people WHERE

uid

> LIKE '1'");

> $query_data = mysql_fetch_array( $query );

>

> echo $query_data[0]; // the uid

> echo $query_data[1]; // the startdate as it is, you may have to convert to

> date()

> echo $query_data[2]; // the training

>

> ?>

>

> So, just drop the while.

>

> Krister

>

> ----- Original Message -----

> From: <gareth.mccullough@v...>

> To: "beginning php" <beginning_php@p...>

> Sent: Thursday, February 21, 2002 10:19 AM

> Subject: [beginning_php] fetch_array

>

>

> > I am wanting to store different peoples training experiences within one

> > table.  The table will have startdate, training and uid (userid).I want

to

> > show the training for a person that has been selected from a list.  I

was

> > thinking of using

> >  while($query_data = mssql_fetch_array($result)){

> > but then realised that that would bring everyone's training into the

table

> > can more parameters be added to this while statement so that I could get

> > it to just fetch the rows where a person's uid - $uid?

> >

> > Thanks in advance,

> > GARETH




> $subst('Email.Unsub').

> >

>

>




$subst('Email.Unsub').



Message #5 by "Nikolai Devereaux" <yomama@u...> on Thu, 21 Feb 2002 08:55:03 -0800

small side note -- why not use mysql_fetch_assoc instead?  It's not that

much slower or inefficient to run instead of fetch_array, and your array

indexes are the string column names that you selected in your query

statement.



This is such a useful tool because if you should ever have to modify a

querys slightly or whatever, your code should still work fine after your

modifications, provided you're still selecting the same stuff.



Relying on numerical array indexes as the result of a query is a little

misleading.  If you've truly abstracted your database connectivity and

queries layer from your application and called FUNCTIONS instead of running

queries in place (mimicing stored procedures, I guess) then it's all the

more misleading to see this:



$training = getUserTrainingInfo($userId);

echo "Name: $training[0]";



When you could see this instead:

echo "Name: $training[name]";





okay, small personal rant over. =D



nik



> -----Original Message-----

> From: Krister [mailto:krister@m...]

> Sent: Thursday, February 21, 2002 8:24 AM

> To: beginning php

> Subject: [beginning_php] Re: fetch_array

>

>

> Hi, again. Sorry there is a litlle mistake.

>

> ...LIKE 'your_row'");  of course, not number 1

>

> Krister

>

> ----- Original Message -----

> From: "Krister" <krister@m...>

> To: "beginning php" <beginning_php@p...>

> Sent: Thursday, February 21, 2002 5:10 PM

> Subject: [beginning_php] Re: fetch_array

>

>

> > Hi Gareth

> >

> > This wil print the row from the first uid - uid number 1 - and your

> > tablename is people:

> >

> > <?php

> >

> > $query = mysql_query("SELECT uid, startdate, training FROM people WHERE

> uid

> > LIKE '1'");

> > $query_data = mysql_fetch_array( $query );

> >

> > echo $query_data[0]; // the uid

> > echo $query_data[1]; // the startdate as it is, you may have to

> convert to

> > date()

> > echo $query_data[2]; // the training

> >

> > ?>

> >

> > So, just drop the while.

> >

> > Krister

> >

> > ----- Original Message -----

> > From: <gareth.mccullough@v...>

> > To: "beginning php" <beginning_php@p...>

> > Sent: Thursday, February 21, 2002 10:19 AM

> > Subject: [beginning_php] fetch_array

> >

> >

> > > I am wanting to store different peoples training experiences

> within one

> > > table.  The table will have startdate, training and uid

> (userid).I want

> to

> > > show the training for a person that has been selected from a list.  I

> was

> > > thinking of using

> > >  while($query_data = mssql_fetch_array($result)){

> > > but then realised that that would bring everyone's training into the

> table

> > > can more parameters be added to this while statement so that

> I could get

> > > it to just fetch the rows where a person's uid - $uid?

> > >

> > > Thanks in advance,

> > > GARETH




> > $subst('Email.Unsub').

> > >

> >

> >




> $subst('Email.Unsub').

>

>




> $subst('Email.Unsub').



Message #6 by "Nikolai Devereaux" <yomama@u...> on Tue, 26 Feb 2002 10:05:36 -0800

omg, i'm a loser ... I completely misread your original post.  I didn't see

"mssql" I saw "mysql".



The mssql_fetch_assoc is implemented, but I don't think they've added it to

the release version of PHP yet -- they say that it's only available in the

cvs snapshot:



http://www.php.net/manual/en/function.mssql-fetch-assoc.php





Anyway, it sounds like you're running a query that returns several rows...

what you'd have to do is loop through each row.





$query = "select name, course from training where uid='$uid'";

$res   = mssql_query($query);



while($row = mssql_fetch_row($res))

{

    echo "$row[0] is enrolled in $row[1].\n";

}





hope this helps,



nik



> -----Original Message-----

> From: Gareth McCullough [mailto:gareth.mccullough@v...]

> Sent: Tuesday, February 26, 2002 8:02 AM

> To: 'yomama@u...'

> Subject: FW: [beginning_php] Re: fetch_array

>

>

> Nik

> 	Firstly cheers for the feedback.  I am not entirly sure how the

> mssql_fetch_assoc works (plus not sure if it works using microsoft sql 7).

> I need your help to try and get my head around this problem I am

> having (it

> will probably be real easy to solve but my problem is I can never see the

> easy way).

> 	What I am trying to do is have different peoples training all in one

> database with database columns:

>

> uid, startdate, description

>

> 	I then have them in a table on a web page under headings Start &

> Course.  I can get them to go into the table under the right headings, but

> the problem is that my php will only read the first record where uid 

> '$uid';

>

> ie.$query = "select * from training where uid ='$uid'";

> $result = mssql_query($query);

>

> The database contains serveral records with the same uid.  So I would like

> the web page to show all of these records within the same table.

> So really

> what I need to do is read through the whole of the databse finding all

> records where uid='$uid' and select the startdate and description from the

> database and put them into the table.

>

> Hope this makes sense to you.  If you can't help or don't have

> time to help,

> thats no problem but cheers anyway.

> Thanks

> Gareth

>

>

> -----Original Message-----

> From: Nikolai Devereaux [mailto:yomama@u...]

> Sent: Thursday, February 21, 2002 4:55 PM

> To: beginning php

> Subject: [beginning_php] Re: fetch_array

>

>

>

> small side note -- why not use mysql_fetch_assoc instead?  It's not that

> much slower or inefficient to run instead of fetch_array, and your array

> indexes are the string column names that you selected in your query

> statement.

>

> This is such a useful tool because if you should ever have to modify a

> querys slightly or whatever, your code should still work fine after your

> modifications, provided you're still selecting the same stuff.

>

> Relying on numerical array indexes as the result of a query is a little

> misleading.  If you've truly abstracted your database connectivity and

> queries layer from your application and called FUNCTIONS instead

> of running

> queries in place (mimicing stored procedures, I guess) then it's all the

> more misleading to see this:

>

> $training = getUserTrainingInfo($userId);

> echo "Name: $training[0]";

>

> When you could see this instead:

> echo "Name: $training[name]";

>

>

> okay, small personal rant over. =D

>

> nik

>

>

> > > ----- Original Message -----

> > > From: <gareth.mccullough@v...>

> > > To: "beginning php" <beginning_php@p...>

> > > Sent: Thursday, February 21, 2002 10:19 AM

> > > Subject: [beginning_php] fetch_array

> > >

> > >

> > > > I am wanting to store different peoples training experiences

> > within one

> > > > table.  The table will have startdate, training and uid

> > (userid).I want

> > to

> > > > show the training for a person that has been selected from

> a list.  I

> > was

> > > > thinking of using

> > > >  while($query_data = mssql_fetch_array($result)){

> > > > but then realised that that would bring everyone's training into the

> > table

> > > > can more parameters be added to this while statement so that

> > I could get

> > > > it to just fetch the rows where a person's uid - $uid?

> > > >

> > > > Thanks in advance,

> > > > GARETH




  Return to Index