0

I have the following code that is supposed to call a function in a loop, and then pass the argument to it and put the selected items in a database. I don't think I am passing the correct arguments through to the function though, so can you have a look?

<?php

function welcome($grill){
$link = mysql_connect('localhost', 'sc2brsting', '1A2');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

mysql_select_db('sc2bring1', $link);

$sql = "INSERT INTO `sc2broad_tesing1`.`Persons` (`re`) VALUES ($grill)";

mysql_query($sql);

mysql_close($link);
}

?>

<?php

$grass=0;
while($grass<500){
$file = $DOCUMENT_ROOT . "website.com";
$doc  = new DOMDocument();
@$doc->loadHTMLFile($file);


$elements = $doc->getElementsByTagName('a');

for ($i=106; $i<=204; $i=$i+2)
  {
  $grill = $elements->item($i)->nodeValue . " ";
welcome($grill);

  }
$grass=$grass+24;
}

?>

the problem im having is that the variable $grill isn't passing through the function

4
  • 1
    what the problem you are facing Commented Jan 3, 2012 at 5:54
  • could you describe what is the value of $grill you are getting in welcome function, also check your code $elements->item($i)->nodeValue what the value of $grill you are getting by this syntex Commented Jan 3, 2012 at 6:07
  • did you try echo'ing $grill..? Commented Jan 3, 2012 at 6:11
  • when i echo grill, the values come up as they are suppose too Commented Jan 3, 2012 at 6:30

1 Answer 1

1

You are not escaping the variable $grill when inserting it into to database.

This will cause an MySQL error, hence the impression the argument is not passed to the function.

The line should look like this:

$sql = "INSERT INTO `sc2broad_tesing1`.`Persons` (`re`) VALUES ('".$grill."')";
Sign up to request clarification or add additional context in comments.

3 Comments

just as a note: you don't have to use '". $grill ."' you can just do $sql = "INSERT INTO sc2broad_tesing1.Persons (re) VALUES ('$grill')";
That's what I wrote :-) @Jakub edited it, but that's OK as it's good practice.
sorry, yeah I edited, I used it that way due to better color highlight in my editor (always looks cleaner to me too), I realize its overkill.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.