2

I successfully created a table in my database, using PHP. Now, I'm trying to fill it with data. When I var_dump the data I'm trying to add, it correctly renders - it's not undefined.

I don't get any errors, but there are no entries in my SQL tables. What did I do wrong? Thanks.

Database layout here:

foreach($x->channel->item as $entry) {

  if ($y < 8) {

      $con=mysqli_connect("localhost","usernameremoved",
        "passwordremoved","databasenameremoved");
      // Check connection
      if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
      mysqli_query($con,"INSERT INTO Entries (Link, Title)
      VALUES ($entry->link, $entry->title)");
      echo "Tables updated successfully.";
      mysqli_close($con); 


      $y++;

  }
}

UPDATE, for Watcher:

Parse error: syntax error, unexpected '$entry' (T_VARIABLE) in C:\xampp\htdocs\ (... ) \PHP\rss\index.php on line 60

if ($y < 8) {

  mysqli_query($con,"INSERT INTO Entries (Link, Title)
  VALUES ("$entry->link", "$entry->title")");
  echo "Tables updated successfully.";



  $y++;

}
7
  • 2
    do you really really need to connect and close to mysql each loop? that doesn't make sense Commented Sep 16, 2014 at 3:00
  • creating the result string inside the loop for a single insert will be far more efficient Commented Sep 16, 2014 at 3:01
  • Makes sure the $y < 8 condition is actually passing. You only seem to be incrementing $y if the condition already passes. Commented Sep 16, 2014 at 3:02
  • You also probably need to add quotes around the data that you're inserting. Commented Sep 16, 2014 at 3:04
  • Watcher, if I add quotes I get an error. If I use apostrophes, I still don't get any data into my tables. VALUES ('$entry->link', '$entry->title')"); Commented Sep 16, 2014 at 3:08

2 Answers 2

1

This case is pretty much what prepared statements were created for.

// Database connection
$db = new MySQLi("localhost","usernameremoved", "passwordremoved","databasenameremoved");
if ($db->error) {
    echo "Failed to connect to MySQL: ".$db->error;
}
// Prepared statement
$stmt = $db->prepare('INSERT INTO entries (Link, Title) VALUES (?, ?)');
if ($stmt === false) {
    die('Could not prepare SQL: '.$db->error);
}
// Bind variables $link and $title to prepared statement
if ( ! $stmt->bind_param('ss', $link, $title)) {
    die('Could not bind params: '.$stmt->error);
}
$y = 0;
foreach ($x->channel->item as $entry) {
    if ($y >= 8) {
        break;
    }
    // Set values on bound variables
    $link  = $entry->link;
    $title = $entry->title;

    // Execute
    if ($stmt->execute() === false) {
        die('Could not execute query: '.$stmt->error);
    }
    $y++;
}
$stmt->close();
$db->close();
Sign up to request clarification or add additional context in comments.

1 Comment

I don't have enough rep to vote you up, so I can only offer my thanks here. :)
1

Just take off that connect and close outside that loop. And as per Dagon, combine them into a multiple insert instead. Example:

$con = mysqli_connect("localhost","usernameremoved", "passwordremoved","databasenameremoved");
$stmt = 'INSERT INTO Entries (Link, Title) VALUES ';
$values = array();
$y = 0;
foreach ($x->channel->item as $entry) {
    if($y < 8) {
        $values[] = "('$entry->link', '$entry->title')";
    }
    $y++;
}
$values = implode(', ', $values);
$stmt .= $values;
mysqli_query($con, $stmt);
mysqli_close($con); 

Comments

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.