Solved

Need assistance with PHP PDO SELECT inside a WHILE

Posted on 2015-02-18
3
84 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 250 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 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 250 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 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 250 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

751 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