PDO: Like condition doesn't work using bindParam - php

well... why doesn't work this sql statement?
public function searchProfile() {
$termino = $this->term;
$termino = "%".$termino."%";
$sql = "SELECT * FROM cat_perfiles WHERE UPPER(Nombre) LIKE UPPER(:term)";
$result = $this->dbConnect->prepare($sql) or die ($sql);
$result->bindParam(':term',$termino,PDO::PARAM_STR);
$numrows = $result->rowCount();
$jsonSearchProfile = array();
if ($numrows > 0) {
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$jsonSearchProfile[] = array(
'IdPerfil' => $row['Id'],
'NomPerfil' => $row['Nombre'],
'DesPerfil' => $row['Descripcion'],
'EdoPerfil' => $row['Activo']
);
}
$jsonSearchProfile['success'] = 'success';
return $jsonSearchProfile;
} else {
return false;
}
}
I check data from $this->term and is correct! But when compare with LIKE doesn't work.
I hope can help me!

You forgot to execute the query
$result->execute();

Related

Why PHP function is returning Only the one row from mysql

I am fetching data from MYSQL database and Looping through the result but its returning only one row !!! can any one explain what's wrong
function getAll()
{
global $con;
$SQL = "SELECT * FROM users";
$stmt = $con->query($SQL);
if($stmt->num_rows > 0){
while($row = $stmt->fetch_row())
{
return $row;
}
}
}
The return should be outside the while loop !
Here is how you can get all the rows :
function getAll()
{
global $con;
$SQL = "SELECT * FROM users";
$stmt = $con->query($SQL);
if($stmt->num_rows > 0){
$arr = array();
while($row = $stmt->fetch_row())
{
$arr[] = $row;
}
return $arr;
}
}
Or you can do something like this return a generator and loop through :
function getAll()
{
global $con;
$SQL = "SELECT * FROM users";
$stmt = $con->query($SQL);
if($stmt->num_rows > 0){
while($row = $stmt->fetch_row())
{
yield $row;
}
}
}
see the result here !
echo '<pre>';
foreach (getAll() as $value) {
print_r($value);
}
Once you hit a return statement all execution of that function stops. You cannot return multiple times.
You can either return an array of all rows:
function getAll()
{
global $con;
$SQL = "SELECT * FROM users";
$stmt = $con->query($SQL);
if($stmt->num_rows > 0) {
while($row = $stmt->fetch_row())
{
$array[] = $row;
}
return $array;
}
}
Or use it as a generator (probably not what you want):
function getAll()
{
global $con;
$SQL = "SELECT * FROM users";
$stmt = $con->query($SQL);
if($stmt->num_rows > 0){
while($row = $stmt->fetch_row())
{
yield $row;
}
}
}

Sqlsrv query doesn't show any result

SELECT * FROM dbo.Warehouse returns result, but when I change to SELECT * FROM dbo.Accessories, it neither shows any result nor echo 'Rows not found'. Both Warehouse and Accessories tables have rows. Here is my script.
<?php
require('db_connect.php');
$query = 'SELECT * FROM dbo.Warehouse';
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$stmt = sqlsrv_query($conn, $query, $params, $options);
if(!$stmt){
die(print_r(sqlsrv_errors(),true));
}
$c = 0;
$result[] = array();
if(sqlsrv_num_rows($stmt) != 0){
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
$row = array($c => $row);
$result[$c] = $row;
$c++;
}
} else {
echo("Rows not found");
}
echo json_encode($result);
?>
Please help me, thanks in advance. :(

Returning mysqli results outside of function

Given the following code:
function fetchData($mysqli){
$sql = "select * from `test`";
$result = mysqli_query($mysqli,$sql);
return $result;
}
$result = fetchData($mysqli);
while($row = mysqli_fetch_array($result)){
echo $row['id'];
}
My code is obviously more complicated than this. It loops itself until it yields some results changing some variables at each iteration.
$result is empty. What am I doing wrong? Thank you!
FULL CODE:
function fetchItem($itemID, $period, $mysqli){
$periodArray = array('7', '30', '60', '90', '180');
while (current($periodArray) !== $period) next($periodArray);
$currentPeriod = current($periodArray);
$sql = "SELECT * from `test` where `period` = '$period'";
$result = mysqli_query($mysqli,$sql);
$row_count = $result->num_rows;
if($row_count < 5){
$currentPeriod = next($periodArray);
fetchItem($itemID, $currentPeriod, $mysqli);
} else if($row_count >= 5){
$currentPeriod = current($periodArray);
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
// var_dump($rows); <-- returns all results
return $rows;
}
}
$output = fetchItem($itemID, $period, $mysqli);
echo '<pre>';
print_r($output); <-- NULL
echo '</pre>';
As you can see if I don't get results for a given period it moves onto the next one.
Your code should be:
function fetchData($mysqli){
$sql = "select * from `test`";
$init = mysqli_query($mysqli, $sql);
$result = $init->fetch_array(MYSQLI_ASSOC);
return $result;
}
$result = fetchData($mysqli);
foreach($result as $row){
echo $row['id'];
}

SQL Select statement below < operator not working

So for some reason when I use the = operater in my prepared statement there is no problem however when I use the below operator ( < ) it gives an error.
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given.
So I know mysql gives false if there is a problem with my query. however I cant see the problem.
function availableapps($security)
{
if(empty($databasemanager)) { $databasemanager = new databasemanager(); }
$db = $databasemanager->connectionstring();
$result = $db->prepare("SELECT name FROM applications Where security<?");
$result->bind_param("s", $security);
$result->execute();
$types = array();
while(($row = mysqli_fetch_assoc($result->get_result()))) {
$types[] = $row['name'];
}
$result->close();
return $types;
}
Just for demonstration purposes,
final working code:
function availableapps($security)
{
if(empty($databasemanager)) { $databasemanager = new databasemanager(); }
$db = $databasemanager->connectionstring();
$result = $db->prepare("SELECT name FROM applications Where security<?");
$result->bind_param("s", $security);
$result->execute();
$results = $result->get_result();
$types = array();
while($row = $results->fetch_assoc()) {
$types[] = $row['name'];
}
$result->close();
return $types;
}
while ($row = $result->fetch_assoc()) {

How to convert mysqli_fetch_assoc results into json format?

i am trying to convert the result of my query into a json format so i can grap it with jquery in another file. I dont get any errors but its not recognised as json.
$patientquery = mysqli_query($connect, "SELECT * FROM login WHERE assignedTo='$logID'");
$numrows = mysqli_num_rows($patientquery);
if($numrows > 0)
{
while($rows = mysqli_fetch_assoc($patientquery))
{
$dbloginID = $rows['loginID'];
$dbname = $rows['name'];
$result[] = array('patient'=>array('id' => $dbloginID, 'name' => $dbname));
}
}
else
{
$result[] = 'No Patients yet';
}
echo json_encode($result);
please try this:
$patientquery = mysqli_query($connect, "SELECT * FROM login WHERE assignedTo='$logID'");
$numrows = mysqli_num_rows($patientquery);
$result = array();
if($numrows > 0)
{
while($rows = mysqli_fetch_assoc($patientquery))
{
$dbloginID = $rows['loginID'];
$dbname = $rows['name'];
$result['patient'][] = array('id' => $dbloginID, 'name' => $dbname);
}
}
else
{
$result[] = 'No Patients yet';
}
echo json_encode($result);
You should declare $result outside while loop like this
$result = array();

Categories