Solved

Need assistance with PHP PDO SELECT inside a WHILE

Posted on 2015-02-18
3
81 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

919 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

14 Experts available now in Live!

Get 1:1 Help Now