Solved

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

Posted on 2014-12-07
2
91 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add a date range function to PDF Generation 9 41
XML loaded in a form with dropdown 6 54
Conditional Array Element 7 28
sitemap.xml 3 29
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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

734 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