Getting aggregate function result from php stmt? - php

I am attempting to retrieve the latest id from the DB and it works in my terminal but will not work as a prepared statement.
Here is my code.It all works up to it, inserting and pulling a id if it is not through the max function.
$stmt=$mysqli->prepare("select id from pickup where id in (select max(id) from pickup)");
$stmt->bind_result($id);
$stmt->fetch();
$stmt->close();
$mysqli->close();
return $id;

check
$stmt=$mysqli->prepare("select max(id) as id from pickup");
$stmt->execute();
$stmt->bind_result($id);
$stmt->fetch();
$ret_id = $id;
$stmt->close();
$mysqli->close();
return $ret_id;

Related

MySQL returns only the first character of the fields, but works fine on local

I don't know what is going on exactly, but all only the first character is returned for all my columns when I uploaded my website. It works perfectly fine on a local machine.
I found a similar question here, but I didn't manage to find the answer:
https://stackoverflow.com/questions/10507848/mysql-query-returns-only-the-first-letter-of-strings-only-when-page-is-viewed-on
// Log Table Query
unset($stmt);
$stmt = $db->stmt_init();
$stmt = $db->prepare( "SELECT * FROM logs ORDER BY `id` DESC" );
$stmt->store_result();
$stmt->bind_result($r_id, $r_time, $r_logger, $r_message, $r_category);
$stmt->execute();
while( $stmt->fetch() )
{
var_dump($r_message);
var_dump($r_category);
}
$stmt->close();
This outputs on localhost for example:
string(5) "Hello"String(3) "Cow"
But on the live server:
string(1) "H"String(1) "C"
Any ideas?
Edit
I think that this applies only to string types. The integer types return for example:
int(2893)
I'm assuming that your database or table config is similar to your localhost (better to double check your table).
I noticed one mistake:1. You called store_result() before calling execute(). As per http://php.net/manual/en/mysqli-stmt.store-result.php execute() should be called first.
See my code this might solve your problem:
/* unsetting doesn't matter you're
going to overwrite it anyway */
unset($stmt);
/* you dont need to initialize $stmt with $db->stmt_init(),
$db->prepare() method will create it for you */
$stmt = $db->stmt_init();
$stmt = $db->prepare("SELECT * FROM logs ORDER BY `id` DESC");
/* execute the query first before storing
the result and binding it your variables */
if (!$stmt->execute()) {
echo "query execution error";
exit();
}
/* store the result */
$stmt->store_result();
/* then bind your variables */
$stmt->bind_result($r_id, $r_time, $r_logger, $r_message, $r_category);
/* fetch data and display */
while($stmt->fetch()) {
var_dump($r_message);
var_dump($r_category);
}
$stmt->close();
Let me know if this solved your problem.
Alternatively, you can use the straight forward way since you're not giving any input parameter like WHERE first_name LIKE <input here> to your query:
$result = $db->query("SELECT * FROM logs ORDER BY `id` DESC");
if ($result === false) {
echo "query execution error";
exit();
}
/* You can use either MYSQLI_NUM or MYSQLI_ASSOC os MYSQLI_BOTH
see php.net for more info */
echo "<pre>";
while($line = $result->fetch_array(MYSQLI_NUM)) {
print_r($line);
echo "\n";
}
echo "</pre>";
As suggested in the question you linked, try the code without unset($stmt) and $stmt = db->stmt_init() Instead, you might use free_result()
// Log Table Query
$stmt->free_result(); //Free old results first
$stmt = $db->prepare( "SELECT * FROM logs ORDER BY `id` DESC" );
$stmt->store_result();
$stmt->bind_result($r_id, $r_time, $r_logger, $r_message, $r_category);
$stmt->execute();
while( $stmt->fetch() )
{
var_dump($r_message);
var_dump($r_category);
}
$stmt->close();

MySQL Query not returning a row value in PHP

