Solved

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

Posted on 2014-12-07
2
92 Views
Last Modified: 2014-12-07
How do I transition this select statement to PDO with PHP?

$result1 = mysql_query("SELECT * FROM `product_description`");
while($row1 = mysql_fetch_assoc($result1))
{


mysql_query("INSERT INTO `product_description` (`product_id`, `name`, `description`, short_description) VALUES ('".mysql_real_escape_string($row1['product_id'])."','".mysql_real_escape_string($row1['name'])."','".mysql_real_escape_string($row1['description'])."','".mysql_real_escape_string($row1['short_desc'])."')");


}

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 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40485785
<?php
$database_name = "";
$username = "";
$password = "";

$conn = new PDO('mysql:host=localhost;dbname='.$database_name, $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Get rows
$result = $conn->query("SELECT * FROM `product_description`");

// Prepare the update sql
$do_update = $conn->prepare("INSERT INTO `product_description` (`product_id`, `name`, `description`, `short_description`)
VALUES (:product_id, :name, :description, :short_desc)");

while ($row = $result->fetch(PDO::FETCH_ASSOC)){
{

	// Execute the update for each row
	$do_update->execute(array(
		':product_id'	=>	$row['product_id'],
		':name'		=>	$row['name'],
		':description'	=>	$row['description'],
		':short_desc'	=>	$row['short_desc']
	));
}

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40485869
Here's an article covering most of the aspects of the conversion.
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

In the case of the SELECT statement, there are no changes needed at all.  Things only change when you use external variables as part of the query.  The article explains why and shows a few ways of making the transition from direct variable injection (into the query string) and indirect injection via parameterized queries or placeholders.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
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 …

630 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