I need to do something like this:
if (isset($account)) {
$this->db->where('account', $account);
}
if (isset($subject)) {
$this->db->where('subject', $subject);
}
if (isset($contact)) {
$this->db->where('_from', $contact);
}
//here i need to get result
$resul1 = $this->db->get('table');
$limit = 5; $offset =10;
$this->db->limit($limit, $offset);
//here i need to get result with offset and limit
$result2 = $this->db->get('table');
$result1 should execute this query
SELECT *
FROM `table`
WHERE `account` = 'some account'
AND `subject` = 'some subject'
AND `_from` = 'name'
and $result2 should execute this query ($query1 + offset and limit):
SELECT *
FROM `table`
WHERE `account` = 'some account'
AND `subject` = 'some subject'
AND `_from` = 'name'
LIMIT 10, 5
but $result2 execute as a separate query: select * FROM table LIMIT10, 5
is it possible to achieve this or I need to start the query from the begining?
LIMIT Without limit() Function
result = $this->db->get('table', 0, $offset);
Or using manual select:
$query = "SELECT *, COUNT(account) AS total FROM `table` WHERE `account` = ? AND `subject` = ? AND `_from` = ? LIMIT 0, ?";
$result = $this->db->query($query, array($account, $subject, $contact, $offset));
Related
I have two put two condition in the result of a sql query result for the below function:
Table projects has two columns a_name and b_name and I want to exclude result in which b_name has value = xxxxx and value in a_name and b_name are same.
Below is currently used:
public function search_project_name($term)
{
$params = array( ':term' => $term);
$sql = "SELECT * FROM projects WHERE project_name like '%$term%'ORDER BY projects.create_date DESC $limit";
$stmt = parent::query($sql, $params);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
$result[]=$row;
endwhile;
return $result;
}
You could add these condition with a not operator:
SELECT *
FROM projects
WHERE a_name like '%$term%' AND
NOT (a_name = 'xxxx' and a_name = b_name)
Problem part is $term in query so replace $term with :term try updated code:
public function search_project_name($term){
$params = array(
':term'=>$term);
$sql = "SELECT * FROM projects WHERE project_name like '%:term%'ORDER BY projects.create_date DESC $limit";
$stmt = parent::query($sql, $params);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
$result[] = $row;
endwhile;
return $result;
}
One more missing parameter is $limit you used in query but not passed to function?
With $limit as 2nd parameter default set to 10 rows:
public function search_project_name($term, $limit = 10){
$params = array(
':term'=>$term);
$sql = "SELECT * FROM projects WHERE project_name like '%:term%'ORDER BY projects.create_date DESC $limit";
$stmt = parent::query($sql, $params);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
$result[] = $row;
endwhile;
return $result;
}
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 can i use letters instead of numbers when selecting data from MySql?
This is working:
<?php
$page = '1001';
$result = mysqli_query($con,"SELECT * FROM `comment` WHERE `page` = $page LIMIT 1001");
?>
but not if $page = 'page_one'.. The row 'page' is VARCHAR (30)
EDIT - trying to clarify question:
This is what i would like to do, but its not working - gives an error:
$page = 'page_one';
$result = mysqli_query($con,"SELECT * FROM `comment` WHERE `page` = $page LIMIT page_one");
You should really use prepared statements:
$page = 'abcd';
$stmt = $con->prepare("SELECT * FROM `comment` WHERE `page` = ? LIMIT 1001"));
$stmt->bind_param("s", $page);
$stmt->execute();
Simply add ' around your parameter when its a string and not an integer:
$page = "somerandomtext";
SELECT * FROM `comment` WHERE `page` = '$page' LIMIT 1001
What he meant was:
<?php
$page = '1001';
$result = mysqli_query($con,"SELECT * FROM `comment` WHERE `page` = '$page' LIMIT 1001");
?>
That way the Query String will contain the ' characters..
I need to generate a random row from my MySql database and i have found example here:
http://akinas.com/pages/en/blog/mysql_random_row/
And i want to use solution 3 which looks like this:
$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users` ");
$offset_row = mysql_fetch_object( $offset_result );
$offset = $offset_row->offset;
$result = mysql_query( " SELECT * FROM `users` LIMIT $offset, 1 " );
My code looks like this now:
$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users` ");
$offset_row = mysql_fetch_object( $offset_result );
$offset = $offset_row->offset;
$result = mysql_query( "SELECT * FROM `users` WHERE profile_image='2' LIMIT $offset, 1 " );
$random_date = mysql_fetch_array($result);
echo $random_date['user_name']; //display username of random user
But when i refresh the page: approximatly 7 of 10 times nonthing shows up. No username at all and also im trying to print out the id of the user but it's also empty.
It seems that it's not getting anything at all from the database when refreshing and sometimes it get's data from the database. Any idea why this might happen?
In this particular case, the problem is that you're working with two different queries; the first query is:
SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users`
Whereas the second one includes a condition:
SELECT * FROM `users` WHERE profile_image='2' LIMIT $offset, 1
^^^^^^^^^^^^^^^^^^^^^^^
Both queries should operate over the same result set.
Its because in your first query you dont have a where, but in the second query you do.
if you have 50 rows and only 15 with profile_image = 2 That will be the reason why most appear as nothing.
Your query becomes LIMIT 30,1 when there are only 15 results for example
Make sure the same where is used in both queries.
Also avoid using mysql_* functions in new code
The solution for my problem was this, BIG thanks to: #Jack
This query will find random row in a mysql table very fast.
$gender_search = $logged_in_user['gender_search'];
$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users` WHERE profile_image='2' AND gender='$gender_search'");
$offset_row = mysql_fetch_object( $offset_result );
$offset = $offset_row->offset;
$result = mysql_query( "SELECT * FROM `users` WHERE profile_image='2' AND gender='$gender_search' LIMIT $offset, 1 " );
$random_date = mysql_fetch_array($result);
I have now converted this string to PDO if someone needs it.
$statement = $dbConn->prepare("SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users` WHERE profile_image=? AND gender=?");
$statement->execute(array('2',$logged_in_user['gender_search']));
$offset_row = $statement->fetch(PDO::FETCH_OBJ);
$offset = $offset_row->offset;
$statement2 = $dbConn->prepare("SELECT * FROM `users` WHERE profile_image=? AND gender=? LIMIT ?, 1");
$statement2->execute(array('2', $logged_in_user['gender_search'], $offset));
$random_date = $statement2->fetch(PDO::FETCH_BOTH);
I have a server list, and first of all I want to order the servers by the most online users,
but in case that there is a server that is currently in progress + has the most players online at the moment, I want to ignore it.
Basically order by PLAYERS COUNT + status = available, but it doesn't work?
$query = $this->controller->db->fetch("argonite_servers", null, null, "server_status = 'Available', server_players");
And this is my method:
public function fetch($table, array $criteria = null, $limit = null, $order = null)
{
// The query base
$query = "SELECT * FROM $table";
// Start checking
if ($criteria) {
$query .= ' WHERE ' . implode(' AND ', array_map(function($column) {
return "$column = ?";
}, array_keys($criteria)));
}
// limit
if ($limit)
{
$query .= " LIMIT ". $limit;
}
//order
if ($order)
{
$query .= " ORDER BY ".$order." DESC";
}
$check = $this->pdo->prepare($query) or die('An error has occurred with the following message:' . $query);
if ($criteria)
$check->execute(array_values($criteria));
else
$check->execute();
return $check;
}
Why won't it display all servers whom are available + has the most players at the top?
Right now, my table shows this:
(source: gyazo.com)
;
What did I do wrong?
You're where clause is probably select * from argonite_servers where server_status = 'Available' order by server_players desc; It looks like you've merged your order and criteria clauses in your method call instead of keeping them separate.
So instead of
$query = $this->controller->db->fetch("argonite_servers", null, null, "server_status = 'Available', server_players");
You might want:
$query = $this->controller->db->fetch("argonite_servers", "server_status ='Available'", null, "server_players");
EDIT:
To keep the 'available' status showing you are lucky that 'Available' is alphabetically sorted ahead of so do this:
$query = $this->controller->db->fetch("argonite_servers", null, null, "server_status ASC, server_players");