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.

Fetching data to get array of ids i need

$STH = $DBH->prepare("SELECT char_id FROM att WHERE raid_id=:id AND spec=:spec AND status=:status");
$STH->bindValue(":id", $id);
$STH->bindValue(":spec", $curr_spec);
$STH->bindValue(":status", $curr_status);
$STH->setFetchMode(PDO::FETCH_ASSOC);
$STH->execute();
$temp1 = $STH->fetchAll();

This code works perfectly. It returns correct values.

$temporary =$temp1[0]["char_id"];
echo $temporary;

But when i try to get values from array in this for loop it returns nothing in echo.

for($i=0; $i < 2; $i++)
{
$tempor =$temp1[i]["char_id"];
echo $tempor;

//...
}

I've spent a lot of time, but I couldn't solve it myself. If possible, please help me.

share|improve this question
    
This is your entire code or there's codes between your echo $temporary and for statement? –  DontVoteMeDown Jun 10 '13 at 14:31
    
You just forget the "$" before var name! $temp[$i]['char_id'] –  JoDev Jun 10 '13 at 14:34

1 Answer 1

up vote 3 down vote accepted

its not i change it to $i. You are accessing undefined constant and error_log would show you notice error.

$tempor =$temp1[i]["char_id"];

----------------^ change it to $i

Tip: instead of using for loop you could try using foreach loop and have a counter that will only loop for first 2 records.

share|improve this answer
    
And the OP will probably want to make sure count($temp1) >= 2 –  landons Jun 10 '13 at 14:33
    
Ah, you're right. I feel embrassed. Should have seen it. But it wasn't actually the point why I posted this question. I thought that was the point why my SQL syntax is wrong but it seems to be something else. I have such query: $STH = $DBH->query("SELECT * FROM 'char' WHERE id=$tempor"); $STH->setFetchMode(PDO::FETCH_CLASS, "char"); $curr_char = $STH->fetch(); –  Nonemoticoner Jun 10 '13 at 14:41
    
PHP Error says: Syntax error or access violation: 1064 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 ''char' WHERE id=2' at line 1' in /home/a9434194/public_html/raid.php:218 Stack trace: #0 /home/a9434194/public_html/raid.php(218): PDO->query('SELECT * FROM '...') 'char' is one of my MySQL tables. –  Nonemoticoner Jun 10 '13 at 14:47

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.