Note that there are some explanatory texts on larger screens.

plurals
  1. POTransferring DB data between PHP and Javascript 2D array
    text
    copied!<p>I am attempting to get all the data from a MySQL db with PHP, initialise a 2D java array and populate it with the PHP data.</p> <p>I am having trouble embedding JS in the PHP. I have marked up what is working and what isn't.</p> <p>As you will see, some of the embedded java works but not all.</p> <p>Any thoughts?</p> <pre><code>&lt;body&gt; &lt;?php $con = mysql_connect("XXXXXX.COM","guest","password"); mysql_select_db("HHG", $con); if (!$con) { die('Could not connect: ' . mysql_error()); } else { $result = mysql_query("SELECT * FROM articles", $con); $numrows = mysql_num_rows($result); echo "DB connection OK &lt;br/&gt;"; echo "Found "; echo $numrows; echo " records &lt;br/&gt;&lt;br/&gt;"; } // EVERYTHING WORKS UP TO HERE ?&gt; &lt;script type="text/javascript"&gt; document.write("THIS IS THE FISRT JS DOING SOMETHING"); // THIS DOES NOTHING numrows = &lt;?php echo $numrows; ?&gt;; // THIS DOES NOTHING string [][] hhgdata = new string[numrows][4]; // THIS DOES NOTHING document.write("Records = " + numrows + "&lt;br/&gt;"); // THIS DOES NOTHING &lt;/script&gt; &lt;? $counter = 1; while ($row = mysql_fetch_assoc($result)) { echo $row["idimg"]; echo "&lt;br/&gt;"; //THIS WORKS $hhgtitle = $row["hhgtitle"]; //THIS WORKS echo $hhgtitle; echo "&lt;br/&gt;"; //THIS WORKS ?&gt; &lt;script type="text/javascript"&gt; //THIS WORKS counter = &lt;?php echo $counter; ?&gt;; //THIS WORKS document.write("counter = " + counter + "&lt;br/&gt;&lt;br/&gt;"); //THIS WORKS hhgtitle = &lt;?php echo $hhgtitle; ?&gt;; // THIS DOES NOTHING document.write("Title: "); // THIS DOES NOTHING hhgdata[counter][1]= hhgtitle; // THIS DOES NOTHING document.write(hhgdata[counter][1]); // THIS DOES NOTHING &lt;/script&gt; &lt;? $counter++; // THIS WORKS } ?&gt; &lt;/body&gt; </code></pre>
 

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