Jump to content

[SOLVED] Do You See a Syntax Error in this One Line of Code?


Recommended Posts

Error reads unexpected T_STRING

 

Line causing the error

 echo "Link = " . $set['link'] ", E-mail = " . $set['email'] . "<br/>";

 

 

complete code

 <?php

$links = fetch_links("http://southcoast.craigslist.org/sss/");
$sets = array();
foreach($links as $link) {
   $sets[] = fetch_email($link);
}

foreach($sets as $set) {
   $dbx= mysql_connect("localhost", "root", "");   //include before any database implematation


if (!$dbx)
{
die('Could not connect: ' . mysql_error());
}

mysql_SELECT_db("craigslist", $dbx);
mysql_Query("INSERT INTO addresses (sale_items)
VALUES ('$set');

mysql_close($dbx);


echo "Link = " . $set['link'] ", E-mail = " . $set['email'] . "<br/>";

}

function fetch_links($page_url) {
   $pattern = '#<a href="(/[a-z]{3}/\d{10}\.html)">#';
   $page = file_get_contents($page_url);
   preg_match_all($pattern, $page, $matches);
   return $matches[1];
}

function fetch_email($page_link) {
   $pattern = '#(sale-[a-z0-9]+-\d+@craigslist\.org)#';
   $page = file_get_contents("http://southcoast.craigslist.org" . $page_link);
   preg_match($pattern, $page, $out);
   return array('link'=>$page_link, 'email'=>$out[1]);
}
endforeach;
?>

the problem is here

mysql_Query("INSERT INTO addresses (sale_items)
VALUES ('$set');

 

And why is that?

 

You don't close the string or the function params.

 

mysql_Query("INSERT INTO addresses (sale_items) VALUES ('$set')");

 

After fixing this the endforeach; is unexpected everywhere i try to put :shrug:

What's block does the endforeach; at the end relate to ?

 

to help you i have formatted the code (to make it more visible)

 <?php

$links = fetch_links("http://southcoast.craigslist.org/sss/");
$sets = array();
foreach($links as $link) {
	$sets[] = fetch_email($link);
}

foreach($sets as $set) {
	$dbx= mysql_connect("localhost", "root", "");   //include before any database implematation


	if (!$dbx)
	{
		die('Could not connect: ' . mysql_error());
	}

	mysql_SELECT_db("craigslist", $dbx);
	mysql_Query("INSERT INTO addresses (sale_items) VALUES ('$set')");

	mysql_close($dbx);


	echo "Link = " . $set['link'] .", E-mail = " . $set['email'] . "<br/>";

}

function fetch_links($page_url) {
	$pattern = '#<a href="(/[a-z]{3}/\d{10}\.html)">#';
	$page = file_get_contents($page_url);
	preg_match_all($pattern, $page, $matches);
	return $matches[1];
}

function fetch_email($page_link) {
	$pattern = '#(sale-[a-z0-9]+-\d+@craigslist\.org)#';
	$page = file_get_contents("http://southcoast.craigslist.org" . $page_link);
	preg_match($pattern, $page, $out);
	return array('link'=>$page_link, 'email'=>$out[1]);
}
endforeach;
?>

What's block does the endforeach; at the end relate to ?

 

to help you i have formatted the code (to make it more visible)

 <?php

$links = fetch_links("http://southcoast.craigslist.org/sss/");
$sets = array();
foreach($links as $link) {
	$sets[] = fetch_email($link);
}

foreach($sets as $set) {
	$dbx= mysql_connect("localhost", "root", "");   //include before any database implematation


	if (!$dbx)
	{
		die('Could not connect: ' . mysql_error());
	}

	mysql_SELECT_db("craigslist", $dbx);
	mysql_Query("INSERT INTO addresses (sale_items) VALUES ('$set')");

	mysql_close($dbx);


	echo "Link = " . $set['link'] .", E-mail = " . $set['email'] . "<br/>";

}

function fetch_links($page_url) {
	$pattern = '#<a href="(/[a-z]{3}/\d{10}\.html)">#';
	$page = file_get_contents($page_url);
	preg_match_all($pattern, $page, $matches);
	return $matches[1];
}

function fetch_email($page_link) {
	$pattern = '#(sale-[a-z0-9]+-\d+@craigslist\.org)#';
	$page = file_get_contents("http://southcoast.craigslist.org" . $page_link);
	preg_match($pattern, $page, $out);
	return array('link'=>$page_link, 'email'=>$out[1]);
}
endforeach;
?>

 

It relates to

foreach($sets as $set) {

But that block ends

foreach($sets as $set) {
    $dbx= mysql_connect("localhost", "root", "");   //include before any database implematation


    if (!$dbx)
    {
       die('Could not connect: ' . mysql_error());
    }

    mysql_SELECT_db("craigslist", $dbx);
    mysql_Query("INSERT INTO addresses (sale_items) VALUES ('$set')");

    mysql_close($dbx);


    echo "Link = " . $set['link'] .", E-mail = " . $set['email'] . "<br/>";

}//<----------HERE--------

But that block ends

foreach($sets as $set) {
    $dbx= mysql_connect("localhost", "root", "");   //include before any database implematation


    if (!$dbx)
    {
       die('Could not connect: ' . mysql_error());
    }

    mysql_SELECT_db("craigslist", $dbx);
    mysql_Query("INSERT INTO addresses (sale_items) VALUES ('$set')");

    mysql_close($dbx);


    echo "Link = " . $set['link'] .", E-mail = " . $set['email'] . "<br/>";

}//<----------HERE--------

 

Well for some reason beyond my knowledge the foreach is causing the script to load forever?

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.