How do I get the autoincrement id without being registered?

1

Hello,

How do I get the id that is autoincrement without the item being registered in the database? Or I will have to do 2 process, first register the post, then register the post in the category?

I'm with a system to register posts here, and the tables are separate so I can register the same post in several categories:

Table:Products

Table:Categories

Relationshiptable:Products~Categories

PHPcodetoregisterthepost:

$cadastrarItem=$conexao->prepare("INSERT INTO tb_mark (name_mark, description_mark, keywords, att_mark, image_mark, link) VALUES (:title, :description, :keywords, NOW(), :img, :linkitem)");
                $cadastrarItem->bindParam(':title', $title, PDO::PARAM_STR);
                $cadastrarItem->bindParam(':img', $novoNome, PDO::PARAM_STR);
                $cadastrarItem->bindParam(':description', $description, PDO::PARAM_STR);
                $cadastrarItem->bindParam(':keywords', $tags, PDO::PARAM_STR);
                $cadastrarItem->bindParam(':linkitem', $linkitem, PDO::PARAM_STR);

                $verificaItens = $conexao->prepare("SELECT name_mark FROM tb_mark WHERE name_mark=:title");
                $verificaItens->bindParam(':title', $title, PDO::PARAM_STR);
                $verificaItens->execute();

                if($verificaItens->rowCount() == 0)
                {
                    $cadastrarItem->execute();
                    echo '<script language= "javascript">
                    location.href="/admin_include_brand/register_item";
                    </script>';
                }
                else
                {
                    echo '<script language= "javascript">
                    location.href="/admin_include_brand/register_error";
                    </script>';
                }

And it would be basically this for me to register the category in the table:

    $cadastrarItemCategoria = $conexao->prepare("INSERT INTO tb_category_itens (id_item, id_category) VALUES (:id_item, :id_category)");
// id_item = 'id' da tabela produtos
// id_category = 'id' da tabela categorias
    $cadastrarItemCategoria->bindParam(':id_item', $id_item, PDO::PARAM_STR);
    $cadastrarItemCategoria->bindParam(':id_category', $category, PDO::PARAM_STR);
    
asked by anonymous 09.02.2016 / 20:25

2 answers

5

Boy did I have these problems these days

SHOW TABLE STATUS LIKE 'nomedatabela' 

Here's how I used it.

try {
$sql = "SHOW TABLE STATUS LIKE 'nomedatabela' ";  
$stmt = $DB->prepare($sql);
$stmt->execute();
$resultado = $stmt->fetch();
$proximoID = $resultado['Auto_increment'];  // a chave esta aqui
 } catch (Exception $ex) {
 echo $ex->getMessage();
}
echo $proximoID;
    
09.02.2016 / 20:42
0

There is a method using information_schema .

The MySQL build will be:

SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = <NOME DA TABELA> AND table_schema = <NOME DO BANCO DE DADOS>

For example:

<?php

// Dados necessários:
$nome_tabela = 'tabela';
$nome_banco = '';

// Se deixar o $nome_banco em branco isto irá usar a função DATABASE():
$nome_banco = ($nome_banco != '') ? "'".$nome_banco."'" : "DATABASE()";

// MYSQLI! Irá efetuar a query:
$id = $mysqli->query("SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = '$nome_tabela' AND table_schema = $nome_banco");

// Irá pegar e exibir o AUTO_INCREMENT:
$id = $id->fetch_array();
$id = $id['AUTO_INCREMENT'];
echo $id;

?>
    
14.02.2016 / 18:43