I am working on a website, and while coding, I came across a strange bug.
I am storing MySQL query results in an array, then call a JavaScript function with the stored data. The array originally contained 9 items: 8 with tinyint(4) type (in the database - I don't know if it could be useful), and one with int(11). Later, I realized I have to pass a String with them, too, so I included a Varchar(40) type item.
Here is the problem. Until this point, the code runs without any error - I can pass the variables, use them in the JS code and display them on the site. As soon as I include the string, the site crashes (nothing is displayed).
Here is the code:
<?php
// Create connection
$con=mysqli_connect("mysql7.000webhost.com","a2248489_admin","SQL123DevAdmin","a2248489_players");
$con2=mysqli_connect("mysql7.000webhost.com","a2248489_admin","SQL123DevAdmin","a2248489_players");
// Check connection
$troopArray = array(array());
$result = mysqli_query($con,"SELECT ID, Name FROM Player");
$counter = 0;
while($row = mysqli_fetch_array($result)) {
$troops = mysqli_query($con,"SELECT * FROM Troops WHERE ID = '" . $row['ID'] . "' ");
while($sor = mysqli_fetch_array($troops)) {
$troopArray[$counter][0] = $sor['archerLvl'];
$troopArray[$counter][1] = $sor['giantLvl'];
$troopArray[$counter][2] = $sor['wizardLvl'];
$troopArray[$counter][3] = $sor['balloonLvl'];
$troopArray[$counter][4] = $sor['dragonLvl'];
$troopArray[$counter][5] = $sor['minionLvl'];
$troopArray[$counter][6] = $sor['hogLvl'];
$troopArray[$counter][7] = $sor['golemLvl'];
$troopArray[$counter][8] = $sor['witchLvl'];
$troopArray[$counter][9] = $row['ID'];
$troopArray[$counter][10] = $row['Name'];
$counter++;
}
}
//------------------Writing the name into a new DIV------------------------
echo '<script type="text/javascript">';
echo 'var nameButton = document.createElement("input");';
echo 'nameButton.setAttribute("type","button");';
echo 'nameButton.setAttribute("value","' . $row['Name'] . '");';
echo 'nameButton.setAttribute("name","' . $row['ID'] . '");';
echo 'nameButton.style.backgroundImage="url(\"images/login_button/button_name.png\")";';
echo 'nameButton.style.marginLeft = "5px";';
echo 'nameButton.style.marginTop = "0px";';
echo 'nameButton.style.height="60px";';
echo 'nameButton.style.width="200px";';
echo 'nameButton.style.border ="none";';
echo 'nameButton.style.backgroundColor ="transparent";';
echo 'nameButton.style.fontWeight="900";';
echo 'nameButton.style.paddingLeft="50px";';
echo 'nameButton.style.textAlign="left";';
echo 'nameButton.onclick = function()
{
TableDraw(' . $troopArray[0][0] . ',' . $troopArray[0][1] . ',' . $troopArray[0][2] . ',' . $troopArray[0][3] . ',' . $troopArray[0][4] . ',' . $troopArray[0][5] . ',' . $troopArray[0][6] . ',' . $troopArray[0][7] . ',' . $troopArray[0][8] . ',' . $troopArray[0][9] . ',' . $troopArray[0][10] . ');
};';
echo 'document.body.appendChild(nameButton);';
echo '</script>';
echo '<br>';
?>
Everything's OK until $troopArray[$counter][10] = $row['Name'];
, I even use it later on when I set it as a button's text. If I change 'Name' into 'ID', the code runs.
As far as I know, I can store any type of variables in a PHP array, because it's not truly an array. But why I can't seem to store a string among the integers?
name
instead ofName
by any chance? – Fred -ii- Jul 2 at 13:36echo 'nameButton.setAttribute("value","' . $row['Name'] . '");';
, and it's working (the button's text is correct). – benedekadam Jul 2 at 13:37}
other than that, I don't know what the problem could be. Try adding error reporting to the top of your file(s)error_reporting(E_ALL); ini_set('display_errors', 1);
see if it yields anything more. – Fred -ii- Jul 2 at 13:46