This is returning false - php

if($stmt = $this->Sys->db->prepare("INSERT INTO dj_videos (title, url, caption) VALUES (?, ?, ?)")) {
$stmt->bind_param('sss', $title, $url, $comment);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
if($stmt->num_rows == 1) {
return TRUE;
}
else {
return FALSE;
}
This is returning false but it inserts the information into the database everytime. It blows my mind.

mysqli_stmt::execute Returns TRUE on success or FALSE on failure.
So you only need to return the result of it:
if($stmt = $this->Sys->db->prepare("INSERT INTO dj_videos (title, url, caption) VALUES (?, ?, ?)")) {
$stmt->bind_param('sss', $title, $url, $comment);
return $stmt->execute();
}
By the way, if the statement is UPDATE, DELETE, or INSERT, the total number of affected rows can be determined by using the mysqli_stmt_affected_rows() function.

num_rows returns the number of rows in a statement result set.
You want affected_rows, which returns the number of rows changed, deleted, or inserted by the last executed statement
$stmt->bind_param('sss', $title, $url, $comment);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
if($stmt->affected_rows == 1) {
return TRUE;
}
else {
return FALSE;
}

Related

MySql Insert returns null

i have the following function:
public function insertMomentToDB($uid, $type, $data)
{
$stmt = $this->conn->prepare("INSERT INTO moments_newsfeed(user_id, type, time, data) VALUES(?, ?, NOW(), ?)");
$stmt->bind_param("sss", $uid, $type , $data);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result)
{
return true;
}
else
{
return false;
}
}
where $data = json undecoded, i want to put it in json format inside the table
$result is always null so it returns false..

Function won't select data from database

I'm working on a new website at the moment and I have a bit of a problem.
I look if an item exist, if it does, it will execute an insert query. The insert query works but the problem is that the select query doesn't work
Class
public function doubt($event_id)
{
$exist = $this->conn->prepare("SELECT * FROM agenda WHERE id=?");
$exist->bind_param('s', $event_id);
$exist->execute();
$exist->get_result();
if($exist->num_rows != null)
{
$now = new DateTime();
$date = $now->getTimestamp();
$status = 2;
$stmt = $this->conn->prepare("INSERT INTO absence (user_id,event_id,status,ip,date) VALUES (?, ?, ?, ?, ?) ");
$stmt->bind_param('sssss', $_SESSION['user_session'], $event_id, $status, $_SERVER['REMOTE_ADDR'], $date);
$stmt->execute();
$stmt->close();
return true;
}
else
{
return false;
}
}

convert string to integer in php pdo

How do I convert string data type into integer with php pdo? I have tried using PDO::PARAM_INT and PDO::PARAM_STR but return false and error when I tried to insert data to postgresql. Is there any way to solve this?
here the codes :
function create(){
// to get time-stamp for 'created' field
$this->getTimestamp();
//write query
$query = "INSERT INTO
" . $this->table_name . "
(id_barang, nama_barang, harga_beli, harga_jual, created) VALUES (?, ?, ?, ?, ?)";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(1, $this->id_barang, PDO::PARAM_STR);
$stmt->bindParam(2, $this->nama_barang, PDO::PARAM_STR);
$stmt->bindParam(3, $this->harga_beli, PDO::PARAM_INT);
$stmt->bindParam(4, $this->harga_jual, PDO::PARAM_INT);
$stmt->bindParam(5, $this->created);
if($stmt->execute()){
return true;
}else{
return false;
}
}
it return false and i'm using alert if function create() false.
harga_beli and harga_jual are integer data type in postgresql. in html form i'm using javascript function to separate the zero. example : 60.000, and the numbers should be an integer on database.
You can use intval function :
echo intval('042'); // 42
You can leave it up to PDO to do the conversion
If execute is false check for errors
The proper way to know if it is successful is to check the affected row
$stmt->bindParam(1, $this->id_barang);
$stmt->bindParam(2, $this->nama_barang);
$stmt->bindParam(3, $this->harga_beli);
$stmt->bindParam(4, $this->harga_jual);
$stmt->bindParam(5, $this->created);
$stmt->execute() or die('error:'.$this->conn->errorInfo());
if($stmt->rowCount() > 0){
return true;
}else{
return false;
}

Insert_id fails to return a value

Can anyone see what is wrong with the code below? For some reason i can get it to return the id of the affected row..in fact it doesnt return anything but the query runs fine and the record is created in the db...
$stmt = $this->db->stmt_init();
if($stmt->prepare('INSERT INTO Assets(id,assetName,type, username, password, mail) VALUES (?,?,?,?,?,?)'))
{
$stmt->bind_param("isssss", $id, $assetName, $type, $username, $password, $mail);
$stmt->execute();
$stmt->close();
return $stmt->insert_id;
}
else
{
$stmt->close();
return "";
}
You are closing the statements before trying to get the insert id:
$stmt->close();
return $stmt->insert_id;

Possible to use multiple/nested MySQLi statements?

Is it possible to have a MySQLi prepared statement within the fetch() call of a previous statement? If not, what's the best way around it?
Example code:
if($stmt = $link->prepare("SELECT item FROM data WHERE id = ?")) {
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($item);
while( $stmt->fetch() ) {
/* Other code here */
$itemSummary = $item + $magic;
if($stmt2 = $link->prepare("INSERT INTO summaries (itemID, summary) VALUES (?, ?)")) {
$stmt2->bind_param("is", $itemID, $itemSummary);
$stmt2->execute();
$stmt2->close();
}
}
}
This is the single connection way:
if($stmt = $link->prepare("SELECT item FROM data WHERE id = ?")) {
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->store_result(); // <-- this
$stmt->bind_result($item);
while( $stmt->fetch() ) {
/* Other code here */
$itemSummary = $item + $magic;
if($stmt2 = $link->prepare("INSERT INTO summaries (itemID, summary) VALUES (?, ?)")) {
$stmt2->bind_param("is", $itemID, $itemSummary);
$stmt2->execute();
$stmt2->store_result(); // <-- this
/*DO WHATEVER WITH STMT2*/
$stmt2->close();
}
}
}
You should be able to do that, although you make have to start a second connection.
Or use store_result.

Categories