?
Solved

Transitioning to PDO/MySQL.  Need help with importing file from URL

Posted on 2014-12-18
5
Medium Priority
?
141 Views
Last Modified: 2014-12-19
How do I make this file compatible with PDO.  I am getting the following error...

Fatal error: Cannot access empty property (on this line - $conn->$filename="http://website.com/feeds/productdownload.php?id=MPB_MzY2MDc3MzY2MDc3MzQw&version=2&format=csv&fields=sku";)






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

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

$conn->$filename="http://website.com/feeds/productdownload.php?id=MPB_MzY2MDc3MzY2MDc3MzQw&version=2&format=csv&fields=sku";



//rebs,rebe,spec,os&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] = mysql_real_escape_string($value);
}

echo ".$data[0].";


}

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
5 Comments
 
LVL 14

Accepted Solution

by:
quizwedge earned 668 total points
ID: 40507634
I might be missing something, but why are you doing $conn->$filename= instead of just $filename=? Doesn't look like you're trying to use $conn->$filename anywhere else.

Also, for fopen, do you have allow_url_fopen set to true? http://php.net/manual/en/filesystem.configuration.php
0
 

Author Comment

by:lawrence_dev
ID: 40507713
quizwedge, Thanks!  I attempted to manipulate the code several different ways to make it work.  I viewed the page your posted above and cannot make heads or tails of it.    If someone could show me how to do it, that would be great.
0
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 668 total points
ID: 40507977
@quizwedge is right, it should be just $filename without $conn-> in front of it.
0
 
LVL 14

Expert Comment

by:quizwedge
ID: 40508753
Try running
phpinfo();

Open in new window


That should print to the screen a bunch of information, part of which should be allow_url_fopen. In order to use fopen with a URL, that should be on. Assuming it's on, you should be able to do something similar to

$conn = new PDO("mysql:host=$host;dbname=$db;charset=$charset", $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$filename="http://website.com/feeds/productdownload.php?id=MPB_MzY2MDc3MzY2MDc3MzQw&version=2&format=csv&fields=sku";

//open the file
$handle = fopen($filename, "r");

Open in new window


Hope that helps.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 664 total points
ID: 40508867
This line of code instantiates a new PDO object and assigns a pointer in $conn that points to the new object.

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

To understand what a PDO object is and does, you might want to read the PHP.net manual about PDO.  There you will find the methods and properties you can expect to find in a PDO object.
http://php.net/manual/en/book.pdo.php

To read a file from an external URL, you can often use file_get_contents().  To read the file as an array, separated on the line-endings you can use file().  To read the file incrementally as a set of numerically indexed arrays suitable for importing into a "quick-hack" database you can use fopen() and fgetcsv().

This is probably something we have all done before - read a flat file and put some of the data into a database table.  If you want to post the CREATE TABLE statement and a small sample of your test data set, we can easily show you a tested and working example, complete with explanatory comments.
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.
Suggested Courses

764 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