Note that there are some explanatory texts on larger screens.

plurals
  1. POphp Import CSV to MySQL where CSV contains HTML Content
    primarykey
    data
    text
    <p>My method <code>$csv-&gt;getContent();</code> contains my column names and the 'data' is my individual rows to import to the database. The loop below works flawlessly when importing my various CSVs to the database. However, when I have a column in the CSV that has html content, many rows import fine but there are some that do not and I get the <code>SQLSTATE[HY093]: "Invalid parameter number: number of bound variables does not match number of tokens"</code> error.</p> <p>From my research, I am seeing that doing things the way I have shown below is supposed to properly escape characters to avoid the need for addslashes but it does not seem to make a difference. The only time this does work is if I <code>addslashes(addslashes($values));</code> but thats kind of ridiculous and it leaves my content in the database with <code>\\\</code> before the applicable characters.</p> <p>Am I missing a step here or am I just stuck. The way I see it, I should be able to "bulletproof" my content to get it into my db each and every time. </p> <pre><code>$this-&gt;db-&gt;exec("SET CHARACTER SET utf8"); $this-&gt;db-&gt;beginTransaction(); $content = $csv-&gt;getContent(); foreach($content['data'] as $key =&gt; $value) { $sql = "INSERT INTO `".$destination_table."` (`"; $sql .= implode("`, `", $content['columns']); $sql .= "`) VALUES ("; $sql .= implode(", ", array_fill(0, count($content['columns']), "?")); $sql .= ")"; $statement = $this-&gt;db-&gt;prepare($sql); $statement-&gt;execute($value); } $this-&gt;db-&gt;commit(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload