Solved

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

Posted on 2014-12-18
5
125 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
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 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 167 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 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checking CSRF token within a function 36 74
Dynamic varibles 5 32
Google Maps API and PHP 25 27
PHP Query return divisible by 3 3 15
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to count occurrences of each item in an array.
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.

806 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