Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
4  
Is your column called name instead of Name by any chance? –  Fred -ii- Jul 2 at 13:36
    
What's in your error log? –  Wooble Jul 2 at 13:37
1  
@Fred-ii- No, I later use it in the same format in this line: echo 'nameButton.setAttribute("value","' . $row['Name'] . '");';, and it's working (the button's text is correct). –  benedekadam Jul 2 at 13:37
    
maybe your array is fixed length to 10? Can't remember if PHP arrays were fixed length –  user1094553 Jul 2 at 13:39
1  
(Sidenote) If that's your full code or close to, you're missing a closing brace } 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

2 Answers 2

I don't see any error in your code, but one thing is strange for me:

$troopArray = array(array());

You should change this line simple into:

$troopArray = array();

and check if the problem still occurs

One extra thing is that your $counter is always 0. It doesn't cause problem but maybe you want also to increment it before end of loop?

EDIT

If problem still occurs you should show us the whole loop code (now it finishes after first loop and maybe somewhere later you change something in your $row variable

share|improve this answer
    
Yeah, those two array() are also strange to me. –  Fred -ii- Jul 2 at 13:52
1  
No. As you can see, $troopArray is an array of arrays, as the code references an array using $troopArray[$counter]. I'm a bit confused, 'cause $counter never gets increased –  Julian Jul 2 at 13:52
    
@Julian I know, but I haven't seen such construct. Even if array has 3 dimensions you simple always initialize array using $array = array(); as far as I know –  Marcin Nabiałek Jul 2 at 13:54
    
No. PHP initializes an array as you set an element of it. So because of PHP's fail safeness, he don't need's to initialize an array at all. This would also work perfectly: $troopArray; $troopArray[0]['a'] = "b";. –  Julian Jul 2 at 13:56
2  
@benedekadam: It IS relevant as you see! Things like this are the likeliest issue in coding, I think. –  Julian Jul 2 at 13:57

I can't see an issue, but can it be recoded to use a JOIN and save the loop inside a loop? Assuming that they are both on the same database

$troopArray = array();
$counter = 0;
$sql = "SELECT  a.ID, a.Name, b.archerLvl, b.giantLvl, b.wizardLvl, b.balloonLvl, b.dragonLvl, b.minionLvl, b.hogLvl, b.golemLvl, b.witchLvl
        FROM Player a
        INNER JOIN Troops b
        ON a.ID = b.ID";
$result = mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result)) 
{
    $troopArray[$counter] = array();
    $troopArray[$counter][0] = $row['archerLvl'];
    $troopArray[$counter][1] = $row['giantLvl'];
    $troopArray[$counter][2] = $row['wizardLvl'];
    $troopArray[$counter][3] = $row['balloonLvl'];
    $troopArray[$counter][4] = $row['dragonLvl'];
    $troopArray[$counter][5] = $row['minionLvl'];
    $troopArray[$counter][6] = $row['hogLvl'];
    $troopArray[$counter][7] = $row['golemLvl'];
    $troopArray[$counter][8] = $row['witchLvl'];
    $troopArray[$counter][9] = $row['ID'];
    $troopArray[$counter][10] = $row['Name'];
    $counter++;
}

If you do this you can try doing a print_r of each $row and also a print_r of $troopArray when it has been populated and check that they look right.

As an aside, does the first Name happen to be a number?

EDIT - problem appears to be where you assign the values from this array into a javascript array. You have assigned it into the javascript array without surrounding quotes. This is probably resulting in javascript trying to find a variable called the value of Name to assign into the array.

share|improve this answer
    
+1 good catch. OP should've posted full code from the beginning and easily avoid all of this wasted time for everybody. –  Fred -ii- Jul 2 at 14:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.