Solved

Need assistance with PHP PDO SELECT inside a WHILE

Posted on 2015-02-18
3
80 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
  • 2
3 Comments
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 250 total points
Comment Utility
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 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 250 total points
Comment Utility
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 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 250 total points
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now