[PHP-DB] Re: MySQL query from multiple tables

2002-06-29 Thread andy

hello Matthew:

if I understood your problem right, you have to join the two tables.

E.g:

SELECT
ph.*,
pf.prod_ID,
pf.feat_ID,
pf.feat_value
FROM
product_head ph,
product_features pf

WHERE
ph.prod_cat_ID = 1
AND ph.prod_cat_ID = pf.prod_cat_ID /*join the two tables with the same
id*/
AND pf.yourcriteria = whatever /* put your criteria for table 2 here*/

Hope this helps a bit,

Andy

--
--
http://www.globosapiens.net
Global Travellers Network


Matthew Nock [EMAIL PROTECTED] schrieb im Newsbeitrag
news:[EMAIL PROTECTED]...
 Hi All,

 I have a strange query which I need to build - I have looked at the JOIN
and
 aliasing functions of mySQL but not sure if I can achieve want I want to
do.

 I have two tables (product_head, and product_features) (in addition to a
 number of other tables) which form the list of products I have available,
 and the features available with each product.

 I want to be able to retrieve from product_head the rows which are for a
 specific product category (ie: 1)..  this bit is easy.

 SELECT * from product_head where prod_cat_ID = 1;

 This query could return 1 or more rows - works great.

 but I also want to retrieve records from the product_features table.
 information stored in this table is:

 prod_ID feat_ID feat_value

 so for each product ID, there could be any number of features.  I need to
 retrieve only certain feat_value records, for specific products, where the
 feat_ID equals certain values .

 can I build a single query that will return all these results similar to
 below?


 Prod_ID Prod_name Feat_1_value Feat_2_value feat_3_value
 1 prod1 34 25 15
 2 prod2 10 15 15
 ...

 does this all make sense?




-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] Re: [PHP] saving temporary image to database

2002-06-29 Thread Rasmus Lerdorf

There is no imagejpeg() call in the code snippet you provided.  And you
don't say how it is failing.  You haven't provided us with enough data to
answer this question.

-Rasmus

On Sat, 29 Jun 2002, andy wrote:

 Hi there,

 I would like to save a jpg into a blob field of mysql. The function
 underneath works fine if I read the image from the temporary destination
 where php did put it after uploading.

 My problem is, that I would like to do some funky stuff to the image like
 changing colors or adding watermarks. So I have several functions ahead
 before I used to store them successfully to the file system. Now I would
 like to store it to a blob field, but this does not work. Like I said it
 works to store the temp file, but not the other one. I guess it has to do
 with something regarding the imagejpeg function.

 Here is what I tryed:
   #
# save image to db into blob

// this does not work (outputImg is a the colorcorrected file)
  $data = addslashes($outputImg);

 // this one would work
# $data = addslashes(fread(fopen($picture_location, r),
 filesize($picture_location)));

   $stmt =
INSERT INTO test.picture_test
 (file_name, file_type, picture)
 VALUES
 ('$name', '$picture_location_type', '$data')
   ;
   execute_stmt($stmt, $link);
   #

 Maybe some of you guy has a good idea on that.

 Thanx for any help,

 Andy



 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] Re: [PHP] saving temporary image to database

2002-06-29 Thread andy

... sorry I thought it would be to much code.
Here is the complete code :

  $inputImg = ImageCreateFromJPEG($picture_location);

  # old size
  $srcX = imagesx($inputImg);
  $srcY = imagesy($inputImg);

   # new size
   $ratio = ($srcY / $dstY);
   $dstX = ($srcX / $ratio);

   $outputImg = ImageCreateTrueColor($maxX, $dstY);
   imagefill($outputImg, 0, 0, ImageColorAllocate($outputImg, 0, 0,0));
   imagecopyresampled($outputImg, $inputImg, (($maxX - $dstX) / 2),0,0,0,
$dstX, $dstY, $srcX, $srcY);

#
 # save image to db into blob

// this does not work (outputImg is a the colorcorrected file)
   $data = addslashes($outputImg);

 // this one would work
