I am trying to create a query using multiple WHERE conditions, both AND and NOT, and I am having trouble. Both conditions work separately, but not together. Most likely I've gotten the syntax wrong.
Here's the query
$getposts = mysql_query("SELECT * FROM Posts WHERE (category=".$_POST['category'].") AND (id NOT IN ( '" . implode($array, "', '") . "' )) ORDER BY popularity DESC") or die(mysql_query());
I've tried with and without parentheses, but with no change. As I say, both this query:
$getposts = mysql_query("SELECT * FROM Posts WHERE category=".$_POST['category']." ORDER BY popularity DESC") or die(mysql_query());
and this query:
$getposts = mysql_query("SELECT * FROM Posts WHERE id NOT IN ( '" . implode($array, "', '") . "' ) ORDER BY popularity DESC") or die(mysql_query());
work, just not when combined.
Any help is much appreciated!
Related
I am new in CodeIgniter, I want to count all rows from database table but i use limit in query and i want all count without use limit how can i do ?
my code is below :
$sql = " SELECT intGlCode,fkCategoryGlCode,'C' as acctyp,varEmail,varContactNo as phone,CONCAT(varFirstName,' ',varLastName) as name,dtCreateDate,chrStatus,varMessage as message
FROM " . DB_PREFIX . "Customer WHERE varEmail='$userEmail'
UNION
SELECT intGlCode,'' as fkCategoryGlCode,'P' as acctyp,varEmail,varPhoneNo as phone,varName as name,dtCreateDate,chrStatus,txtDescription as message FROM
" . DB_PREFIX . "Power WHERE varEmail='$userEmail' ORDER BY intGlCode DESC
LIMIT $start, $per_page ";
$query = $this->db->query($sql)
i use limit for pagination but i want to get all record from table.
You can add new column in both above and below UNION queries. It will be like below.
select (select count(*) from your_query), your_columns from query_above_union
UNION
select (select count(*) from your_query), your_columns from query_below_union
your_query = your full actual query your are using currently.
Although I am not sure about Codeigniter. But sure about SQl.
* If you count all records with all data including limit, than you can use this code. please check it. I hope it will works for you.*
$countsql = " SELECT intGlCode,fkCategoryGlCode,'C' as acctyp,varEmail,varContactNo as phone,CONCAT(varFirstName,' ',varLastName) as name,dtCreateDate,chrStatus,varMessage as message
FROM " . DB_PREFIX . "Customer WHERE varEmail='$userEmail'
UNION
SELECT intGlCode,'' as fkCategoryGlCode,'P' as acctyp,varEmail,varPhoneNo as phone,varName as name,dtCreateDate,chrStatus,txtDescription as message FROM
" . DB_PREFIX . "Power WHERE varEmail='$userEmail' ORDER BY intGlCode DESC";
$sql = $countsql. " LIMIT $start, $per_page";
$totalRecords = $this->db->query($countsql);
$result["total_rows"] = $totalRecords->num_rows();
$query = $this->db->query($sql);
$result["list"] = $query->result_array();
In my sql table i had more then 300000 entries.
$marketname = more then 200 !
with this select i can see the last 15 entries.
$sql = "SELECT price FROM markets where market = '" . $marketname . "' order by time desc LIMIT 15,1";
$sql = "SELECT price FROM markets where market = '" . $marketname . "' order by time desc LIMIT 15,1";
and so on for the last 30, 60, 120.
$sql = "SELECT price FROM markets where market = '" . $marketname . "' order by time desc LIMIT 30,1";
$sql = "SELECT price FROM markets where market = '" . $marketname . "' order by time desc LIMIT 60,1";
But is there a combination of all, because when i show it on one page its very very slow with 200 markets!!
like..
$sql = "SELECT price FROM markets where market = '" . $marketname . "' order by time desc LIMIT 15,1 AND LIMIT 30,1 AND LIMIT 60,1 AND LIMIT 120,1";
First of all, try to not do SQL injection style SQL. Use PDO or MySQLi with prepared statements. With that in mind, do not limit your query's last part.
SELECT price FROM markets WHERE market = :marketName ORDER BY time DESC limit 100
Would query the price of the market by marketname (prep statement), order it and limit it to 100 records.
I am generating the first part of the query like this:
while ($all_products = $db->fetch_array($all_prods))
{
$filter_string .= 'AND product_id !=';
$filter_string .= $all_products['item_id'];
$filter_string .= ' ';
}
and then the second part like this:
$sql_more_items = $db->query("SELECT * FROM db_products
WHERE owner_id='" . $user_id . "' AND active=1 '" . $filter_string . "'
ORDER BY RAND() LIMIT 10");
However it's giving me a mySQL syntax error and the $filter_string part strangely adds ' twice before and after the string, so it runs like this:
WHERE user_id='12345' AND active=1 'AND product_id !=0001 AND product_id !=0002 ' ORDER BY RAND ...
What am I doing wrong?
$filter_string adds ' because you put it there. :P
Try with just the double quotes around $filter_string:
$sql_more_items = $db->query("SELECT * FROM db_products WHERE owner_id='" . $user_id . "' AND active=1 " . $filter_string . "ORDER BY RAND() LIMIT 10");
$sql_more_items = $db->query("SELECT * FROM db_products
WHERE owner_id='" . $user_id . "' AND active=1 '" . $filter_string . "'
ORDER BY RAND() LIMIT 10");
Check the way you're performing a string concatenation (putting together strings). It seems like there's a copy/paste error as you're using '" instead of just a "
I would use whitespace (and a good code editor) to your advantage by reformatting your code to look like this:
$queryString = "SELECT * FROM db_products WHERE owner_id='$user_id'"
." AND active=1 " //Note these
. $filter_string //are separated
. "ORDER BY RAND() LIMIT 10 "; //into individual lines
$sql_more_items = $db->query($queryString);
This style helps you keep track of whether you're using " or ' for your strings and also helps you debug things more easily than putting it into one giant hard to read string.
That's probably because of the part
`"' AND active=1 '"`
^.... This ' here
My table in mysql has special data stamp (startdata). It is date when event starts. Older events stored in database to, but i don't need them to appear in the MySQL answer.
So is there any way sending query to database that includes parameter i need? (for example, not showing rows where startdate older than today).
Now my code looks like:
$res3 = mysqli_query($con,"SELECT * FROM raspisanie WHERE
instr='" . $instrument . "' AND school IN(" . $array2 . ")
AND type='regular' AND state='1' ORDER by startdate");
Add condition in your where clause.
$res3 = mysqli_query($con,"SELECT * FROM raspisanie WHERE
instr='" . $instrument . "' AND school IN(" . $array2 . ")
AND type='regular' AND state='1' AND startdate >now() ORDER by startdate");
I simply want to ORDER the comments by the ID, but I have no luck in doing it. Can't figure out what to do, because this is confusing me: articleid='" . mysql_real_escape_string($_GET['id']) . "'
Would you guys happen to know how I could go about ordering the comments by the id in DESC? thanks!
<?php
$amount_get = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "'"); $comments = mysql_num_rows($amount_get);
$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "'");
if (mysql_num_rows($grab)==0) {
echo "<div class='alert alert-note-x'>Sorry, it looks like their are no comments to be displayed, check back later!</div>";
}
while($row = mysql_fetch_array($grab)){
?>
First of all you're doing the same SELECT two times. That's pretty unnecessary since you can count rows and get the data from a single query. Additionally to this replace commentid with the unique id of your comment table and you're set. Replace DESC with ASC to reverse the sort order.
<?php
$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' ORDER BY commentid DESC");
$comments = mysql_num_rows($grab);
if (mysql_num_rows($grab)==0) {
echo "<div class='alert alert-note-x'>Sorry, it looks like their are no comments to be displayed, check back later!</div>";
}
while($row = mysql_fetch_array($grab)){
?>
add ORDER BY clause
$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' ORDER BY articleid, ID DESC");
your query is vulnerable with SQL Injection, please read the article below to protect from it,
How can I prevent SQL injection in PHP?
try this
$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' order by articleid desc");
I think your comments table habe a column id then, so:
$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' ORDER BY id DESC");
This is a sql thing to sort it, not php, so you just have to modify your sql statement.
$amount_get = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' ORDER BY id DESC ");
$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "' ORDER BY id DESC");
Three suggestions:
1) Complete your "select" statement in a string variable (as shown below; it makes debugging much easier)
2) Consider using prepared statements instead of raw "select" (or update or delete!).
It can help performance. But it makes your PHP much more secure!
3) Consider moving away from the (deprecated) mysql_query() syntax
<?php
$sql =
"SELECT * FROM comment WHERE articleid='" .
mysql_real_escape_string($_GET['id']) . "' order by articleid desc";
$amount_get = mysql_query($sql);
$comments = mysql_num_rows($amount_get);
$sql =
"SELECT * FROM comment WHERE articleid='" .
mysql_real_escape_string($_GET['id']) . "'order by articleid desc";
$grab = mysql_query($sql);
...
Here's a good link on the mySQLi and PDI APIs that supercede the old mysql_query() syntax:
mysqli or PDO - what are the pros and cons?
And here's a good link on prepared statements:
http://forum.codecall.net/topic/44392-php-5-mysqli-prepared-statements/#axzz2Dazi7pQ4
You can use order by clause in your query
<?php
$getarticles = array();
$getarticles = mysql_query("SELECT * FROM comment order by articleid desc");
if(empty($getarticles)){
echo "<div class='alert alert-note-x'>Sorry, it looks like their are no comments to be displayed, check back later!</div>";
}
echo "<pre>";
print_r($getarticles);
echo "</pre>";
for($i=0;$i<count($getarticles);$i++){
//display
}
?>