SQL query with 2 AND possible? - php

I have the following query in SQL
$querycountry = mysql_query("SELECT
`country_iso2`
FROM
`ban_country_ip`
WHERE `ip_from` <= INET_ATON('$ip')
AND ip_to >= INET_ATON('$ip')
AND checkbox = 1
ORDER BY NAME ");
Is it a valid query if i added the second AND.
Any suggestions on how to overcome this.

Is it a valid query if i added the second AND.
Yes it is. You can use multiple conditions for a single query and AND or OR is used to add more than one conditions.
From http://www.tutorialspoint.com/mysql/mysql-where-clause.htm
You can specify more than one conditions using AND or OR operators.

Related

Error trying to select field while not selecting first character

I'm getting the error: Incorrect syntax near the keyword 'AS'.
I'm trying to select a column from my MSSQL database while NOT SELECTING the first character of that field.
Below is the code (Within PHP):
$sql = "SELECT RIGHT(Column, LEN(Column) - 1) FROM Table WHERE [Column] ='".$search."' AS Column2";
First of all. Please be careful. Your code looks vulnerable to an SQL injection. Better use paramterized queries.
The second "AS" at the end is not needed.
Try it that way:
SELECT RIGHT(Revlv, LEN(Revlv) - 1) AS Revlv2 FROM table_name WHERE [Objkt] ='".$search."'
Or better yet:
SELECT RIGHT(Revlv, LEN(Revlv) - 1) AS column_name FROM table_name WHERE [Objkt] = ?
Read more about parameterized queries here
Also take care when the Revlv column only contains an empty string. The query will fail in that case.
try this:
$sql = "SELECT RIGHT(Column, LEN(Column) - 1) AS Column2 FROM Table WHERE [Column] ='".$search."' ";
As all answers simply fix the syntax instead of fixing the logic:
There's no need to use RIGHT + LEN to extract everything but the first character, simply use
substring(Revlv from 2) AS Revlv2 -- Standard SQL to extract everything after the first character
As SQL Server has a slighty different syntax:
substring(Revlv, 2, 8000) AS Revlv2 -- T-SQL to extract everything after the first character
You need to put AS after the SELECT part of your query never put it at the end of your query. Try to do it like this
$sql = "SELECT RIGHT(Column, LEN(Column) - 1) AS Column2 FROM Table WHERE [Column] ='".$search."' ";
SOURCE
try this.
$sql="SELECT RIGHT(Revlv, LEN(Revlv)-1) AS Revlv2
FROM tablename where['objkt']='$search'";

How to Make PHP/SQL Query to Select Rows Where Column Doesn't Equal Variable

I am trying to make a Query for an MSSQL table using PHP that will find rows with a column that doesn't equal a certain value.
I am currently using this:
$selecting6 = "select * from used_trailers1 where photo1='".$picature2."' or photo='".$picature2."' or name='".$picature2."' or photo2='".$picature2."' or photo3='".$picature2."' or photo4='".$picature2."' and Orderid isnot '".$id."'";
To no avail, this code doesn't work.
What proper code can be used to select rows that don't have the value of $id?
Thank you for any help. All help is appreciated.
Try this
$selecting6 = "select * from used_trailers1 where (photo1='".$picature2."' or photo='".$picature2."' or name='".$picature2."' or photo2='".$picature2."' or photo3='".$picature2."' or photo4='".$picature2."') and Orderid !='".$id."'";
Not equal operator in SQL standard is <> But MySQL and SQL Server also support !=. Read Comparison Operators in SQL Server Comparison Functions and Operators in MySQL.
SELECT [*] FROM [tbl] WHERE id <> $id

MySQL SELECT COUNT with if statement - What is the best practice?

I'm getting inconsistent results with the way I'm doing this so I thought I'd ask the geniuses here what you think. Here is what I'm doing...
$ccquerycount = "SELECT COUNT(*) as `count` FROM payments_cc_info WHERE user_id=$userid";
....
....
if ($row['count'] == "1") {
} else {
}
Is there a better way to design the if statement to achieve consistent results?
I am assuming that you are executing the query, retrieving the result set, fetching the row and extracting the value before the if statement (if not, then perhaps you need to do that first! Here is a start for PHP-MySql.
In case if you have already done that and forgot to mention this in your post (innocent until proven guilty I always say!! :) ), then could you provide us with more data on what are the different datasets when you got inconsistent results?
Try this
$ccquerycount = "SELECT * FROM payments_cc_info WHERE user_id=$userid";
$result = mysql_query($ccquerycount)
if (mysql_num_rows($result)==1) {
} else {
}
mysql_num_rows()
Get number of rows in result
Warning mysql_ extension is being deprecated . Instead, the MySQLi or PDO_MySQL extension should be used .
You can also add if condition inside of your mysql query like below.
SELECT
rating,
trade_service_id,
ifnull(round(sum(belonging_product_ratings.rating)/count(*),0),0) AS avg,
count(*) AS count
FROM
belonging_product_ratings WHERE trade_service_id != 0
GROUP BY
belonging_product_ratings.trade_service_id
other alternative is as below
select
BelongingProductRating.*,
User.*
from
belonging_product_ratings AS BelongingProductRating
LEFT OUTER JOIN
users AS User ON
IF( BelongingProductRating.rated_user_id != '$userID', BelongingProductRating.rated_user_id, BelongingProductRating.user_id) = User.id
You can say if condition inside mysql query is same as ternary if.
You can set query according to your php variable too.
I am sharing this code which i was ended up.
all you need to do is set it according to your table fields and conditions.
feel free to ask.

PHP mysqli condition greater than or less than symbol error

I am getting the following strange issue in mysqli prepared statement
This is my sample query and params
SELECT * from ecf_request WHERE id > ? OR id < ?
...
$stmt->bind_param("ii", $id, $id1); //5, 10
When i use just = conditions it's working fine but for > and < conditions it's not working.
I am getting "Number of variables doesn't match number of parameters in prepared statement" error
Is it possible to use greater than and less than symbols in prepared statements ?
I need to implement it for date filter condition. records between two dates.
Thanks in advance
one id cannot be greater and smaller to itself at the same time.
you are giving a condition like id >10 and id <10 at the same time.
If you want both id1 and id2 to be selected use SELECT * from ecf_request WHERE id IN(?,?) instead of AND and if you want to select everything in between then use SELECT * from ecf_request WHERE id BETWEEN ? AND ?
You can use the BETWEEN statement. Here's an example:
SELECT * FROM `ecf_request` WHERE `id` BETWEEN ? AND ?

How to solve this sql query?

I have the following query to fetch rows from wpr_subscribers provided that the conditions in the query.
SELECT subscribers.* FROM wpr_subscribers subscribers, wpr_followup_subscriptions subscriptions WHERE
subscribers.id=subscriptions.sid AND
subscribers.active=1 AND
subscribers.confirmed=1 AND
subscriptions.type='autoresponder' AND
subscriptions.eid=$autoresponder_id AND
subscriptions.sequence = -2 OR
CEIL((subscriptions.last_date-subscriptions.doc)/86400) >= $message_index
The query returns two of every row. How do I solve this issue?
SELECT DISTINCT subscribers.* FROM wpr_subscribers subscribers, wpr_followup_subscriptions
subscriptions WHERE
subscribers.id=subscriptions.sid AND
subscribers.active=1 AND
subscribers.confirmed=1 AND
subscriptions.type='autoresponder' AND
subscriptions.eid=$autoresponder_id AND
subscriptions.sequence = -2 OR
CEIL((subscriptions.last_date-subscriptions.doc)/86400) >= $message_index
DISTINCT keyword would help you in returning only the unique(distinct) rows in the table. Are you sure on the logic of AND and OR in the query and the unique data that you will now receive is indeed correct. Just for the info, AND operator has precedence over OR however you can override that by using parenthesis.
It is probably your OR statement
subscriptions.sequence = -2 OR
CEIL((subscriptions.last_date-subscriptions.doc)/86400) >= $message_index
You should enclose this in parenthesis for whatever the scope of the OR is

Categories