I have this table
cityreportcomment :
-----------------
reportID (fK)
cityID (fK)
comment
EDIT: if city has no comment, there is no row in the table: a row (for a city) exists only if that city has a comment.
for every report, I print 13 cities (that are in another table). I would like to check if city has a comment, then echo comment, if not, echo 'different'; but my code doesn't work.
for ($i = 0; $i < 13; ++$i) {
$stmt = $conn->prepare("SELECT * FROM cityreportcomment WHERE reportID=? AND cityID=?");
if (!$stmt) {
die(printf("Error: %s.\n", mysqli_stmt_error($stmt)));
} else if (!$stmt->bind_param('ii', $reportID, $selectcityID_array_unique[$i])) {
die(printf("Error: %s.\n", mysqli_stmt_error($stmt)));
} else if (!$stmt->execute()) {
die(printf("Error execute from ereportcomment table: %s.\n", mysqli_stmt_error($stmt)));
} else {
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
if (isset($row['cityID']) && $row['cityID'] == $selectcityID_array_unique[$i]) {
$selectcomment2[] = 'same' . $row['cityID'] . (isset($row['comment']) ? $row['comment'] : "");
} elseif (!isset($row['cityID']) || $row['cityID'] != $selectcityID_array_unique[$i]) {
$selectcomment2[] = 'different';
}
}
}
/* end else */
$stmt->close();
echo $i . ' ' . $selectcomment2[$i] . '<br>';
} //end for
PROBLEM:
if $i=1
has comment, $i=2
no comment, $i=3
has comment, $i=4
no comment, my echo results is
1 comment1
2 comment3
3
4
5
.....
It should be
1 comment1
2 different
3 comment3
4 different
5 different
.....
SOLUTION:
Thanks to everybody for your help: after reading every answer and every comment, this is what I did (very important is to unset $comment2 at the beginning of the loop):
for ($i = 0; $i < 13; ++$i) {
unset( $selectcomment2 ); //veeeery important
$stmt = $conn->prepare("SELECT * FROM cityreportcomment WHERE reportID=? AND cityID=?");
if (!$stmt) {
die(printf("Error: %s.\n", mysqli_stmt_error($stmt)));
} else if (!$stmt->bind_param('ii', $reportID, $selectcityID_array_unique[$i])) {
die(printf("Error: %s.\n", mysqli_stmt_error($stmt)));
} else if (!$stmt->execute()) {
die(printf("Error execute from ereportcomment table: %s.\n", mysqli_stmt_error($stmt)));
} else {
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$selectcomment2 = $row['comment'];
}
}
/* end else */
$stmt->close();
if ( !isset($selectcomment2 ) ){
$selectcomment2 = 'different';
}
echo $i . ' ' . $selectcomment2[$i] . '<br>';
} //end for
Thanks again (please don't edit this line, I really appreciate all the help I received)
答案 0 :(得分:2)
Use this instead:
if (child.name == "MapContainer") {
continue;
}
Your if/else doesn't work because you are checking if while ($row = $result->fetch_assoc()) {
if (!empty($row['cityID']) && $row['cityID'] == $selectcityID_array_unique[$i]) {
$selectcomment2[] = 'same' . $row['cityID'] . (!empty($row['comment']) ? $row['comment'] : "");
} elseif (empty($row['cityID']) || $row['cityID'] != $selectcityID_array_unique[$i]) {
$selectcomment2[] = 'different';
} else {
continue; # do nothing
}
}
"is set", in other words, if it exists or not... However, because you are using it on a mysql loop where you are returning those fields, they will always be set (exist). I suspect that what you want to check is, if $row['cityID']
is empty or not, which in that case you could use $row['cityID']
instead.
!empty()
答案 1 :(得分:1)
You have this SQL:
public class MyResource extends AbstractResource {
private Manager manager;
@Inject
public MyResource(@Named("authorization") Authorization auth,
@Named("helper") Helper helper) {
super(auth, helper);
this.manager = new Manager();
}
...
You are binding "SELECT * FROM cityreportcomment WHERE reportID=? AND cityID=?"
to the $selectcityID_array_unique[$i]
placeholder, so every record returned will have a cityID
equal to cityID
.
Then you are checking this:
$selectcityID_array_unique[$i]
Naturally, it will always be true. If no records are returned from the query, you will not see if (isset($row['cityID']) && $row['cityID'] == $selectcityID_array_unique[$i]) {
, because the while loop will execute zero times.
You can fix it like this:
different
答案 2 :(得分:1)
If the comment is empty, it won't be !isset.
This code should do what you want:
I would like to check if city has a comment, then echo comment, if not, echo 'different'
string input;
int num;
cout << "Enter a series of number, then type Q to process: ";
while(true)
{
cin >> input;
if(input == "q" || input == "Q")
break;
num = strtol(input.c_str());
.....
}