TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-11-2010, 09:20 AM   #1 (permalink)
The Visitor
 
Join Date: Nov 2010
Posts: 2
Thanks: 1
gj384 is on a distinguished road
Default Filling an array with values from a single column over specific rows in MySQL DB

Hi there,

I'm trying to populate a single-column array with values from one column in MySQL, but only from certain rows in which a specific other column matches a certain string. This is my function:

PHP Code:
function getProductList($field,$term){
      
$q "SELECT PartNo FROM ".TBL_PRODUCTS." WHERE ".$field." = '".$term."'";
      
$result mysql_query($q$this->connection);
      
$dbarray mysql_fetch_array($result);
      return 
$dbarray;
   } 
"$field" is the name of the column I'm searching, and "$term" is the actual string I'm searching for in that column. "PartNo" is the name of the column from which I want to draw the results, which I then want placed into the array.

This is how I'm calling to get the results of that function:

PHP Code:
$compatibleProductsArray=$database->getProductList("Cat",$productInfo[2]); 
In the code above, the name of the column I'm searching is "Cat", and the string I'm searching for is stored in "$productInfo[2]".

At the moment, I'm doing a print_r of the array to show the contents, like so:

PHP Code:
print_r($compatibleProductsArray); 
Which gives me the following output:

Array ( [0] => T7S25T [PartNo] => T7S25T )

But I'm expecting many more rows than that... What am I doing wrong?

Incidentally, after I work out how to fill the array, I'm then planning to convert the array into a CSV string. Is there an easy way to do that, or does knowing that final goal make it any easier to work out a solution to all of the above?

Thanks a million, in advance...
gj384 is offline  
Reply With Quote
Old 11-11-2010, 02:27 PM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

mysql_fetch_array only returns the first row of the result and then moves the internal data pointer ahead. The array has 2 entries because 1 is for the numeric indexed array and the other one is for the associative array. what you can do is replace this:
php Code:
$dbarray = mysql_fetch_array($result);
to this
php Code:
$dbarray = array();
while($row = mysql_fetch_array($result)) {
    $dbarray[]=$row['PartNo'];
}
If the results are kinda big, it is better if you use mysql_fetch_row or mysql_fetch_assoc since mysql_fetch_array double the entries for numeric and associative.
From the manual:
mysql_fetch_row() - Get a result row as an enumerated array
mysql_fetch_array() - Fetch a result row as an associative array, a numeric array, or both
mysql_fetch_assoc() - Fetch a result row as an associative array
mysql_fetch_object() - Fetch a result row as an object
tony is offline  
Reply With Quote
The Following User Says Thank You to tony For This Useful Post:
gj384 (11-11-2010)
Old 11-11-2010, 09:45 PM   #3 (permalink)
The Visitor
 
Join Date: Nov 2010
Posts: 2
Thanks: 1
gj384 is on a distinguished road
Default

Wow! So straightforward. Thanks so much! Works like a dream.
gj384 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
feedback on my class please frostyboy33 Advanced PHP Programming 7 10-22-2012 09:12 AM
Return array values smrtalex Absolute Beginners 14 04-25-2009 11:00 PM
Array mess Killswitch Absolute Beginners 4 12-14-2008 07:35 AM
Filling an array with an array benton General 5 08-05-2008 02:54 AM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 11:30 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design