Barand Posted February 5, 2020 Share Posted February 5, 2020 10 minutes ago, mconte said: your version might not support MAX() Which versions don't? I haven't come across one. Quote Link to comment Share on other sites More sharing options...
phppup Posted February 5, 2020 Author Share Posted February 5, 2020 (edited) Yes, MCONTE, both of those seem to work, but apparently mysqli_insert_id($link); works ONLY during an INSERT and not as a method to SELECT (independent of an INSERT in the same script). Barand, I have a simple table being displayed row by row that is giving me results from other script efforts. Didn't want to waste space by posting it. echo "</tr>"; while($row = mysqli_fetch_array($result)){ echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['first_name'] . "</td>"; echo "<td>" . $row['last_name'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; Edited February 5, 2020 by phppup Quote Link to comment Share on other sites More sharing options...
Barand Posted February 5, 2020 Share Posted February 5, 2020 That has nothing to do with your problem of finding the MAX(id), although it does show that you are capable, sometimes, of processing the results of a query. Quote Link to comment Share on other sites More sharing options...
mconte Posted February 5, 2020 Share Posted February 5, 2020 18 minutes ago, Barand said: Which versions don't? I haven't come across one. I guess prior to 4.0 🙄 @phppup please do an "explain persons;" and show us the results ...is your id field a string?? Quote Link to comment Share on other sites More sharing options...
Barand Posted February 5, 2020 Share Posted February 5, 2020 (edited) 16 minutes ago, mconte said: "explain persons;" Do you mean "describe persons"? NM - I see they do the same thing mysql> describe pup; +------------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | amount | decimal(8,2) | YES | | NULL | | | transaction_date | datetime | YES | | NULL | | +------------------+--------------+------+-----+---------+----------------+ 4 rows in set (0.01 sec) mysql> explain pup; +------------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | amount | decimal(8,2) | YES | | NULL | | | transaction_date | datetime | YES | | NULL | | +------------------+--------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) I only ever used explain with queries, not tables. Edited February 5, 2020 by Barand Quote Link to comment Share on other sites More sharing options...
phppup Posted February 5, 2020 Author Share Posted February 5, 2020 (edited) Field Type Null Key Default Extra id int(11) NO PRI NULL auto_increment first_name varchar(30) NO NULL last_name varchar(24) NO NULL email varchar(70) NO UNI NULL sum int(11) NO NULL Persons is a simple table made up of some simple columns: id, first_name, last_name, email, sum. Edited February 5, 2020 by phppup Quote Link to comment Share on other sites More sharing options...
mconte Posted February 6, 2020 Share Posted February 6, 2020 are you seeing an error from "select max(id) from persons;" Quote Link to comment Share on other sites More sharing options...
phppup Posted February 7, 2020 Author Share Posted February 7, 2020 $sql = "SELECT MAX(id) FROM persons" ; if($result = mysqli_query($conn, $sql)){ if(mysqli_num_rows($result) > 0){ echo "<br /><br />ROW ".$row['id']."<br /><br />"; } } gives me: ROW and an ERROR message of "Notice: Undefined index: id" Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 7, 2020 Share Posted February 7, 2020 Probably cause the selected value is named "max(id)" or something similar. Change the query to provide a name for the function result. select max(id) as maxid should do it and then you reference 'maxid' for your value. Quote Link to comment Share on other sites More sharing options...
phppup Posted February 7, 2020 Author Share Posted February 7, 2020 (edited) select max(id) as maxid resulted in: ERROR: Could not able to execute select max(id) as maxid. Unknown column 'id' in 'field list' $sql ="select max(id) FROM persons as maxid"; still provided NO result Edited February 7, 2020 by phppup Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted February 7, 2020 Share Posted February 7, 2020 42 minutes ago, phppup said: ...and an ERROR message of "Notice: Undefined index: id" You need to fetch the row and assign it to $row. Examples can be found here:https://www.php.net/manual/en/mysqli-result.fetch-assoc.php Quote Link to comment Share on other sites More sharing options...
Barand Posted February 7, 2020 Share Posted February 7, 2020 (edited) Works fine for me mysql> select * from pup; +----+-------+--------+---------------------+ | id | name | amount | transaction_date | +----+-------+--------+---------------------+ | 1 | Curly | 10.00 | 2020-01-01 10:00:00 | | 2 | Larry | 20.00 | 2020-01-15 12:30:00 | | 3 | Mo | 15.00 | 2020-02-01 09:00:00 | | 4 | Peter | -5.00 | 2020-02-01 10:30:00 | | 5 | Paul | 10.00 | 2020-02-02 11:30:00 | | 6 | Mary | 5.00 | 2020-02-02 12:15:00 | +----+-------+--------+---------------------+ 6 rows in set (0.00 sec) mysql> select MAX(id) as maxid FROM pup; +-------+ | maxid | +-------+ | 6 | +-------+ 1 row in set (0.01 sec) or <?php $res = $conn->query("SELECT MAX(id) as maxid FROM pup"); $row = $res->fetch_assoc(); echo $row['maxid']; //--> 6 ?> Edited February 7, 2020 by Barand Quote Link to comment Share on other sites More sharing options...
phppup Posted February 7, 2020 Author Share Posted February 7, 2020 Got it working. Cleared up my error messages. Even re-wrote it in Procedural style for my clarity. Thank you all for your input. Thank you, <strong> sensei </strong> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.