Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need assistance with PHP PDO SELECT inside a WHILE

Posted on 2015-02-18
3
Medium Priority
?
88 Views
Last Modified: 2015-02-19
I need assistance on how to set up a SELECT within a WHILE.  How do I do this

Variable from import is $data[0].   (SKU)

Another table has Prohibited_SKU.

Desired result:

IF prohibited_sku === $data[0]  then $googleprohibited="1"

ELSE

googleprohibited="0"


$filename="http://website.com/feeds/productdownload.php?id=MPB&version=2&format=csv&fields=sku&time= . $time . ";



//open the file
$handle = fopen($filename, "r");
//this is what causes it to skip
fgetcsv($handle,",");



//begin looping through the lines
while (($data = fgetcsv($handle, ",")) !== FALSE)
{


// loop all column values and escape special characters
 foreach ($data as $key => $value){
  $data[$key] = $value;
}
//echo $data[0];  

$query = $conn->query('SELECT DISTINCT `prohibited_sku` FROM `Google_Prohibited_Skus` WHERE `prohibited_sku` === $sku');
while ($row100 = $query->fetch(PDO::FETCH_ASSOC))
{
if ($row100[prohibited_sku] === $sku) {
$googleprohibited="1";
} else {
//$googleprohibited="0";
}
}
echo $googleprohibited."&nbsp;<BR>";

//INSERT HERE

Open in new window

0
Comment
Question by:lawrence_dev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 1000 total points
ID: 40618262
You have to assign $data[0] to $sku variable before to use it in your query:
// loop all column values and escape special characters
 foreach ($data as $key => $value){
  $data[$key] = $value;
}
//echo $data[0];  
$sku = $data[0];

$query = $conn->query('SELECT DISTINCT `prohibited_sku` FROM `Google_Prohibited_Skus` WHERE `prohibited_sku` === $sku');

Open in new window


Cheers
0
 
LVL 44

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 1000 total points
ID: 40618626
Your logic doesn't make sense. Look at your Query - it will only ever return a maximum of 1 record with 1 column and the value of that column will be the same as the value in $data[0].

You then try to loop through this 1 record to see if it matches $data[0] - if there is a record returned then it will always match $data[0] and set $googleprohibited="1".

If there's no record (i.e. no match) then there's nothing to loop through so $googleprohibited will never get set.
0
 
LVL 44

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 1000 total points
ID: 40618666
Have a look at this code. You're using PDO, so make use of the prepared statements:

// prepare your SQL statement
$stmt = $conn->prepare("SELECT DISTINCT prohibited_sku FROM Google_Prohibited_Skus WHERE prohibited_sku = ?");

// loop through your data file
while (($data = fgetcsv($handle, ",")) !== FALSE) {
	// execute the prepared SQL statement, passing in $data[0]
	$stmt->execute( array($data[0]) );
	
	// if there is a returned value, set googleprohibited to 1, otherwise set it to 0
	$googleprohibited = ( $stmt->fetchColumn() !== false ) ? 1 : 0;
	
	// output the result
	echo $googleprohibited . PHP_EOL;
}

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question