-1

[PHP 8.1/SQL 10.4]

DB Structure

CREATE TABLE `trundschreiben` (
  `rsID` int(11) NOT NULL,
  `rsMTGLemail` varchar(255) COLLATE latin1_german1_ci NOT NULL,
  `rsVersendet` tinyint(1) DEFAULT NULL,
  `rsVersanddatum` date DEFAULT NULL,
  `rsAbsender` varchar(255) COLLATE latin1_german1_ci DEFAULT NULL,
  `rssprache` varchar(3) COLLATE latin1_german1_ci NOT NULL,
  `rskurse` text COLLATE latin1_german1_ci NOT NULL,
  `rskurseplain` text COLLATE latin1_german1_ci NOT NULL,
  `rsfirmenname` varchar(255) COLLATE latin1_german1_ci DEFAULT NULL,
  `rstyp` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;

PHP Code:

   $sql = "SELECT MAX(rsID) as rsid FROM trundschreiben";   
   $result = mysqli_query($db_link, $sql);  
   $nrRS =  mysqli_fetch_array($result, MYSQLI_ASSOC)

GOAL

Get the highest value of 'rsID' from the table

WHAT I TRIED

In phpmyadmin it works perfectly, the value is returned

QUESTION

In PHP, var_dump($nrRS); returns me a value of

array(1) { ["rsid"]=> NULL }

while the $result returns me this array

current_field: 0 
field_count: 1 
lengths: null 
num_rows: 1 
type:0

How come I can't receive the value as I expect? Thank you!

4
  • 1
    PHP returns me a value of null...where? You haven't shown any attempt to use the contents of $nrRS to output anything. What does var_dump($nrRS); return? Commented Aug 31, 2022 at 11:40
  • @ADyson it's simple null :) Commented Aug 31, 2022 at 11:41
  • @ADyson array(1) { ["rsid"]=> NULL } Commented Aug 31, 2022 at 11:43
  • 1
    I think that could only happen if the table contains no rows - demo: dbfiddle.uk/… . If phpMyAdmin gives you results for the same query, then my guess would be that your PHP code is looking at a different copy of your database. Commented Aug 31, 2022 at 11:47

1 Answer 1

0

When your table is empty, the max value is NULL. After inserting at least one row in the table, your code will start returning a number.

PHP online environment

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.