Jump to content

Recommended Posts

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 by phppup
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 by Barand
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 by phppup
$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"

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 by phppup

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 by Barand
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.