I noticed there was a more relevant section for me to post so adding my question here.

I'm parsing an HTML page and I receive the correct data I want inside $numbers (code below) but when I go to insert into my sql table I'm not sure I'm going about it correctly. Maybe I'm saving the data incorrectly. I have a 50 rows in my table and 50 entries in $numbers I want to transfer. Any thoughts on my code?

$html = file_get_html($url);

foreach($html->find('span[id*=numbers]') as $key => $info) 
{ echo ($key + 1).'. '.$info->plaintext."<br />\n";
$numbers[$key+1]=$info; } 

$sql="INSERT INTO numbersdata VALUES ('$numbers')";
$result = mysql_query($sql); 
//I'm connected to my DB already

    Echo $sql and you'll find it simply has the word "Array" inside the VALUES clause. That's because you can't insert an array directly into a string (since it doesn't make sense to try to do such a thing). In addition, a single tuple in the VALUES clause means you'll be INSERT'ing a single row. If you wanted multiple rows, you would need multiple tuples.

    Either loop through the array and manually build up the query, or use [man]implode/man to join the elements of the array together to form a string.

      Isn't $numbers an array? You can't stick an array in the database unless it's serialized ... but that's not what you want to do here ... shouldn't you loop through $numbers and either build a large query or perform 50 separate inserts?

        Thank you..I know how I should do this now. Haven't had a chance to work on it just yet but shouldn't be an issue. Thanks.

          Write a Reply...