Solved

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

Posted on 2014-12-18
5
117 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
5 Comments
 
LVL 14

Accepted Solution

by:
quizwedge earned 167 total points
Comment Utility
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
Comment Utility
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 82

Assisted Solution

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

Expert Comment

by:quizwedge
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now