-3

I have come across a strange problem, which im trying to solve for quite some time now but can´t find any solution to this.

I am generating some lines with information which each of includes one checkbox. I have the following code in PHP which checks if a certrain entry exists, if so the checkbox is checked.

$sql = "SELECT COUNT(*) anz FROM jubilaeum WHERE jahr='".$Jahr."' AND mon='".$num."' AND AdrNr='".$RS1_row["AdrNr"]."' AND type='1'";
$rs_erledigt = $db->prepare($sql);
$rs_erledigt->execute();
$row = $rs_erledigt->fetch();
$anz = $row["anz"];

The Code generates me the following SQL Query:

SELECT COUNT(*) anz FROM jubilaeum WHERE jahr='2019' AND mon='5' AND AdrNr='14061' AND type='1'

PHPMyAdmin Query & Result

Now i am using a basic IF to check if there are any records found so i can check a checkbox

<input type="checkbox" name="mychk" id="mychk" value="somevalue" <?php if($anz>0) echo "checked"; ?> />

All checkboxes which have a proper entry in my DB are checked, except the very first one generated, i can swap the boxes around at free will, the first one never gets checked. I tried to use the $row["Anz"] directly in my IF, didn´t fix the problem.

I think that PHP doesnt interpret the returned value of my query correctly, but i am clueless about how to fix this.

Did someone encounter similar problems and can help me with this? Im new to posting in here, so please tell me if you need some more information. Thanks in advance!

Edit: I just tried to change the Query from COUNT(*) to if(COUNT(*)>0,'ja','nein') while also changing the if to if($anz=="ja") the value of $anz still remains empty.

Community
  • 1
  • 1
Loggi
  • 35
  • 1
  • 7
  • 1
    Is your query failing from your code? try `var_dump($anz)` and see if you're actually getting what you expect. – Geoffrey May 24 '19 at 08:34
  • Use `var_dump` to check what `$anz` actually contains. If it doesn’t contain anything, do the same with `$row` to see if that contained what you expected to begin with. – 04FS May 24 '19 at 08:35
  • Try `var_dump($row)` to see if it is what you expect. Perhaps you actually have only a `$row[0]` value... – Nick May 24 '19 at 08:35
  • Also, are you doing this in a loop, making an extra database query for each single checkbox? “Queries in loops” is a general performance no-no, and should be avoided whenever possible. This could probably be done in a single query, if you added a GROUP BY clause. – 04FS May 24 '19 at 08:37
  • `var_dump($anz)` and `var_dump($row)` both gives me NULL. But when i execute the very same query on PhpMyAdmin directly, i get the correct result – Loggi May 24 '19 at 08:37
  • @04FS Thank you very much for your input, i actually could change it to a single query outside the loop – Loggi May 24 '19 at 08:43
  • var_dump `$rs_erledigt` and the result of `$rs_erledigt->execute()` as well, to see if preparing the statement or executing it failed. – 04FS May 24 '19 at 08:52
  • If preparing failed he would get an error trying to call `execute()` on a boolean. – Barmar May 24 '19 at 08:53
  • Make sure you have full error reporting enabled. See https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php. Also enable error signaling in PDO: https://www.php.net/manual/en/pdo.error-handling.php – Barmar May 24 '19 at 08:57
  • One of the statements must be failing. Even if the query doesn't return anything, `$row` should be `FALSE`, not `NULL`. – Barmar May 24 '19 at 08:58
  • 1
    BTW, you should learn to use parametrized queries rather than substituting variables into SQL. – Barmar May 24 '19 at 08:58
  • @04FS `var_dump($rs_erledigt);` shows me that preparing works. Since i changed the Query to be executed outside the loop, i don´t get this problem anymore, everything works fine. – Loggi May 24 '19 at 09:01
  • @Barmar I just checked again `var_dump($row)` actually IS false, not NULL, i didn´t see that correctly. I will look into parametrized queries aswell, thank you. – Loggi May 24 '19 at 09:03
  • It's still not really possible. That query always returns exactly one row. Is it possible you're calling `fetch()` twice? The first fetch will retrieve the row, the second will return `FALSE`. – Barmar May 24 '19 at 09:05
  • Please post the whole loop so we can see exactly what you're doing. – Barmar May 24 '19 at 09:06
  • @Barmar Well, i feel very stupid now, i just double checked everything word by word again and noticed that there is indeed a second fetch. Thank you very much. – Loggi May 24 '19 at 09:10
  • If you had posted the actual code in the first place we could have saved a lot of time. – Barmar May 24 '19 at 09:11
  • @Barmar Yeah, i will make sure to do so in future posts, thank you. – Loggi May 24 '19 at 09:14

1 Answers1

0

I found the solution. My issue was a second fetch on my Query. After removing it, everything works fine.

Loggi
  • 35
  • 1
  • 7