I'm having some troubles with the fetch-method. Here's the code:
$stmt = $db->prepare("INSERT INTO x (name) VALUES (:username)");
$stmt->bindParam(':username', $username);
$result = $stmt->execute();
if ($result == '1') {
$id = $stmt->fetch();
return $id;
}
The query is executed and data inserted, and $result is '1'. However, when I try to fetch the result, it's returning false.
Any tips?
You can $db->lastInsertId() in MySQL (apparently it's not dependable, but I've never had a problem with it). Either That or run another query: SELECT LAST_INSERT_ID() and fetch that.
If you're wanting to fetch the number of rows inserted, follow it with this query:
$rows_inserted = $db->query("SELECT FOUND_ROWS()")->fetchColumn();
Related
I know my conn is good and the direct SQL produces the desired output, but my $row variable is not getting values from the -> FETCH()
Similar fetch() working in other code blocks
$stmt = $conn->prepare("SELECT firstname,vcode FROM users WHERE email = ? AND verifydate IS NULL AND active = 0");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows === 1) {
$row = $stmt->fetch();
$firstname = $row[0];
$vcode = $row[1];
}
No error msgs -- simply $row[] values empty
In mysqli, $stmt->fetch() doesn't return the row, it just returns true or false to indicate whether a row could be fetched. You need to use $stmt->fetch_row() to get a numerically-indexed row, or $stmt->fetch_assoc() to get an associative array.
$stmt->fetch() is used along with $stmt->bind_result(), e.g.
$stmt->bind_result($firstname, $vcode);
$stmt->fetch();
If you see similar code that works in other applications, they must be using PDO rather than mysqli.
I have a query, and I want to get the last ID inserted. The field ID is the primary key and auto incrementing.
I know that I have to use this statement:
LAST_INSERT_ID()
That statement works with a query like this:
$query = "INSERT INTO `cell-place` (ID) VALUES (LAST_INSERT_ID())";
But if I want to get the ID using this statement:
$ID = LAST_INSERT_ID();
I get this error:
Fatal error: Call to undefined function LAST_INSERT_ID()
What am I doing wrong?
That's because that's an SQL function, not PHP. You can use PDO::lastInsertId().
Like:
$stmt = $db->prepare("...");
$stmt->execute();
$id = $db->lastInsertId();
If you want to do it with SQL instead of the PDO API, you would do it like a normal select query:
$stmt = $db->query("SELECT LAST_INSERT_ID()");
$lastId = $stmt->fetchColumn();
lastInsertId() only work after the INSERT query.
Correct:
$stmt = $this->conn->prepare("INSERT INTO users(userName,userEmail,userPass)
VALUES(?,?,?);");
$sonuc = $stmt->execute([$username,$email,$pass]);
$LAST_ID = $this->conn->lastInsertId();
Incorrect:
$stmt = $this->conn->prepare("SELECT * FROM users");
$sonuc = $stmt->execute();
$LAST_ID = $this->conn->lastInsertId(); //always return string(1)=0
You can get the id of the last transaction by running lastInsertId() method on the connection object($conn).
Like this $lid = $conn->lastInsertId();
Please check out the docs https://www.php.net/manual/en/language.oop5.basic.php
I have a query, and I want to get the last ID inserted. The field ID is the primary key and auto incrementing.
I know that I have to use this statement:
LAST_INSERT_ID()
That statement works with a query like this:
$query = "INSERT INTO `cell-place` (ID) VALUES (LAST_INSERT_ID())";
But if I want to get the ID using this statement:
$ID = LAST_INSERT_ID();
I get this error:
Fatal error: Call to undefined function LAST_INSERT_ID()
What am I doing wrong?
That's because that's an SQL function, not PHP. You can use PDO::lastInsertId().
Like:
$stmt = $db->prepare("...");
$stmt->execute();
$id = $db->lastInsertId();
If you want to do it with SQL instead of the PDO API, you would do it like a normal select query:
$stmt = $db->query("SELECT LAST_INSERT_ID()");
$lastId = $stmt->fetchColumn();
lastInsertId() only work after the INSERT query.
Correct:
$stmt = $this->conn->prepare("INSERT INTO users(userName,userEmail,userPass)
VALUES(?,?,?);");
$sonuc = $stmt->execute([$username,$email,$pass]);
$LAST_ID = $this->conn->lastInsertId();
Incorrect:
$stmt = $this->conn->prepare("SELECT * FROM users");
$sonuc = $stmt->execute();
$LAST_ID = $this->conn->lastInsertId(); //always return string(1)=0
You can get the id of the last transaction by running lastInsertId() method on the connection object($conn).
Like this $lid = $conn->lastInsertId();
Please check out the docs https://www.php.net/manual/en/language.oop5.basic.php
I keep thinking on that "error" but can't say why it returns false.
I've already done a SELECT for this but that is in an other file..
$result = $db->dbh->prepare("SELECT thumbs FROM skill WHERE id=? LIMIT 1");
$result->bindParam(1, $id);
// $id == 4 here
$result->execute();
$row = $result->fetch(PDO::FETCH_ASSOC);
// $row == false > why ?
$thumbs = $row['thumbs'];
When i'm trying to run this on PhpMyAdmin, it works well.
I execute this code on an AJAX call, and using the same config.php file for the $db conection.
Another question:
$sql_in = $db->dbh->prepare("INSERT INTO `voted_ip` (id, ip) VALUES (:id, :ip)");
// $id == 4
$sql_in->bindParam(":id", $id);
$sql_in->bindParam(":ip", $ip, PDO::PARAM_STR);
$sql_in->execute();
it inserts "0" and my ip. Why 0 ?
Please help
That is becasue of the $id which is a STRING is converted at 0 by MySQL.
$id = intval($id);
I have a query, and I want to get the last ID inserted. The field ID is the primary key and auto incrementing.
I know that I have to use this statement:
LAST_INSERT_ID()
That statement works with a query like this:
$query = "INSERT INTO `cell-place` (ID) VALUES (LAST_INSERT_ID())";
But if I want to get the ID using this statement:
$ID = LAST_INSERT_ID();
I get this error:
Fatal error: Call to undefined function LAST_INSERT_ID()
What am I doing wrong?
That's because that's an SQL function, not PHP. You can use PDO::lastInsertId().
Like:
$stmt = $db->prepare("...");
$stmt->execute();
$id = $db->lastInsertId();
If you want to do it with SQL instead of the PDO API, you would do it like a normal select query:
$stmt = $db->query("SELECT LAST_INSERT_ID()");
$lastId = $stmt->fetchColumn();
lastInsertId() only work after the INSERT query.
Correct:
$stmt = $this->conn->prepare("INSERT INTO users(userName,userEmail,userPass)
VALUES(?,?,?);");
$sonuc = $stmt->execute([$username,$email,$pass]);
$LAST_ID = $this->conn->lastInsertId();
Incorrect:
$stmt = $this->conn->prepare("SELECT * FROM users");
$sonuc = $stmt->execute();
$LAST_ID = $this->conn->lastInsertId(); //always return string(1)=0
You can get the id of the last transaction by running lastInsertId() method on the connection object($conn).
Like this $lid = $conn->lastInsertId();
Please check out the docs https://www.php.net/manual/en/language.oop5.basic.php