# $data = addslashes(fread(fopen($picture_location, r),
filesize($picture_location)));

  $stmt =
INSERT INTO test.picture_test
(file_name, file_type, picture)
VALUES
 ('$name', '$picture_location_type', '$data')
  ;
   execute_stmt($stmt, $link);
  #


Rasmus Lerdorf [EMAIL PROTECTED] schrieb im Newsbeitrag
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
 There is no imagejpeg() call in the code snippet you provided.  And you
 don't say how it is failing.  You haven't provided us with enough data to
 answer this question.

 -Rasmus

 On Sat, 29 Jun 2002, andy wrote:

  Hi there,
 
  I would like to save a jpg into a blob field of mysql. The function
  underneath works fine if I read the image from the temporary destination
  where php did put it after uploading.
 
  My problem is, that I would like to do some funky stuff to the image
like
  changing colors or adding watermarks. So I have several functions ahead
  before I used to store them successfully to the file system. Now I would
  like to store it to a blob field, but this does not work. Like I said it
  works to store the temp file, but not the other one. I guess it has to
do
  with something regarding the imagejpeg function.
 
  Here is what I tryed:
#
 # save image to db into blob
 
 // this does not work (outputImg is a the colorcorrected file)
   $data = addslashes($outputImg);
 
  // this one would work
 # $data = addslashes(fread(fopen($picture_location, r),
  filesize($picture_location)));
 
$stmt =
 INSERT INTO test.picture_test
  (file_name, file_type, picture)
  VALUES
  ('$name', '$picture_location_type', '$data')
;
execute_stmt($stmt, $link);
#
 
  Maybe some of you guy has a good idea on that.
 
  Thanx for any help,
 
  Andy
 
 
 
  --
  PHP General Mailing List (http://www.php.net/)
  To unsubscribe, visit: http://www.php.net/unsub.php
 

--
--
http://www.globosapiens.net
Global Travellers Network



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] AVG fucntion help

2002-06-29 Thread CrossWalkCentral

Using PHP

I need some help running and AVG qery

the syntax is as follows

$results=sql_query(Select AVG(rate) from MYTABLENAME where id=$id,$dbi)

This works but I am not sure how to get is value into a variable.



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] Excel to MySQL??

2002-06-29 Thread Chase

Does anyone know of a way that I can export an Excel spreadsheet to a 
file that I can import to MySQL?  I found a program that will do this 
for Access, but I would rather leave Access out of this if possible. 
Thanks!!

Chase


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP-DB] Excel to MySQL??

2002-06-29 Thread Peter Lovatt

Hi

from Excel export to csv

use phpMyAdmin to load it into mysql (recent versions of phpMyAdmin has an
Excel import function)

HTH

Peter

---
Excellence in internet and open source software
---
Sunmaia
www.sunmaia.net
[EMAIL PROTECTED]
tel. 0121-242-1473
---

 -Original Message-
 From: Chase [mailto:[EMAIL PROTECTED]]
 Sent: 29 June 2002 22:22
 To: [EMAIL PROTECTED]
 Subject: [PHP-DB] Excel to MySQL??


 Does anyone know of a way that I can export an Excel spreadsheet to a
 file that I can import to MySQL?  I found a program that will do this
 for Access, but I would rather leave Access out of this if possible.
 Thanks!!

 Chase


 --
 PHP Database Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP-DB] Excel to MySQL??

2002-06-29 Thread Jen Swofford

Or if you don't use phpMyAdmin you can use LOAD DATA INFILE once you have
saved the Excel doc as a .csv file:

http://www.mysql.com/doc/L/O/LOAD_DATA.html

