does not work request - php

What am I doing wrong in my query?
thank you in advance for your help
new - not work
$query = "SELECT * ";
$query .= "FROM photographs ";
$query .= "WHERE `caption` LIKE '%".$query."%' ";
$query .= "OR `caption2` LIKE '%".$query."%' ";
//$query .= "WHERE visible = 1 ";
$query .= "ORDER BY $order_by LIMIT $start, $display ";
$result = mysqli_query ($connection, $query);
old query - work
//$query = ("SELECT * FROM photographs WHERE (`caption` LIKE '%".$query."%') OR (`caption2` LIKE '%".$query."%')");
//$result = mysqli_query($connection, $query);

You are overwriting the $query variable with parts of your query. :-)

LIKE '%".$query."%' ";
should be replaced with
LIKE '%".$yourTerm."%' ";
where $yourTerm is what you are trying to search in your database

Related

print this query out put in json format to use in php webservices

i have written this query in php file to get data from database ,it is working fine and getting required data .
but how to print the retrieved data in json fromat for using web services
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY position ASC";
$result = mysqli_query($connection, $query);
$subject = mysqli_fetch_assoc($result);
print_r($subject);
what code has to be done please help me.
Have you tried json_encode? Like:
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY position ASC";
$result = mysqli_query($connection, $query);
$subject = mysqli_fetch_assoc($result);
echo json_encode($subject);
For more information you can check: PHP Manual
try this :
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY position ASC";
$result = mysqli_query($connection, $query);
$subject = mysqli_fetch_assoc($result);
header('Content-Type: application/json');
echo json_encode($subject);

Step by step MySql query to PDO

I'm trying to figure out how can I build a query in PDO like this one
//...
$sql = array();
$sql[] = "SELECT * FROM `posts` WHERE `completed` = '1'";
if($this->is($_GET, 'category')) {
$sql['category'] = "AND `category` = '".$_GET['category']."'";
}
if($this->is($_GET, 'tags')) {
$sql['tags'] = "AND `tags` LIKE '%".$_GET['tags']."%'";
}
$sql[] = "ORDER BY `id` DESC LIMIT ".$offset.", ".$rows_per_page;
$query = $this->query(implode(" ", $sql));
//...
I tried something like that..
$sql = array();
$sql[] = "SELECT * FROM `posts` WHERE `completed` = :completed";
if($this->is($_GET, 'category')) {
$sql['category'] = "AND `category` = :category";
}
$sql[] = "LIMIT 0, 5";
$this->db->query(implode(" ", $sql));
$this->db->bind(array(
':completed' => 1,
':category' => $this->is($_GET, 'category')
));
$fetch = $this->db->fetchAll();
print_r($fetch);
but there's a error that says I can not bind nonexistent variables "SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens"
...and with some research I figure out I can not bind before query
..so.. do you have any idea how can I do this?

How to convert this mySql query to PDO format...?

How should i write my PDO Prepare and bindValue/param statement for this type of query where i check whether the value is not null then only add it to the query string.....
$query = "SELECT * FROM cabs WHERE DATE='$date' ";
if ($mode!=='' || $mode!=="")
$query .="AND MODE='$mode' ";
if ($tfno!=='')
$query .="AND TFNO='$tfno' ";
$query .="ORDER BY TIME";
Quick answer, without testing:
<?php
$params = array(':date' => $date);
$query = "SELECT * FROM cabs WHERE DATE=':date' ";
if ($mode!=='' || $mode!=="") {
$query .="AND MODE=':mode' ";
$params[':mode'] = $mode;
}
if ($tfno!=='') {
$query .="AND TFNO=':tfno' ";
$params[':tfno'] = $tfno;
}
$query .="ORDER BY TIME";
$req = $dbh->prepare($query);
$req->execute($params);
Just push in the param array each time your query gets more filters, and using a name should be easier, I'm not sure that array_push would preserve the order, so ..

my PHP MySQL syntax is too long

Thank you, very nice solution, that was answered my question.
How can I use your solution if my $sql is as follows:
$sql = "SELECT * FROM $tbl_main, $tbl_country, $tbl_members
WHERE ($tbl_main.country_id = $tbl_country.country_id) AND ($tbl_main.member_id = $theselect) ORDER BY $chosenTable.$orderby LIMIT $startpoint, $limit";
i.e. how can I use your solution somewhere in the middle of a code.
You can conditionally concatenate the extra condition:
$sql = "SELECT * FROM $tbl_main, $tbl_country, $tbl_members
WHERE ($tbl_main.country_id = $tbl_country.country_id)";
if(isset($theselect)) {
$sql .= " AND ($tbl_main.member_id = $theselect)";
}
$sql .= " ORDER BY $chosenTable.$orderby LIMIT $startpoint, $limit";

MySQL Query for Matching Items Help

I'm having a little trouble getting this query to work:
$userId = mysql_real_escape_string( $_SESSION['user_id'] );
$userPassProvided = mysql_real_escape_string( $_POST['oldPassword'] );
$query = "SELECT user_id, AES_DECRYPT( user_pass, '".$db_aes_key."' ) AS user_pass ";
$query .= "FROM users_tbl WHERE MATCH( user_id, user_pass ) ";
$query .= "AGAINST( '".$userId."', '".$userPassProvided."' IN BOOLEAN MODE ) LIMIT 1";
$result = mysql_query( $query, $mysql_db );
What I would like to do is query users_tbl for the record wherein user_id and user_pass are the same as $userId and $userPassProvided, respectively. Can someone please tell me what is wrong with my query?
Thanks. :)
The following is functionally equivalent to what you seem to want to do. (Do read "however..." below)
$query = "SELECT user_id, AES_DECRYPT( user_pass, '".$db_aes_key."' ) AS user_pass ";
$query .= "FROM users_tbl ";
$query .= "WHERE user_id = '".$userId."' ";
$query .= " AND AES_DECRYPT(user_pass, '".$db_aes_key."' ) = '".$userPassProvided."' ";
$query .= "LIMIT 1";
...however MySQL would have to AES-decript every single encoded password in the database. This will be both computationally expensive and prevent using any SQL index.
Alternatively, you may consider encrypting the supplied password, and match it to the ones stored in the database. Maybe something like that (note: untested):
$query = "SELECT user_id, AES_DECRYPT( user_pass, '".$db_aes_key."' ) AS user_pass ";
$query .= "FROM users_tbl ";
$query .= "WHERE user_id = '".$userId."' ";
$query .= " AND user_pass = AES_ENCRYPT('".$userPassProvided."', '".$db_aes_key."' ) ";
$query .= "LIMIT 1";
MATCH () AGAINST () doesn't work like you're expecting it to. What it does is attempts to match a single string in AGAINST() against each of the columns provided in MATCH(), rather than comparing value1 against column1 and value2 against column2.
Have you tried ...WHERE user_id = '".$userId."' AND user_pass = '"$userPassProvided"' LIMIT 1?

Categories