I have a variable called $description that has a paragraph of information in it. Some of these descriptions are a sentence or 2, some are long, so im using blobs to save this instead of var char. This statement executes without a problem, but nothing actually gets saved. No errors reported.
$query = "UPDATE event SET description=? WHERE id=? LIMIT 1";
if($stmt = $db -> prepare($query))
{
$null = NULL;
$stmt -> bind_param("bi", $null, $id);
$stmt -> send_long_data(0, $description);
$stmt -> execute();
}
Is there something im missing?
Instead of binding b as blob, try to refer to s as string
Related
Why does this return no MySQL rows:
$sql = $conn -> prepare("select * from table where id = ?");
$sql -> bind_param('i', $array[0]);
$array = array(1);
$sql -> execute();
But when I put the array before bind_param it's working. It works like this:
$sql = $conn -> prepare("select * from table where id = ?");
$array = array(1);
$sql -> bind_param('i', $array[0]);
$sql -> execute();
Or
$array = array(1);
$sql = $conn -> prepare("select * from table where id = ?");
$sql -> bind_param('i', $array[0]);
$sql -> execute();
mysqli_statement::bind_param($types, &$var1) accepts variables by reference. So, it's intended to deal with variables that are not defined yet.
And normally you can do like this
$sql = $conn -> prepare("select * from table where id = ?");
$sql -> bind_param('i', $i);
$i = 1;
$sql -> execute();
and it would work flawless.
However, an array is another matter. Like it is noted my #mario in the comment, Referencing an undeclared variable automatically defines it. So, when you reference a variable (and here $sql->bind_param('i', $array[0]); you are effectively doing it), both $array and $array[0] would be created. Where $array[0] is a reference.
Were you assigning a value to this variable, i.e $array[0] = 1;, it would have worked.
But you assigned an brand new value to the entire array. As a result, it now contains not a reference, but a new value.
The above can be illustrated with a simple code snippet
As you can see, as long as you retain the original array member, the reference remains.
But as soon as you assign a brand new value to the entire array, the reference is gone!
But it's gone only from the array, but not from the function. There it still points to that odd &NULL value. That's why you cannot get any result.
shared my thoughts in the comments
$sql = $conn -> prepare("select * from table where id = ?");
$sql -> bind_param('i', $array[0]);// undefined since $array is not yet defined
$array = array(1);// you've just defined it here
$sql -> execute();
$sql = $conn -> prepare("select * from table where id = ?");
$array = array(1);// you have defined it first
$sql -> bind_param('i', $array[0]);// PHP will know $array, since you've defined it in the row above
$sql -> execute();
hope this helps!
if there's something unclear, please let me know!
I have a database with the following columns: id, name, zone.
I need the function to return the name of the record that contains the zone that arrives as a parameter
static public function mdlShowName($table, $zone){
$stmt = Conection::conect()->prepare("SELECT name FROM $table WHERE zone = :$zone");
$stmt -> bindParam(":name", name, PDO::PARAM_STR);
$stmt -> execute();
return $stmt -> fetch();
$stmt-> close();
$stmt = null;
}
The parameter placeholder is not a variable. Don't use $zone, just give it a label.
$stmt = Conection::conect()->prepare("SELECT name FROM $table WHERE zone = :zone");
The name by which you bind the parameter must be the same as the label you used as a placeholder in the query. Then bind it to the PHP variable that has the value.
Don't bother with PDO::PARAM_STR or other param types. The MySQL PDO driver ignores these anyway. They might be more important if you use some other brand of RDBMS (Oracle, Microsoft, etc.).
You don't need the : in the parameter name here.
$stmt -> bindParam("zone", $zone);
An alternative is to just pass an array to execute(). If you do this, then skip the bindParam() calls.
$stmt -> execute( ["zone" => $zone] );
Tip: This is all explained in the documentation!
I am confused with the way lastInsertId() function is written. Say for example I have the following queries with lastInsertId() function.
$myinsert = $pdo->prepare("INSERT INTO some_table(something)VALUE(:something");
$myinsert -> bindValue(':something', $something);
$myinsert -> execute();
$insert = $pdo->prepare("INSERT INTO table(something)VALUE(:something");
$insert-> bindValue(':something', $something);
$insert-> execute();
$lastId = $pdo->lastInsertId();
$stmt = $pdo->prepare("INSERT INTO another_table(something)VALUE(:something");
$stmt -> bindValue(':something', $something);
$stmt -> execute();
Now the confusion is that as everyone knows and can see here that in the satement $lastId = $pdo->lastInsertId(); there is no where mentioned whether to fetch the last inserted ID from $myinsert query or $insert query or from $stmt query. So how does it know where to fetch the ID from? Since, lastInsertId() function is placed above the $stmt query, it definitely will not fetch last inserted id from $stmt query as when the $lastid was declared $stmt query was not yet executed. But how does it know it has to fetch from $insert query and not from $myinsert query as in whole statement $lastId = $pdo->lastInsertId(); there is nothing defined like to fetch from so and so particular query? Please help me understand the way it works.
It will just give you the insert ID from the last insert prior to making the call which generates an auto-increment value. It could be that each insert will generate one, but it will only be the last one executed prior to this call.
I'm trying to get data from a MySQL database using PDO. For example, I would run blixUserGetInfo("2767207") if the user's id was 2767207.
But, whenever I run it with a User ID other than 0, blixUserGetInfo("0"), null is returned, and no errors are thrown, even though the supplied ID does exist in the database.
function blixUserGetInfo($userid){
$connection = //database connection
// replacing this with
// "SELECT * FROM `users` WHERE `id` = $userid"
// works for some reason
$statement = "SELECT * FROM `users` WHERE `id` = ?";
$prepared = $connection->prepare($statement);
$prepared->bindValue(1, $userid); //returns TRUE
$prepared->execute(); //also returns TRUE
$code = $prepared->errorCode(); //returns '00000' (no error)
return $prepared->fetch(); //returns null
}
But, If I change $statement from
"SELECT * FROM `users` WHERE `id` = ?"
to
"SELECT * FROM `users` WHERE `id` = $userid"
and remove the bindValue statement, it works as expected.
I've also tried changing the bindValue statement to bindValue(1, $userid, PDO::PARAM_INT), but still have no luck. Both of the times the statement returns true.
Why doesn't the first example work like it should? Is it a bug, am I doing something wrong, or is it expected behavior?
I really dislike this bindValue and bindParam complexity unless it's absolutely necessary. You can simplify your code using parameters for execute
$statement = "SELECT * FROM `users` WHERE `id` = ?";
$prepared = $connection->prepare($statement);
$prepared->execute(array($userid));
While i know this does not answer why bindValue is not working, it takes away having to worry about pass by reference and pass by value stuff which could be a factor in this question.
If this execute does not return an error and your table does have data for that condition than its almost surely going to return it.
Although i didn't confirm it but your act of setting the connection to null before you fetch might be the actual reason you fail to see any data. Why would you set the connection to NULL and try to fetch afterwards ?
I suggest you just not use the bindValue() function anymore.
The bindParam() function works better in this case. Use the value placeholder instead of ?'s (:[placeholder name])
$statement = "SELECT * FROM `users` WHERE `id` = :id";
$prepared = $connection->prepare($statement);
$prepared -> bindParam(":id", $value)
$prepared -> execute();
I'm trying to get to grips with mysqli but finding it a struggle compared to the now depreciated mysql. So far with the old methods I've been able to get information back about my tables in an associative array. I'm trying to form a prepared statement and echo the id number back. I would also like to be able to print the whole sql statement that has been binded, but seen as I can't even echo the id number from a single SELECT statement, it is out of the question at the moment.
$db = new mysqli('xxx', 'xx', 'xx', 'xxxx');
$sql = "SELECT user_id, name FROM users WHERE name=?"
$statement = $db -> prepare($sql);
$statement -> bind_param("s", "Emma");
$statement -> execute();
$statement -> bind_result($id, $name);
$output = $statement -> fetch();
echo $output -> $id . " " . $name;
I seem to be getting lost at the line bind_result. I figured if statement is an object, then I should be able to echo them in the form I have devised? When I refresh my page I just get nothing. I have 2 entries in my table and 1 of them does have the name string that is used above.
You think too complex. Just try this:
$db = new mysqli('xxx', 'xx', 'xx', 'xxxx');
$sql = "SELECT user_id, name FROM users WHERE name=?";
$statement = $db->prepare($sql);
$statement->bind_param("s", "Emma");
$statement->execute();
$statement->bind_result($id, $name);
while ($statement->fetch()) {
echo $id . " " . $name;
}
The bind_result() methods takes care that for each $statement->fetch() you execute you get fresh values in the variables $id and $name.
You should take a look at the good documentation of those methods.