Jen Swofford
[EMAIL PROTECTED]

 -Original Message-
 From: Peter Lovatt [mailto:[EMAIL PROTECTED]]
 Sent: Saturday, June 29, 2002 4:49 PM
 To: Chase; [EMAIL PROTECTED]
 Subject: RE: [PHP-DB] Excel to MySQL??


 Hi

 from Excel export to csv

 use phpMyAdmin to load it into mysql (recent versions of phpMyAdmin has an
 Excel import function)

 HTH

 Peter

 ---
 Excellence in internet and open source software
 ---
 Sunmaia
 www.sunmaia.net
 [EMAIL PROTECTED]
 tel. 0121-242-1473
 ---

  -Original Message-
  From: Chase [mailto:[EMAIL PROTECTED]]
  Sent: 29 June 2002 22:22
  To: [EMAIL PROTECTED]
  Subject: [PHP-DB] Excel to MySQL??
 
 
  Does anyone know of a way that I can export an Excel spreadsheet to a
  file that I can import to MySQL?  I found a program that will do this
  for Access, but I would rather leave Access out of this if possible.
  Thanks!!
 
  Chase
 
 
  --
  PHP Database Mailing List (http://www.php.net/)
  To unsubscribe, visit: http://www.php.net/unsub.php
 


 --
 PHP Database Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php






-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP-DB] Excel to MySQL??

2002-06-29 Thread Paul DuBois

At 15:21 -0600 6/29/02, Chase wrote:
Does anyone know of a way that I can export an Excel spreadsheet to 
a file that I can import to MySQL?  I found a program that will do 
this for Access, but I would rather leave Access out of this if 
possible. Thanks!!

Chase

Does it have to be written in PHP? :-)

#! /usr/bin/perl -w
# from_excel.pl - read Excel spreadsheet, write tab-delimited,
# linefeed-terminated output to the standard output.

use strict;
use Spreadsheet::ParseExcel::Simple;

ARGV or die Usage: $0 excel-file\n;

my $xls = Spreadsheet::ParseExcel::Simple-read ($ARGV[0]);
foreach my $sheet ($xls-sheets ())
{
 while ($sheet-has_data ())
 { 
 my data = $sheet-next_row ();
 print join (\t, data) . \n;
 }
}

exit (0);

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] NEW help finding sum or average

2002-06-29 Thread CrossWalkCentral

I have a table called TEST , it cotains 3 fileds
A
B
C

I can have several rows where A is the same value.

I need help with PHP I need to obtain the AVERAGE of B where A=1

Can some one help me on this?





-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP-DB] Excel to MySQL??

2002-06-29 Thread Chase

My webhost is running phpMyAdmin v2.2.2 and it is giving me errors when 
I try to load the file.  Are there any certain characters that cannot be 
in the file?  (ie. $)

Chase

Peter Lovatt wrote:

 Hi
 
 from Excel export to csv
 
 use phpMyAdmin to load it into mysql (recent versions of phpMyAdmin has an
 Excel import function)
 
 HTH
 
 Peter
 
 ---
 Excellence in internet and open source software
 ---
 Sunmaia
 www.sunmaia.net
 [EMAIL PROTECTED]
 tel. 0121-242-1473
 ---
 
 
-Original Message-
From: Chase [mailto:[EMAIL PROTECTED]]
Sent: 29 June 2002 22:22
To: [EMAIL PROTECTED]
Subject: [PHP-DB] Excel to MySQL??


Does anyone know of a way that I can export an Excel spreadsheet to a
file that I can import to MySQL?  I found a program that will do this
for Access, but I would rather leave Access out of this if possible.
Thanks!!

Chase


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


 


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] Usiing FOREACH to loop through Array

2002-06-29 Thread Brad Melendy

Hi All,
I've stumped myself here.  In a nutshell, I have a function that returns my
array based on a SQL query and here's the code:

-begin code---
function getCourses($UID)
 {
 global $link;
 $result = mysql_query( SELECT C.CourseName FROM tblcourses C, tblusers U,
tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND U.ID =
$UID, $link );
 if ( ! $result )
  die ( getRow fatal error: .mysql_error() );
 return mysql_fetch_array( $result );
 }
end code 

I call this from a PHP page with the following code:

begin code--
$myCourses = getCourses($session[id]);
foreach ($myCourses as $value)
 {
 print br$value;
 }
end code---

