How to pass any value in parameterised query in mysqli? [duplicate] - php

This question already has answers here:
MYSQLI prepared statement bind_param types does not work
(2 answers)
Closed 3 years ago.
Objective: to pass any value in where phrase of mysqli parameterised query
code:
<?php
$name="%%";
$age="%%";
$name_op=null;
$age_op=null;
require_once("dbc.php");
$query="SELECT name, age from student WHERE name LIKE ? AND age LIKE ?";
$stmt=mysqli_prepare($dbc, $query);
mysqli_bind_param($stmt, "si", $name, $age);
mysqli_stmt_execute($stmt);
mysqli_bind_result($stmt, $name_op, $age_op);
while(mysqli_bind_fetch($stmt)){
echo "name : $name_op age: $age_op";
}
mysqli_stmt_close($stmt);
mysqli_close($dbc);
?>
Observation:
In mysql prompt,
mysql> SELECT name, age from student WHERE name LIKE "%%" AND age LIKE "%%";
shows all the records.
But, the above php code doesn't display any record.
Please help me in passing any value in parameterised query.

Under "any" value you are assuming any string value. Hence, you must use the correct type: s, not i for the $age variable.
<?php
require_once("dbc.php");
$name="%%";
$age="%%";
$query = "SELECT name, age from student WHERE name LIKE ? AND age LIKE ?";
$stmt = $dbc->prepare($query);
$stmt->bind_param("ss", $name, $age);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()){
echo "name : $row[name] age: $row[age]";
}

Related

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

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

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);

Prepare select statement in php [duplicate]

This question already has answers here:
Can I bind an array to an IN() condition in a PDO query?
(23 answers)
MySQLi Bind Param with an array for IN [duplicate]
(2 answers)
Closed 5 years ago.
I can not figure out how I can prepare my select statement.
$query = "SELECT name, art FROM table_one WHERE name LIKE ? AND art IN ?";
if ($stmt = $db_link->prepare($query)) {
$stmt->bind_param("ss", $name, $art);
$stmt->execute();
if ($stmt->errno){
//Deal with error
}
$name = "%Marc%";
$art = "('green', 'blue', 'red')";
$stmt->execute();
$stmt->bind_result($name, $art);
while ($stmt->fetch()){
//Output data
}
}
So the problem is, that something does not work with the syntax in the prepared statement. This is my first attempt at preparing statements.
I had the query working before without using a prepared statement, but I am forced to use that now.
The old query looked like this:
$query = "SELECT name, art FROM table_one WHERE name LIKE '%$name%' AND art IN ('$art')";
Thank you for your help.

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");

Echo or print a value from sql table using PHP PDO [duplicate]

This question already has answers here:
return one value from database with mysql php pdo
(3 answers)
Closed 6 years ago.
I am trying to echo or print the last value of column usercode using PHP PDO. I tried to do this by using name column and SESSION var which will be the last values as references, but it doesn't work.
$name = $_SESSION['name'];
$query = $db->prepare("SELECT usercode from users where name = $name ");
$query->execute();
$result = $query->setFetchMode(PDO::FETCH_ASSOC);
echo $result;
Here you go:
$name = $_SESSION['name'];
$query = $db->prepare("SELECT usercode from users where name=:name");
$query = $db->bindParam(':name', $name);
$query->execute();
$row = $query->fetch();
echo $row['usercode'];
bindParam is used when you just want to bind a variable reference to a parameter in the query.

Categories