Jump to content

combining two parts of a script - how to invoke a variable between them


dilbertone

Recommended Posts

hello everybody!

 

This is just basic script, where i try to modify for the needs. I try to play with it. i want to parse some data. The whole script has  three parts:

 

1. Fetching

2. parsing

3. storing

 

i want to put all into one script: Two are allready put together - there all seems to be clear...

So  this thread is one that asks for  the combining two parts of a script - how to invoke a variable between them

 

What has happened untill now:

1st i need to have a connection to database lets say MySQL. I will suggest to use mysqli instead of mysql.

Well - okay i safe this db.php

 

 

 

$host="localhost"; //database hostname
$username="******"; //database username
$password="******"; //database password
$database="******"; //database name
?>

 

Now i am going to take a new script and  save this config.php

<?php
require_once('db.php'); //call db.php
$connect=mysqli_connect($host,$username,$password); //connect to mysql through mysqli
if(!$connect){
die("Cannot connect to host, please try later."); //throw error if any problem
}
else
{
$select_db=mysqli_select_db($database); //select database
if(!$select_db){
die("Site Database is down at the moment, Please check later. We will be back shortly."); // error if cannot connect to database or db does not exist
}
}
?>

 

 

Now i have to take care for the script, that takes the files (note this is very basic - it is only a proof of concept.

In the real situation i will take cURL since cURL is much much nicer and more elegant and faster etc.

 

 

<?php
require_once('config.php'); // call config.php for db connection
$content = file_get_contents("<-here the path to the file goes in-> Position XY! an URL is here ");

var_dump($content);

$pattern = '/<td>(.*?)<\/td>/si';
preg_match_all($pattern,$content,$matches);

foreach ($matches[1] as $match) {
    $match = strip_tags($match);
    $match = trim($match);
    var_dump($match);
$sql = mysqli_query("insert into tablename(contents) values ('$match')");
}

?> 

 

 

Note: This is just basic script, where you can modify it for your taste and can play with it.

 

Question:  If i have stored the URLs that i want to parse in a local file - how do i "call" them in the script.

How do i do the call to file where the URLs (there are more than 2500 URLs that have to be parsed) at the

following position: $content = file_get_contents("<-here the path to the file goes in-> Position XY! an URL is here ");

 

 

The folder with the URLs is stored in the same folder as the scripts reside!

 

Many thanks for all hints and for a starting point!

 

if i have to write more - or if you need more infos - or if i have to be more concrete, just let me know!

 

 

i love to hear from you!

db1 8)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.