Now, when I test the SQL from my function directly on the database, it
returns just want I want it to return but it isn't working that way on my
PHP page. For results where there is a single entry, I am getting the same
entry TWICE and for records with more than a single entry I am getting ONLY
the FIRST entry TWICE.

Now I know my SQL code is correct (I am testing it against a MySQL database
using MySQL-Front) so I suspect I'm doing something stupid in my foreach
loop.

I'm hoping someone will spot my dumb mistake.  Thanks very much for any help
at all on this.

Brad



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] Traversing an array usng for loops?

2002-06-29 Thread Matthew Nock

I have a query which returns a number of product details.

there may be one or more rows for each product category  for example:


prod_ID feature_ID  feature_cat_ID  feature_value .
1   2   1   10
1   1   2   100
1   4   6   4
1   87  1   500


how do I process this array so that all my results for each feature-category
are processed in their own for loops?

ie : I want to go through the array for feature-category 1, and create a row
in a HTML Table for each feature in category 1..  then move the category
two, then three and so on.

help! :)




-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] Re: Excel to MySQL??

2002-06-29 Thread Manuel Lemos

Hello,

On 06/29/2002 06:21 PM, Chase wrote:
 Does anyone know of a way that I can export an Excel spreadsheet to a 
 file that I can import to MySQL?  I found a program that will do this 
 for Access, but I would rather leave Access out of this if possible.

I think you can do it via ODBC under Windows. Anyway, if you are running 
PHP under Windows, you may as well use COM objects to access to Excel 
spreadsheets. In that case you may want to try this class:

http://www.phpclasses.org/browse.html/package/86.html

-- 

Regards,
Manuel Lemos


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] 405 Error with Apache 1.3.20/PHP 4.2.1

2002-06-29 Thread Tim Legg


Good Afternoon Everyone,

I have PHP 4.2.1 installed and have my phpinfo(); available for you at:

http://pc047113.stuorg.iastate.edu/phpinfo.php

And have Apache 1.3.20 installed and its httpd.conf is available at:

http://www.public.iastate.edu/~legg/freebsd/httpd.txt



I was playing around with an online tutorial for using php with mysql at:

http://hotwired.lycos.com/webmonkey/99/21/index3a_page4.html?tw=programming

I am working my way through the tutorial just fine, but I run into trouble
when it comes to POSTing from web forms.  PHP interacts with MySQL just
fine, but Apache has a problem with me posting form data.

What I get back is a 405 Method Not Allowed.

The requested method POST is not allowed for the URL /index.html


I find this interesting because index.html has nothing to do with my
experimental page.  You can recreate the error here:

http://pc047113.stuorg.iastate.edu/test.php

If you wish to see the source (with mysql password modified, of course),
refer to:

http://www.public.iastate.edu/~legg/freebsd/test.php.txt


What I need to know is what must I change to allow a web user to enter
arbitrary data and allow Apache to return the data to php/mysql.


Thanks In Advance,

Tim Legg




-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] Re: [PHP] Usiing FOREACH to loop through Array

2002-06-29 Thread Steve Edberg

At 3:27 PM -0700 6/29/02, Brad Melendy wrote:
Hi All,
I've stumped myself here.  In a nutshell, I have a function that returns my
array based on a SQL query and here's the code:

-begin code---
function getCourses($UID)
  {
  global $link;
  $result = mysql_query( SELECT C.CourseName FROM tblcourses C, tblusers U,
tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND U.ID =
$UID, $link );
  if ( ! $result )
   die ( getRow fatal error: .mysql_error() );
  return mysql_fetch_array( $result );
  }
end code 

I call this from a PHP page with the following code:

begin code--
$myCourses = getCourses($session[id]);
foreach ($myCourses as $value)
  {
  print br$value;
  }
end code---

Now, when I test the SQL from my function directly on the database, it
returns just want I want it to return but it isn't working that way on my
PHP page. For results where there is a single entry, I am getting the same
entry TWICE and for records with more than a single entry I am getting ONLY
the FIRST entry TWICE.

Now I know my SQL code is correct (I am testing it against a MySQL database
using MySQL-Front) so I suspect I'm doing something stupid in my foreach
loop.


