insert_id PHP函数什么都不返回

时间:2018-07-09 09:24:39

标签: php mysql sql database mysqli

我想从查询中获取最后插入的行的(主)ID。因此,我发现可以使用函数insert_id来实现这一点,除了它对我始终不返回任何内容。

为什么会这样?

我的代码:

$adduser = "
INSERT INTO users (username, password)
VALUES('".$username."', '".$pass."')";
$addusercon = $conn->query($adduser);
echo "User added. Last ID is: " . $conn->insert_id;

该查询有效,因为用户已添加到我的数据库中,并且添加的行中也包含一个ID(主auto_increment)。

根据要求,我的users表的SQL:

-- phpMyAdmin SQL Dump
-- version 4.7.7
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Gegenereerd op: 09 jul 2018 om 13:52
-- Serverversie: 5.6.39
-- PHP-versie: 5.6.30

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `mydatabasename`
--

-- --------------------------------------------------------

--
-- Tabelstructuur voor tabel `users`
--

CREATE TABLE `users` (
  `id` int(255) NOT NULL,
  `id_company` varchar(255) NOT NULL,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `rights` int(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Gegevens worden geëxporteerd voor tabel `users`
--

INSERT INTO `users` (`id`, `id_company`, `username`, `password`, `email`, `rights`) VALUES
(1, '', 'admin', '$2y$10$qHKALBn6kLttWuxBgy4uW.gp9T5ArwUXSNfFKQjSUkPSZRfySYxEy', 'twan@live.nl', 1),
(2, '', 'stoutjesdijk', '$2y$10$emiGX.EtF122cRYuz5fVI.pNZp0NM678xHj9JDktP5uZ8mMxB/mPO', 'twan@live.nl', 0);

--
-- Indexen voor geëxporteerde tabellen
--

--
-- Indexen voor tabel `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT voor geëxporteerde tabellen
--

--
-- AUTO_INCREMENT voor een tabel `users`
--
ALTER TABLE `users`
  MODIFY `id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=26;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

0 个答案:

没有答案