Solved

Transitioning to PDO/MySQL.  Need help  with SELECT statement.

Posted on 2014-12-17
8
97 Views
Last Modified: 2014-12-19
How do I make this script work with PDO?

$conn = new PDO("mysql:host=$host;dbname=$db;charset=$charset", $user, $pass);

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$conn->$sql = "SELECT * FROM `manufacturers` WHERE name = '$data[8]'";
foreach($pdo->query($sql) as $row1){

$statement = $conn->prepare("INSERT INTO manufacturers_sku (sku,manufacturer)
    VALUES(:sku, :manufacturer)");
$statement->execute(array(
    "sku" => $row1['sku'];
    "manufacturer" => $row1['sku'];
));
}

Open in new window

0
Comment
Question by:lawrence_dev
  • 3
  • 3
  • 2
8 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40505672
$conn = new PDO("mysql:host=$host;dbname=$db;charset=$charset", $user, $pass);

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$results = $conn->prepare("SELECT * FROM `manufacturers` WHERE name = :data")
$results->execute(array(
	":data" => $data[8]
));
$rows = $results->fetchAll(); // Just one way to do it.


$myinsert = $conn->prepare("INSERT INTO manufacturers_sku (sku,manufacturer)
	VALUES(:sku, :manufacturer)");

foreach($rows as $row){
	$myinsert->execute(array(
		":sku" => $row['sku'],
		":manufacturer" => $row['sku'] // Shouldn't this be something else?
	));
}

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40506005
A complete explanation of the transition process, including code examples, is available here.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Comment

by:lawrence_dev
ID: 40506210
OK got an error...

Parse error: syntax error, unexpected '$results' (T_VARIABLE)

$results->execute(array(
      ":data" => $data[8]      //ERROR THIS LINE
));

How do I fix this error?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 58

Expert Comment

by:Gary
ID: 40506215
Theres a missing semi colon after this line

$results = $conn->prepare("SELECT * FROM `manufacturers` WHERE name = :data"); // add the semi colon like I have here

Open in new window

0
 

Author Comment

by:lawrence_dev
ID: 40506246
Thank You!
0
 
LVL 58

Expert Comment

by:Gary
ID: 40506327
Why accept a comment that never answered the question or corrected your code, if you want a PDO tutorial there are far better places to go than some generic here's a one for all blah blah
0
 

Author Comment

by:lawrence_dev
ID: 40507921
I agree Gary!  I will take that into consideration next time.  I just wanted to reward the time and  effort to help.  You did get 450 points! The comments that just say "read this" and provide a link don't really help anyone.  If I could understand all that overly technical stuff with very little explanation,  I would not be paying to be on EE!!!  Thanks for helping me with my code!  Much appreciated!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40508429
There are some things about PHP which are kind of "sine-qua-non."  For example we have to assume that anyone asking about conversion to PDO and specifically asking about a SELECT query statement would understand that a PHP  "parse error" containing the word "unexpected" indicates an up-stream error in the script.  But if the real issue was a PHP parse error, why not just tell us that is your problem?  I'd have gladly helped you correct a parse error, too.  I just thought there was a PDO-related issue (a very common thing today, since MySQL is going to be killed off soon).

@Gary: If you have something to offer the E-E community that is better than my article, please post it - perhaps write your own article or add your comments to show what my article is missing.  I have a great appreciation of constructive criticism and welcome anything you want to add to the conversation!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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 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…

778 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