I think your problem lies in a misunderstanding of the 
mysql_fetch_array() function. It doesn't return the entire result set 
in an array - just one record at a time. You can fix this in one of 
two ways:

(1) Loop though the entire result set in your function:

function getCourses($UID)
 {
  global $link;

  $ResultSet = array();

  $result = mysql_query( SELECT C.CourseName FROM tblcourses C, tblusers U,
   tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND U.ID =
   $UID, $link );

  if ( ! $result )
   die ( getRow fatal error: .mysql_error() );

  while ($Row = mysql_fetch_array( $result ))
  {
   $ResultSet[] = $Row['C.CourseName'];
  }

  return $ResultSet;
 }

...

$myCourses = getCourses($session[id]);
foreach ($myCourses as $value)
 {
 print br$value;
 }

or (2) set a flag in getCourses() so that the query is only executed 
once, otherwise returning a result line - something like:

function getCourses($UID)
 global $link;
 static $result = false;

 if (!$result)
  {
$result = mysql_query( SELECT C.CourseName FROM tblcourses C, 
tblusers U,
 tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND U.ID =
 $UID, $link );
if ( ! $result )
 die ( getRow fatal error: .mysql_error() );
  }
 {
 return mysql_fetch_array( $result );
 }

...

while ($Row = getCourses($session[id]) as $value)
 {
 print br, $Row['C.CourseName'];
 }

(standard caveats about off-top-of-head, untested code apply)

The reason you are getting the first record TWICE is becaouse of the 
default behaviour of the mysql_fetch_array() function. It returns 
both an associative array - ie, elements of the form field-name = 
value - and a numerically indexed array (0, 1, 2, etc.). You can 
alter this behaviour by the second parameter of the function: see

http://www.php.net/manual/en/function.mysql-fetch-array.php

-steve


I'm hoping someone will spot my dumb mistake.  Thanks very much for any help
at all on this.

Brad



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


-- 
++
| Steve Edberg  [EMAIL PROTECTED] |
| University of California, Davis  (530)754-9127 |
| Programming/Database/SysAdmin   http://pgfsun.ucdavis.edu/ |
++
| The end to politics as usual:  |
| The Monster Raving Loony Party (http://www.omrlp.com/) |
++

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] Re: [PHP] Usiing FOREACH to loop through Array

2002-06-29 Thread Steve Edberg

Oops! It's hot, it's Saturday, brain not functioning at 100%, made 
cut'n'paste error:

SNIP

or (2) set a flag in getCourses() so that the query is only executed 
once, otherwise returning a result line - something like:

function getCourses($UID)
 global $link;
 static $result = false;

 if (!$result)
  {
$result = mysql_query( SELECT C.CourseName FROM tblcourses C, 
tblusers U,
 tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND U.ID =
 $UID, $link );
if ( ! $result )
 die ( getRow fatal error: .mysql_error() );
  }
 {
 return mysql_fetch_array( $result );
 }

...

while ($Row = getCourses($session[id]) as $value)
 {
 print br, $Row['C.CourseName'];
 }

SNIP

This last block of code should be

while ($Row = getCourses($session[id]))
 {
 print br, $Row['C.CourseName'];
 }

-steve
-- 
++
| Steve Edberg  [EMAIL PROTECTED] |
| University of California, Davis  (530)754-9127 |
| Programming/Database/SysAdmin   http://pgfsun.ucdavis.edu/ |
++
| The end to politics as usual:  |
| The Monster Raving Loony Party (http://www.omrlp.com/) |
++

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] mail function

2002-06-29 Thread CrossWalkCentral

When using this fucntion listed bellow with the HTML headder

the email sent does not show the FROM in the FORM filed it just displays it
in the email message



Any one have any ideas.



$headers .= From: CrossWalkCentral [EMAIL PROTECTED] \n;




-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] Re: [PHP] Usiing FOREACH to loop through Array

2002-06-29 Thread Brad Melendy

