Why am I getting different results on my home computer and my webpage?

I want to get the all the indexes from a mysql table which correspond to a given student number and week number.
I use this to display the correct answers and the student’s answers side by side.
Often a student will send more than 1 time, which isn’t a problem for me.

if(isset($_POST['weeknr'])) {
		$indexsql = 'SELECT id FROM allstudentsAnswers' . $class . ' WHERE studentnr = \'' .      $studentnr . '\' AND weeknr = \'' . $weeknr . '\'';
		$indexresult = $pdo->query($indexsql);
		
		foreach ($indexresult as $row){
		$indices[] = $row;				
		}
		echo 'this student this week has these entries <br><br>';
		print_r($indices);

On my home computer, this works fine.

print_r($indices); shows me the correct values.

I make php print_r() and echo print all info. When everything works, I will block the printing:

this student this week has these entries (print_r())

Array ( [0] => Array ( [id] => 1 [0] => 1 ) [1] => Array ( [id] => 3 [0] => 3 ) )

this student this week has these entries
there are 2 entries for this week and student number
$indices[0][0] = 1
$indices[1][0] = 3

But when I do this on my webpage, I tried all kinds of things, I get (I made 4 entries for 1 student number and week number):

this student this week has these entries (print_r())

Array ( [0] => Array ( [id] => 2 ) [1] => Array ( [id] => 3 ) [2] => Array ( [id] => 4 ) [3] => Array ( [id] => 9 ) )

there are 4 entries for this week and student number

$indices[0][0] =
$indices[1][0] =
$indices[2][0] =
$indices[3][0] =

I’ve tried many things, but, although print_r($indices); shows the correct information, I cannot grab the values in $indices, as I can on my home computer.

I need the values to output all answers.

Any suggestions please?

Probably an initial condition issue and the fact that the lowest id doesn’t start at 1. There could also be something unusual about the values. What does using var_dump($indices); show? You would need to post the actual code that doesn’t work to get specific help with what is wrong with it.

Some other points about your code -

  1. Don’t put external, unknown, dynamic values directly into an sql query statement. Use a prepared query.
  2. If you do have a need to put a php variable into a string, use a double-quoted string, so that you avoid all the extra quotes and dots.
  3. Properly normalize your data. Don’t create a series of table names that contain a variable value that’s actually part of the data.
  4. Do set the default fetch mode to assoc when you make the database connection. You should almost always use associative indexed data so that your code is self-documenting. This helps avoid coding mistakes, prevents the duplication of numerical/associative data, and when you are selecting more than one column allows your code to continue to work should your table columns get rearranged.
  5. Don’t make up verbose variable names for nothing. The only variable name that must be unique for a block of code is the final variable holding the resultant data. Just use simple variables for common activities, such as $sql for the sql query, $stmt for the PDOStatement, …
  6. The foreach loop is just fetching all of the rows of data. There’s a built-in method to do that - fetchAll()
  7. If you output <pre> </pre> tags around any print_r() or var_dump() statement, the output will be formatted and easier to read.

Thanks for your reply!

  1. Those values in the query statement are checked, they couldn’t be malicious code, or they would not get through. $class is checked, if it is not in the (small) array $classes, reject. If $studentnr is not in the table of student numbers, reject. If $weeknr is not in the array of week numbers, reject.

  2. I don’t understand that.

  3. The tables are identical, except for the class names, “allstudentsAnswers’ . $class” makes this available for any class I might have.

  4. I don’t understand.

  5. The names help me see and understand what is going on.

  6. I will try a fetchAll() on $indexsql, see if that helps.

  7. All this printed output is just for me to see what I am getting, as soon as I get what I want, I will block the printed php output.

The actual code, which works on my laptop, but not on my webpage is:

if(isset($_POST['weeknr'])) {
		$indexsql = 'SELECT id FROM allstudentsAnswers' . $class . ' WHERE studentnr = \'' . $studentnr . '\' AND weeknr = \'' . $weeknr . '\'';
		$indexresult = $pdo->query($indexsql);
		
		foreach ($indexresult as $row){
		$indices[] = $row;				
		}
		echo 'this student this week has these entries <br><br>';
		print_r($indices);
		
		echo '<br><br>this student this week has these entries <br>';

Especially the last part:

$indexcount = count($indices);
echo 'there are ' . $indexcount . ' entries for this week and student number<br>';
for ($a=0; $a < $indexcount; $a++) {		
echo '$indices[' . $a . '][0] = ' . $indices[$a][0] . '<br>';
}

I need the row index values, the primary key for this table, corresponding to a row with this student number and this week number.

The index count is correctly 4, print_r() shows me the correct values: 2, 3 ,4 and 9.

But I do not get any values. I am expecting 2, 3 ,4 and 9. I cannot understand why this works at home but not on my webhost.

With them, I can grab each row by its index and get what I want. Works great at home!!

I don’t think that the initial value of the index is important. It is just a value, a number, in a column, albeit a primary key.

Figured out my problem:

When you return a value from a row, $row is an array, with the column name(s) as key and the value = the column value in that row.

$indexsql = 'SELECT id FROM allstudentsAnswers' . $class . ' WHERE studentnr = \'' . $studentnr . '\' AND weeknr = \'' . $weeknr . '\'';
$indexresult = $pdo->prepare($indexsql);
$indexresult = $pdo->query($indexsql);
		
foreach ($indexresult as $row){$indices[] = $row['id'];	}

Kind of begs the question how it works on my computer, where I just put $indices[] = $row;

But I’m glad it works now as intended

Upon seeing how the code was referencing the value to display it, it’s apparent from the print_r() of the data -

why this doesn’t work. There is no [0] element in the data to display, because the fetch mode is likely different between the two systems.

Thanks again!

I would never have suspected that the fetch() modes could vary!! I’ll remember that!!

Sponsor our Newsletter | Privacy Policy | Terms of Service