0
$sqlCek = "SELECT nomor FROM otoritas.permintaan_aplikasi WHERE tanggal LIKE '".date("Y-m-")."%' ORDER BY idpermintaan_aplikasi DESC LIMIT 0,1";
$queryCek = mysqli_query($koneksi,$sqlCek)or die("error");
echo $sqlCek."<br>";
    
$no = 1;
if(mysqli_fetch_array($queryCek)>0){
    $rowCek = mysqli_fetch_array($queryCek);
    echo var_dump($rowCek)."<br>";
    $no = ltrim(substr($rowCek['nomor'],7),"0") + 1;
}

When I tried to compile the mysql code in PHP code, it didn't show errors (even when i use try and catch exception). So when I tried to search the problem, I found that there is null result when I use echo var_dump (in line 8). When I tried to print the sql variable (line 1 and line 3), it shows SELECT nomor FROM otoritas.permintaan_aplikasi WHERE tanggal LIKE '2022-08-%' ORDER BY idpermintaan_aplikasi DESC LIMIT 0,1. I copy the sql variable on phpmyadmin, the result returns 1 row (Showing rows 0 - 0 (1 total, Query took 0.0006 seconds.)) which it makes me confused on different result on PHP and phpmyadmin.

So, what should I do to solve this and why is it returns NULL? I tried to look for many another similar problem but it didn't help me.

PS : PHP version is 8.1.2

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
pup_in_the_tree
  • 169
  • 1
  • 1
  • 8
  • just curious what you expect from if (mysqli_fetch_array($queryCek)>0). I mean, why there is a mysqli_fetch_array inside the condition? Why you're comparing its result to a number(?!). And why there is a condition at all. In the end, you are fetching 2 rows, discards the first row results, and the second fetch naturally returns you null – Your Common Sense Aug 02 '22 at 04:18
  • because it is if there are more than 0 query count in result, so if it shows 1, I expect to make a new number code here after substring the last number the result found and added by 1 (line 9) – pup_in_the_tree Aug 02 '22 at 04:27
  • 1. this whole sentence makes no sense at all. the "new number" thing doesn't explain why there is a condition, why there is a mysqli_fetch_array inside a condition, and why its result compared to zero. 2. You should never add numbers like this. You should have the autoincrement column in your table and it will "add by 1" automatically – Your Common Sense Aug 02 '22 at 04:30

0 Answers0