Steve,
Thanks very much.  This make total sense to me.  Now, I was pretty sure that
I was getting an array back from mysql_fetch_array because when I do a
'print $myCourses' without specifying any value, I just get 'array' which I
believe I am supposed to, IF it is truly an array.  Anyway, I'm going to
revisit the mysql_fetch_array function right now.  Thanks for your help, it
is greatly appreciated.

...Brad

Steve Edberg [EMAIL PROTECTED] wrote in message
news:p05100300b943f2a7feef@[168.150.239.37]...
 At 3:27 PM -0700 6/29/02, Brad Melendy wrote:
 Hi All,
 I've stumped myself here.  In a nutshell, I have a function that returns
my
 array based on a SQL query and here's the code:
 
 -begin code---
 function getCourses($UID)
   {
   global $link;
   $result = mysql_query( SELECT C.CourseName FROM tblcourses C, tblusers
U,
 tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND U.ID =
 $UID, $link );
   if ( ! $result )
die ( getRow fatal error: .mysql_error() );
   return mysql_fetch_array( $result );
   }
 end code 
 
 I call this from a PHP page with the following code:
 
 begin code--
 $myCourses = getCourses($session[id]);
 foreach ($myCourses as $value)
   {
   print br$value;
   }
 end code---
 
 Now, when I test the SQL from my function directly on the database, it
 returns just want I want it to return but it isn't working that way on my
 PHP page. For results where there is a single entry, I am getting the
same
 entry TWICE and for records with more than a single entry I am getting
ONLY
 the FIRST entry TWICE.
 
 Now I know my SQL code is correct (I am testing it against a MySQL
database
 using MySQL-Front) so I suspect I'm doing something stupid in my foreach
 loop.


 I think your problem lies in a misunderstanding of the
 mysql_fetch_array() function. It doesn't return the entire result set
 in an array - just one record at a time. You can fix this in one of
 two ways:

 (1) Loop though the entire result set in your function:

 function getCourses($UID)
  {
   global $link;

   $ResultSet = array();

   $result = mysql_query( SELECT C.CourseName FROM tblcourses C,
tblusers U,
tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND
U.ID =
$UID, $link );

   if ( ! $result )
die ( getRow fatal error: .mysql_error() );

   while ($Row = mysql_fetch_array( $result ))
   {
$ResultSet[] = $Row['C.CourseName'];
   }

   return $ResultSet;
  }

 ...

 $myCourses = getCourses($session[id]);
 foreach ($myCourses as $value)
  {
  print br$value;
  }

 or (2) set a flag in getCourses() so that the query is only executed
 once, otherwise returning a result line - something like:

 function getCourses($UID)
  global $link;
  static $result = false;

  if (!$result)
   {
 $result = mysql_query( SELECT C.CourseName FROM tblcourses C,
 tblusers U,
  tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND
U.ID =
  $UID, $link );
 if ( ! $result )
  die ( getRow fatal error: .mysql_error() );
   }
  {
  return mysql_fetch_array( $result );
  }

 ...

 while ($Row = getCourses($session[id]) as $value)
  {
  print br, $Row['C.CourseName'];
  }

 (standard caveats about off-top-of-head, untested code apply)

 The reason you are getting the first record TWICE is becaouse of the
 default behaviour of the mysql_fetch_array() function. It returns
 both an associative array - ie, elements of the form field-name =
 value - and a numerically indexed array (0, 1, 2, etc.). You can
 alter this behaviour by the second parameter of the function: see

 http://www.php.net/manual/en/function.mysql-fetch-array.php

 -steve


 I'm hoping someone will spot my dumb mistake.  Thanks very much for any
help
 at all on this.
 
 Brad
 
 
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php


 --
 ++
 | Steve Edberg  [EMAIL PROTECTED] |
 | University of California, Davis  (530)754-9127 |
 | Programming/Database/SysAdmin   http://pgfsun.ucdavis.edu/ |
 ++
 | The end to politics as usual:  |
 | The Monster Raving Loony Party (http://www.omrlp.com/) |
 ++



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DB] Re: [PHP] Usiing FOREACH to loop through Array

