Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I send a PHP variable array to a MySQL database?
    text
    copied!<p>So I have a variable array created from scraping a plaintext data string from a webpage (using Simple HTML DOM Parser class). This variable is the formatted to make it more concise and useful. </p> <p>I now wish to export this data into a MySQL table where the table name is the webpage title (scraped separately) and the data input is an array, where each word extracted from the webpage is a separate data record. </p> <p>Here is my code (where $trimmed is a formatted variable string of data scraped from a user input webpage):</p> <pre><code>$trimmed-&gt;plaintext=trim($trimmed-&gt;plaintext); $array = (explode(" ", $trimmed-&gt;plaintext)); $printarray = print_r ($array); mysql_select_db("test", $connect) or die ('Could not find database.'); $sql = "CREATE TABLE '$title'"; $myquery = sprintf("INSERT INTO WebPage '%s' VALUES '%s'", mysql_real_escape_string($title-&gt;plaintext), mysql_real_escape_string($printarray)); $result = mysql_query($myquery); if (!$result) { $message = '&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; Invalid query: ' . mysql_error() . "\n"; $message .= '&lt;br /&gt;&lt;br /&gt; Whole query entered here: ' . $myquery; die($message); } </code></pre> <p>The error is recieve when trying this is:<br> Invalid query: 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 ''Example Domain' VALUES '1'' at line 1 </p> <p>Whole query entered here: INSERT INTO WebPage 'Example Domain' VALUES '1'</p> <p>I can provide more code if needed, and sorry in advance if I haven't explained this very well; I am quite new to this. </p> <p>Thanks in advance.</p>
 

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