How to get a column value using MySQLi? [duplicate] - php

This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 5 months ago.
I am trying to get a value from column "odznak" in "users" tab for user "user01" and store it in variable $odznak (for searching in another tab.
$stmt = $conn->prepare("SELECT odznak FROM users WHERE username = 'user01'");
$stmt->execute();
$result = $stmt;
$odznak;

You need to fetch the data (say into an associative array)
On the other hand, as a good practice, please use parameterized prepared statement in your select query
So, change to:
$stmt = $conn->prepare("SELECT odznak FROM users WHERE username = ?");
$stmt->bind_param("s", 'user01');
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$odznak=$row["odznak"];
Now, $odznak is the retrieved data

Related

MySQL LIKE operator in PHP [duplicate]

This question already has an answer here:
Correct way to use LIKE '%{$var}%' with prepared statements?
(1 answer)
Closed 1 year ago.
I should execute this method,
but I don't know how pass %research value as LIKE parameter in bind:
public function researchElements($research) {
$stmt = $this->db->prepare("SELECT * FROM product WHERE product_name LIKE "'%?%'"");
$stmt->bind_param('s', $research);
$stmt->execute();
$result = $stmt->get_result();
$result = $result->fetch_all(MYSQLI_ASSOC);
return $result;
}
At least you can use in your query CONCAT function like next:
$stmt = $this->db->prepare("SELECT * FROM product WHERE product_name LIKE CONCAT('%',?,'%')");
Note: if $research variable gets value '', then query will return all rows from the table.

mysqli bind_result and fetch to PDO PostgreSQL [duplicate]

This question already has answers here:
php pdo get only one value from mysql; value that equals to variable
(3 answers)
Closed 2 years ago.
I am trying to convert my mysqli to PDO using PostgreSQL and I am wondering what is the equivalent way of writing the following code in PDO
$result = $mysqli->prepare("SELECT FOUND_ROWS()");
$result->execute();
$result->bind_result($total_rows);
$result->fetch();
What I tried is the following:
$stmt = $this->pdo->prepare('SELECT FOUND_ROWS()');
$stmt->execute();
but I am not sure how to convert the rest of the my_sqli logic to my new PostgreSQL PDO, in particular the $result->bind_result($total_rows); and fetch().
PDO doesn't have anything equivalent to bind_result(). PDOStatement::fetch() returns the row as an array or object, and you extract the result columns from that. So instead of
$result->bind_result($total_rows);
$result->fetch();
echo $total_rows;
you use
$row = $stmt->fetch(PDO::FETCH_NUM);
echo $row[0];
It's generally easier if you assign aliases to the columns being selected, then you can fetch an associative array.
$stmt = $this->pdo->prepare('SELECT FOUND_ROWS() AS total_rows');
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['total_rows'];

Select from table, where column is foreign key in PHP [duplicate]

This question already has answers here:
mysqli prepared statement num_rows returns 0 while query returns greater than 0 [duplicate]
(3 answers)
Closed 3 years ago.
Even though there is an entry in the database, with this query, I always get 0 entries back
$sql = "SELECT * FROM saved_food WHERE user_id = ? AND favorite_food LIKE ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("is", $me['id'], $favFood);
$stmt->execute();
var_dump($stmt->num_rows);
the dump is 0
The user_id colum is a foreign key, and shows to the id of the table "user".
I can't see the error here.
Is there a special method for foreignkey values?
I got the error... facepalm
I forgot to call ->get_result();
$sql = "SELECT * FROM saved_food WHERE user_id = ? AND favorite_food LIKE ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("is", $me['id'], $favFood);
$stmt->execute();
$result = $stmt->get_result();
var_dump($result->num_rows);

Fetch COUNT DISTINCT data with prepared statements [duplicate]

This question already has answers here:
Can PHP PDO Statements accept the table or column name as parameter?
(8 answers)
Closed 5 years ago.
I have this code to get a COUNT DISTINCT data:
$param = 'email';
$stmt = $conn->stmt_init();
$stmt = $conn->prepare("SELECT COUNT(DISTINCT(?)) FROM contatos");
$stmt->bind_param('s',$param);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($count);
while ($stmt->fetch()) {
echo $count;
}
But echo $count always returns 1, but i have dozens of records...
What is wrong?
Thanks
Binding is not allowed for column names (or table names). Your query is not executing correctly. You need to directly pass the name of the field.
$stmt = $conn->prepare("SELECT COUNT(DISTINCT(email)) FROM contatos");

PDO:: Confusion [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
I have no idea why this is not returning anything. I'll show the code and talk through the steps I've taken.
if (isset($_GET['observation'])) {
require_once("../func/connect.php");
$query = "SELECT * FROM observations WHERE option = ?";
$stmt = $db->prepare($query);
$stmt->bindValue(1, $_GET['observation']);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['question'];
} else {
echo 'nope';
}
$row dumps a false boolean, $row['question'] is null.
I've wrote about a million queries and don't have a clue why this doesn't work.
Database table observations consists of id, question & option and the bindValue is correct to match a string in the database.
However, it returns null.
option is a reserved word in mysql so you need to quote it with backticks:
$query = "SELECT * FROM observations WHERE `option` = ?";

Categories