2002-06-29 Thread Brad Melendy

Steve,
Thanks a ton!  I'm now running slick as a whistle using your option #1
(looping through the results in the function) and using the newer
mysql_fetch_assoc() function.  I have a much better understanding of this
now and I really appreciate your help.  Hopefully I'll get a chance to help
someone else next time.  :-)

Steve Edberg [EMAIL PROTECTED] wrote in message
news:p05100300b943f2a7feef@[168.150.239.37]...
 At 3:27 PM -0700 6/29/02, Brad Melendy wrote:
 Hi All,
 I've stumped myself here.  In a nutshell, I have a function that returns
my
 array based on a SQL query and here's the code:
 
 -begin code---
 function getCourses($UID)
   {
   global $link;
   $result = mysql_query( SELECT C.CourseName FROM tblcourses C, tblusers
U,
 tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND U.ID =
 $UID, $link );
   if ( ! $result )
die ( getRow fatal error: .mysql_error() );
   return mysql_fetch_array( $result );
   }
 end code 
 
 I call this from a PHP page with the following code:
 
 begin code--
 $myCourses = getCourses($session[id]);
 foreach ($myCourses as $value)
   {
   print br$value;
   }
 end code---
 
 Now, when I test the SQL from my function directly on the database, it
 returns just want I want it to return but it isn't working that way on my
 PHP page. For results where there is a single entry, I am getting the
same
 entry TWICE and for records with more than a single entry I am getting
ONLY
 the FIRST entry TWICE.
 
 Now I know my SQL code is correct (I am testing it against a MySQL
database
 using MySQL-Front) so I suspect I'm doing something stupid in my foreach
 loop.


 I think your problem lies in a misunderstanding of the
 mysql_fetch_array() function. It doesn't return the entire result set
 in an array - just one record at a time. You can fix this in one of
 two ways:

 (1) Loop though the entire result set in your function:

 function getCourses($UID)
  {
   global $link;

   $ResultSet = array();

   $result = mysql_query( SELECT C.CourseName FROM tblcourses C,
tblusers U,
tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND
U.ID =
$UID, $link );

   if ( ! $result )
die ( getRow fatal error: .mysql_error() );

   while ($Row = mysql_fetch_array( $result ))
   {
$ResultSet[] = $Row['C.CourseName'];
   }

   return $ResultSet;
  }

 ...

 $myCourses = getCourses($session[id]);
 foreach ($myCourses as $value)
  {
  print br$value;
  }

 or (2) set a flag in getCourses() so that the query is only executed
 once, otherwise returning a result line - something like:

 function getCourses($UID)
  global $link;
  static $result = false;

  if (!$result)
   {
 $result = mysql_query( SELECT C.CourseName FROM tblcourses C,
 tblusers U,
  tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND
U.ID =
  $UID, $link );
 if ( ! $result )
  die ( getRow fatal error: .mysql_error() );
   }
  {
  return mysql_fetch_array( $result );
  }

 ...

 while ($Row = getCourses($session[id]) as $value)
  {
  print br, $Row['C.CourseName'];
  }

 (standard caveats about off-top-of-head, untested code apply)

 The reason you are getting the first record TWICE is becaouse of the
 default behaviour of the mysql_fetch_array() function. It returns
 both an associative array - ie, elements of the form field-name =
 value - and a numerically indexed array (0, 1, 2, etc.). You can
 alter this behaviour by the second parameter of the function: see

 http://www.php.net/manual/en/function.mysql-fetch-array.php

 -steve


 I'm hoping someone will spot my dumb mistake.  Thanks very much for any
help
 at all on this.
 
 Brad
 
 
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php


 --
 ++
 | Steve Edberg  [EMAIL PROTECTED] |
 | University of California, Davis  (530)754-9127 |
 | Programming/Database/SysAdmin   http://pgfsun.ucdavis.edu/ |
 ++
 | The end to politics as usual:  |
 | The Monster Raving Loony Party (http://www.omrlp.com/) |
 ++



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php