I don't know why this query won't return a value because when I copy the "echoed" portion into phpmyadmin I do get a record returning:
echo $_GET["cname"];
// Query template
$sql = 'SELECT C.cid FROM `Contact` C WHERE C.email="'.$_GET["cname"].'"';
echo $sql;
// Prepare statement
$stmt = $conn->prepare($sql);
$stmt->execute();
$stmt->bind_result( $res_cid);
echo $res_cid;
$res_cid is apparently 0, but I don't know why because when I paste that query manually into phpmyadmin I do get a value... So why doesn't it return anything?
As already mentioned in the comments - you should make sure your code is secured. You better use the bindparam for that.
As for your question - after you execute your query and bind_result you should also fetch to get the actual value from the database, based on your query:
// Prepare statement
$stmt = $conn->prepare($sql);
$stmt->execute();
$stmt->bind_result( $res_cid);
// Fetch to get the actual result
$stmt->fetch();
echo $res_cid;

How to fetch data from database using prepare statement in php?

I have a database in which I have user_id & associated_id.There can be multiple associated_id for a single user_id. Now I want to fetch all the associated_ids into a single array. I have tried this method but don't know how to get them in array.
$stmt = $this->conn->prepare("SELECT * FROM my_contacts WHERE user_id = ?");
$stmt->bind_param("s", $user_id);
if ($stmt->execute())
{
while ($stmt->fetch())
{
//what to write here
}
//echo var_dump($user);
$stmt->close();
}
Try this:
$stmt = $mysqli->prepare("SELECT associated_id FROM my_contacts WHERE user_id = ?")) {
$stmt->bind_param('s', $user_id); // Bind "$user_id" to parameter.
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
// get variables from result.
$stmt->bind_result($associated_id);
$stmt->fetch();
The results will be stored in the $associated_id array.
You can bind parameters like this and use fetchall method to get all the results in a array
$stmt = $this->conn->prepare("SELECT * FROM my_contacts WHERE user_id = :user_id");
$stmt->bind_param(":user_id", $user_id, PDO::PARAM_INT);
if ($stmt->execute())
{
$result = $stmt->fetchall(PDO::FETCH_ASSOC);
//echo var_dump($user);
$stmt->close();
}
According to your code you used mysqli.
$stmt = $this->conn->prepare("SELECT * FROM my_contacts WHERE user_id = ?");
if($stmt->execute()){
$result = $stmt->get_result();
if($result->nom_rows > 0){
while($row = $result->fetch_assoc()){
var_dump($row)
}
}else{
echo "Sorry NO data found";
}
}else{
echo "Some thing is wrong";
}
here you can't used $stmt->bind_result(); instead of use $stmt->get_result()
$stmt->bind_result(); is only used when you define field in select query
with * you need to used $stmt->get_result()
refer this link for more information
Example of how to use bind_result vs get_result

PHP prepared statement always returns 1

I tryed to call a function from a prepared Statement
stmt= $conn->prepare("SELECT create_user(?,?,?)");
$tt="test";
$stmt->bind_param("sss",$tt,$tt,$tt);
$stmt->execute();
echo "RETURN VALUE".$stmt->fetch();
the returnvalue shouldn't be 1
the function is working if i call it that way directly in mysql console
the function is working correctly and is also applying the changes but the return value is allways 1 no matter what it's returning on the console
I tried to execute another Statement to check if there is a mistake in my function
there are 4 rows in the table
$stmt=$conn->prepare("SELECT Count(Name) from users");
$stmt->execute();
echo $stmt->fetch();
the result is the (it's 1 again) same if i do
$stmt->store_result();
before fetching the result
I'm working with mySQL DB
you have to try something like this:
$stmt=$conn->prepare("SELECT Count(Name) as count from users");
$stmt->execute();
$stmt->bind_result($count);
$stmt->fetch();
echo $count

MYSQLi bind_result is returning null

I am trying to output the variables that I get from the database in my query but nothing is being returned. Using MYSQLi prepared statements.
Please see code below:
$stmt = $con->prepare("SELECT first_name, last_name FROM transactions WHERE order_id = ?");
$stmt->bind_param('i', $order_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($first_name, $last_name);
$stmt->close();
// Output review live to page
echo $first_name;
Where am I going wrong?
You forgot the line to fetch the result. fetch().
Try that:
$stmt->bind_result($first_name, $last_name);
$stmt->fetch(); // ----- > you forget that line to fetch results.
$stmt->close();

Categories