hder :
When I execute the following code, the output is fine, I get 3 records:
$sql = "SELECT * FROM log WHERE follow=1 AND action = 'new' AND processed = 0 AND type = 'question'";
$result = $db->prepare($sql);
$result->execute();
while($row = $result->fetch(PDO::FETCH_ASSOC)):
echo $row['action']." ".$row['type']." id: ".$row['type_id'].'<br>';
//insert more sql in here
endwhile;
NO ERRORS.
I have more sql that I test separately before I add into the while loop:
$id=$row['type_id'];
//retrieve all new questions - name and content & compile into message
$stmt = $db->prepare("SELECT * FROM questions WHERE question_id=?");
$stmt->execute([$id]);
$result = $stmt->fetch();
$content = $result['question'];
$author_id = $result['user_id'];
//get author name
$stmt = $db->prepare("SELECT username FROM users WHERE id=?");
$stmt->execute([$author_id]);
$result = $stmt->fetch();
$name = $result['username'];
echo '<strong>'.$name.': </strong>'.$content.'<br>';
I use a number for $id for testing and I get results.
NO ERRORS.
When I insert this code in to the while loop, I get the fatal error. It executes one loop and fails.
Fatal error: Uncaught Error: Call to a member function fetch() on array
The complete code:
$sql = "SELECT * FROM log WHERE follow=1 AND action = 'new' AND processed = 0 AND type = 'question'";
$result = $db->prepare($sql);
$result->execute();
while($row = $result->fetch(PDO::FETCH_ASSOC)):
//echo $row['action']." ".$row['type']." id: ".$row['type_id'].'<br>';
$id=$row['type_id'];
//retrieve all new questions - name and content & compile into message
$stmt = $db->prepare("SELECT * FROM questions WHERE question_id=?");
$stmt->execute([$id]);
$result = $stmt->fetch();
$content = $result['question'];
$author_id = $result['user_id'];
//get author name
$stmt = $db->prepare("SELECT username FROM users WHERE id=?");
$stmt->execute([$author_id]);
$result = $stmt->fetch();
$name = $result['username'];
echo '<strong>'.$name.': </strong>'.$content.'<br>';
endwhile;
I suspect something very simple, but can't seem to see it. Thanks in advance!
hder :
Became so obvious after taking a little lunch break - I was using $result for all 3 queries. I changed it to $result, $result2, $result3 and that solved the problem:
$sql = "SELECT * FROM log WHERE follow=1 AND action = 'new' AND processed = 0 AND type = 'question'";
$result = $db->prepare($sql);
$result->execute();
while($row = $result->fetch(PDO::FETCH_ASSOC)):
//echo $row['action']." ".$row['type']." id: ".$row['type_id'].'<br>';
$id=$row['type_id'];
//retrieve all new questions - name and content & compile into message
$stmt = $db->prepare("SELECT * FROM questions WHERE question_id=?");
$stmt->execute([$id]);
$result2 = $stmt->fetch();
$content = $result2['question'];
$author_id = $result2['user_id'];
//get author name
$stmt = $db->prepare("SELECT username FROM users WHERE id=?");
$stmt->execute([$author_id]);
$result3 = $stmt->fetch();
$name = $result3['username'];
echo '<strong>'.$name.': </strong>'.$content.'<br>';
endwhile;
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=379508&siteId=1