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)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.