Solved

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

Posted on 2014-12-18
5
138 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 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 110

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

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
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.
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.

707 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