Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need assistance with PHP PDO SELECT inside a WHILE

Posted on 2015-02-18
3
Medium Priority
?
89 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses

886 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