0

I want to get the next auto increment id with PHP. I run the query in mysql and need the result:

column name: auto_increment value = 122

but I am getting a Notice:

Trying to get property 'auto_increment' of non-object

public function findNextAutoIncrementId()
    {
        $db = config::getConnexion();
        $sql = 'SELECT `auto_increment` FROM INFORMATION_SCHEMA.TABLES
                WHERE table_name = newsarticle';
        $req = $db->prepare($sql);
        $req->execute();
        $result = $req->fetch(PDO::FETCH_OBJ);
        return $result;
    }
// when i use it i get  Notice: Trying to get property 'auto_increment' of non-object
$result=$sujetC->findNextAutoIncrementId();
echo "this is : ".$result->auto_increment;

[Edit] : When I asked this question I wanted to find the next 'newsarticle' object that will be created to do some functions at it just after the insert operation and before showing it. What I should have made was to create a function that search for the specific 'newsarticle' after it has been created with the specific attributes

Tahero
  • 328
  • 4
  • 11
  • 4
    ***DO NOT DO THIS.*** You do not want to get the *next* ID, because that's wholly unreliable and *will* break your app and/or corrupt your database's integrity. What you want is to get the ID that the previous operation was given automatically via [mysql](https://dev.mysql.com/doc/refman/5.7/en/getting-unique-id.html) or [PDO](http://php.net/manual/en/pdo.lastinsertid.php). Eg: 1. Insert a new article. 2. Get the ID. 3. Use that ID in subsequent operations. – Sammitch Mar 27 '18 at 01:18
  • i want to get the next id so when i am going to add new news articles . it saves the photoname+id – Tahero Mar 27 '18 at 01:19
  • 3
    ***DO NOT DO THAT.*** I cannot stress this enough. If you try to guess the next ID [and it *is* a ***guess***] there's no guarantee that there's not another simultaneous request doing the exact same thing, guessing the exact same ID. Preventing this situation is 90% or the reason for AUTO_INCREMENT in the first place. – Sammitch Mar 27 '18 at 01:22
  • any idea how should I name the photos related to the article if i don't use the article id ? – Tahero Mar 27 '18 at 01:25
  • Read the docs I linked in my first comment. – Sammitch Mar 27 '18 at 01:26
  • Sorry guys. When I asked this question few years ago I wanted to find the next 'newsarticle' object that will be created to do some functions at it just after the insert operation and before showing it. What I should have made was to create a function that search for the specific 'newsarticle' after it has been created with the specific attributes. – Tahero Jul 11 '19 at 13:34

1 Answers1

2

Read this similar question. You don't need to do so.

Since you are using auto increment I assume, you don't need to specify the next insert ID when you're performing an INSERT query. You can skip the column and MySQL will assign the next ID for you.

For example, you have a table like this:

CREATE TABLE samples (
  id INT(11) NOT NULL AUTO_INCREMENT,
  sample_name VARCHAR(50) NOT NULL,
  PRIMARY KEY (id)
)
ENGINE=InnoDB;

When you're going to insert a new record, you just need to execute the following SQL command:

INSERT INTO samples (sample_name) VALUES ('Some Location')

No need to explicitly specify the value of the AUTO_INCREMENT column.

Raptor
  • 53,206
  • 45
  • 230
  • 366