0

I am trying to insert this array into my database, using an implode statement. But i am struggling to find where I am going wrong: I basically am looking to convert the two functions below to return strings:

 function Titles($link) {
$str = file_get_contents($link);    
if( strlen( $str )>0 ) {    
    preg_match_all( "/\<title\>(.*)\<\/title\>/", $str, $titles );
    if (count($titles) > 1) {
        return $titles[1];   
    }
}

return '';
}

  function getMetas($link) {
$str1 = file_get_contents($link);    

if (strlen($str1)>0) {
    preg_match_all( '/<meta.*?name=("|\')description("|\').*?content=("|\')(.*?)("|\')/i', $str1, $description);
    if (count($description) > 1) {
        return $description[4];   
    }

}


 }


  $data = array();
  foreach ($links as $link) {
$output = array(
    "title"       => Titles($link), 
  "link"        => $link,
  "description" => getMetas($link),
  "keywords" => getKeywords($link) 
   );
   if (empty($output["description"])) 
   {$output["description"] = getWord($link);
  }
   $data[] = $output;
  }
  print_r($data);  

  mysql_query("INSERT INTO scan (title, url, description, keywords) VALUES ('".implode("'),('",$data)."')");

  if (!mysql_query()) {
echo "woops";
    }

   mysql_close($connect);
1
  • try implode("','",$data). You dont need to put individual values in brackets. Commented Sep 26, 2012 at 17:12

2 Answers 2

1

The problem is that $data is a multi-dimensional array, so after you implode, you still have arrays instead of strings in your query.

You can solve that by using implode earlier on $output as well:

$data[] = implode(',', $output);

Apart from that you should use mysql_real_escape_string on your original variables but as the mysql_* functions are being deprecated, you really should switch to prepared statements in mysqli or PDO.

Edit: Actually you need to modify both implodes:

 $data[] = '"' . implode('","', $output) . '"';
 ...
 mysql_query( "INSERT INTO scan (title, url, description, keywords)
    VALUES (" . implode('),(',$data) . ")" );
Sign up to request clarification or add additional context in comments.

9 Comments

and how can I delete the 'Array' parts in my multidimensional array
it doesn't have my title or description from my array
it still won't work, it keeps returning the word 'Array' instead of title or description
@Norman Berry That means your Titles and getMetas functions return arrays. You would need to fix that first.
is there a way to convert those into strings if i show you both arrays?
|
0

Try this

$fields =   array_keys($data);
    $values =   array_values($data);

    $formatedValues =   array();
    foreach($values as $val)
    {   $val    =   "'".addslashes($val)."'";
        $formatedValues[]   =   $val;
    }
      $table="scan";

$sql    =   "INSERT INTO ".$table." (";
    $sql    .=  implode(", ",$fields).") ";
    $sql    .=  "VALUES( ";
    $sql    .=  implode(", ",$formatedValues);
    $sql    .=  ")";
    mysql_query($sql) or die(mysql_error());

5 Comments

is there a way to implode the two returned arrays from the functions above into a string to use in my INSERT
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0) VALUES( 'Array')' at line 1
@Norman Berry print_r(links) to check whether you get proper results
will i have to implode the two function arrays or shall i just follow your code?
@Norman Berry the problem is with inserting array of values why don't you try to execute the insertion in foreach link itself,on behalf of assigning to array insert it to table.

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.