Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-12-18
5
Medium Priority
?
142 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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 …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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

604 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