Link to home
Create AccountLog in
Avatar of rlb1
rlb1

asked on

How do I get all results to show?

Experts,

I appreciate your assistance...  I have two problems with the script.
1.)  There are 21 categories and 21 cat-ID's.  I can only make 1 set of results show up.  I have tried various scripts like "  i++ ", etc, and cannot get more than one row of data.  How do I make multiple rows echo on the screen (and into the database)?

2.)  The database connects fine, and the values  (  $category_id   &  $category)  echo fine.   What is wrong with my insert statement?  Did I structure something incorrectly?

Thanks for your help!!

Randy
<?

$data=file_get_contents('http://www.mywebsite.com');



//Connect to DB   ***********************************************************
mysql_connect('$hostname,'$username','$password') or die("Unable to connect to SQL server");
mysql_select_db('$database') or die("Unable to SELECT DB");

echo "Connected to DB";
echo "<BR>";
//$data=file_get_contents('http://www.cablestogo.com/product_list.asp?cat_id=2030&show=all');




//Keywords
preg_match('%<meta name="keywords" content="(.*?)" />%s',$data,$matches1);

$keywords=$matches1[1];
echo $keywords."<br />";


echo "<br /><br /><br />";






//Description

preg_match_all('%<a[^href=]*href=\"\/categories\.asp\?cat_id=(.*?)\"[^>]*>(.*?)<\/a>%',$data,$matches2,PREG_SET_ORDER);



$category_id=$matches2[0][1];
$category=$matches2[0][2];

mysql_query("INSERT INTO`testcategory`(`cat_id`, `cat_name`) VALUES (`$category_id`,`$category`)");   

//DB connects fine, just no results appearing in database...


echo $category_id;   // echo works fine
echo $category;        
	



$msg1="test category Upload Completed";
echo $msg1;
echo "<BR>";




echo $category_id."<br />";
echo $category."<br />";



?>

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Where to start?  This line is almost certainly wrong.  You need double quotes, right?
mysql_connect('$hostname,'$username','$password') or die("Unable to connect to SQL server");

SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
This line might have worked?  But how would we know?

mysql_query("INSERT INTO`testcategory`(`cat_id`, `cat_name`) VALUES (`$category_id`,`$category`)");  

A better strategy is shown in the code snippet.  It will show you if any errors occurred.
$sql = "INSERT INTO `testcategory` ( `cat_id`, `cat_name` ) VALUES ( `$category_id`, `$category` )";   
$res = mysql_query($sql);
if ($res === FALSE)
{
    echo "<pre>";
    echo PHP_EOL . 'FAIL: ';
    echo PHP_EOL . $sql;
    echo PHP_EOL . mysql_errno() . ' ' . mysql_error();
}

Open in new window

This line might need some attention.

preg_match('%<meta name="keywords" content="(.*?)" />%s',$data,$matches1);

I would call it somewhat brittle.  It will work for <meta name="keywords" but it will fail for <meta  name="keywords" which is a perfectly legitimate variant.

Having seen a few of your questions here at EE, I would like to recommend a really excellent book to you.  It's now in its fourth printing and has been a permanent part of my professional library since Version One.  Great examples, very readable with a downloadable code library that you can copy from.
http://www.sitepoint.com/books/phpmysql4/

Best of luck with the project, ~Ray
Avatar of rlb1
rlb1

ASKER

cyberkiwi:  Thank You very much.  I appreicate your help!  I know enough to be dangerous, but not near enough to be deadly...  I am trying to learn this as fast as I can!!

Ray: I know I drive you absolutely crazy, but I appreicate you always being here to assist.  I am trying!!!  (and I will buy that book sooner than later.)

Ray, what is the difference in these two...
"It will work for <meta name="keywords" but it will fail for <meta  name="keywords" which is a perfectly legitimate variant."
The difference is that one of them has a single space and one has a double space.  It could just as well be a tab, or an end-of-line character or any whitespace.  The point is that there are safe ways of testing for these things, and a regular expression that only tolerates exactly one blank is not very well thought out.

PHP is a rich language with lots of built-in functions to handle the kinds of things you're wrestling with right now.  You do not need to waste time debugging REGEX code when you want to get information from a URL -- it's been done before, trust me.  See the code snippet for an example of the ways to do it (both right and wrong).

If you leave your questions open a little longer you will almost always get a few different ways of looking at the problem and solutions.

Best regards (and buy that book), ~Ray
<?php // RAY_parse_str_parse_url.php
error_reporting(E_ALL);
echo "<pre>";



// DEMONSTRATE HOW TO LOOK AT A URL AND PULL OUT ONE OF THE 'GET' ARGUMENTS
// MAN PAGE http://us3.php.net/manual/en/function.parse-url.php
// MAN PAGE http://us3.php.net/manual/en/function.parse-str.php



// TEST DATA - WE WANT TO FIND THE GET["v"] ARGUMENT
$url = 'http://www.youtube.com/watch?v=_PP5JuqXPqA&url=MUNGDATA';



// DOING IT WRONG: INJECT A VARIABLE WITH THE GET ARGUMENT FOR "v="
parse_str(parse_url($url, PHP_URL_QUERY));
echo PHP_EOL . $v;

// BUT - GAACK! - LOOK AT THE OTHER INJECTED VARIABLE
echo PHP_EOL . $url;



// RESET - SHOW HOW TO DO IT WITHOUT INJECTING UNKNOWN VARIABLES INTO THE SCOPE
$url = 'http://www.youtube.com/watch?v=_PP5JuqXPqA&num=42&spirit=willing&flesh=weak&url=MUNGDATA';

// CAPTURE THE INJECTED VARIABLES IN AN ARRAY
parse_str(parse_url($url, PHP_URL_QUERY), $arr);
echo PHP_EOL . $arr["v"];

// THIS TIME, THERE IS NO DAMAGE DONE!
echo PHP_EOL . $url;

// BECAUSE WE TRAPPED ALL THE INJECTED VARS IN THE ARRAY
echo PHP_EOL;
print_r($arr);

Open in new window