[PHP][MySQL]How To SELECT data with two constrain? - php

I'm new to PHP+MySQL programming
I want to SELCET data using tow constrain at once,
the cons1 is
Tid=user02 and Fid=user01
and cons2 is
Tid=user01 and Fid=user02
the data I want to output is something like:
$result1 = mysql_query("SELECT * FROM myTable WHERE Tid='user02' AND Fid='user01');
+
$result2 = mysql_query("SELECT * FROM myTable WHERE Tid='user01' AND Fid='user02');
= what I want
can it be done in single line?
or can make a result which stored $result1 and $result2
thanks for taking time reading my question

You can OR the two conditions resulting in a single query:
SELECT * FROM chat
WHERE (Tid='user02' AND Fid='user01')
OR (Tid='user01' AND Fid='user02')

$result1 = mysql_query("SELECT * FROM chat WHERE Tid='user02' AND Fid='user01' OR Tid='user01' AND Fid='user02'");
Some reading : boolean algebra and the mysql doc

Try this one:
$result1 = mysql_query("SELECT * FROM chat WHERE Tid='user02' AND Fid='user01'
UNION SELECT * FROM chat WHERE Tid='user01' AND Fid='user02'");

Related

php/mySQL: querying records by id from array?

i want to query several records by id like:
$ids = array(10,12,14,16,....);
the query would be something like:
select * from users where id=10 or id=12 or id=14 or id=16 ..
is it possible to query it in a more comfortable way like (compared to php):
select * from user where in_array(id, array(10,12,14,16))
thanks
You can use IN instead of OR clauses
select * from users where id IN (put_your_array_here)
Example:
select * from users where id IN (10,12,14,16);
Note:
According to the manual for MySQL if the values are constant IN
sorts the list and then uses a binary search. I would imagine that
OR evaluates them one by one in no particular order. So IN is
faster in some circumstances.
Related post
Try this.
$id = array(10,12,14,16,....);
$ids = join("','",$id);
$sql = "select * from user where id IN ('$ids')";
OR
$ids = implode(',', $id);
$sql = "select * from user where id IN ($ids)";
You can do it like that using implode in PHP:
"select * from users where id in '".implode("','", $ids)."'"
Please be sure that your ids are safe though

SELECT query failing after updating it

I modified the following query and am now getting this error.
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result,
boolean given
I have read into what this error means and I know it is my query that is wrong. This worked perfectly before I changed the query. It was this before...
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` = 3");
I changed it to...
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` = 3, 4 ,5");
$array = array();
while ($row = mysqli_fetch_assoc($query)) {
What am I doing wrong in the new query?
your query has a mistake.
Try this:
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` in (3, 4 ,5)");
The in serves the same function as an OR
Your query is indeed wrong, you cannot check a value like that.
If you want to check against multiple values, you can make use of the IN keyword.
"SELECT * FROM users WHERE `group` IN (3, 4 ,5)"
If you want to check of either one of the choice use IN instead:
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` IN (3, 4 ,5)");
For comma separated value use IN
SELECT * FROM users WHERE `group` IN(3,4,5);
you may try this following also,may be you will get your result.
SELECT * FROM users WHERE group = 3 or group = 4 or group = 5;
Thanks.

SQL WHERE id is odd?

I was wondering if it were possible to list all of the ODD ID' using PHP and SQL.
I am currently using, which shows all results:
$result = $con->query("SELECT * FROM Users");
How would it be possible to list only ODD results using PHP and SQL?
thanks in advance..
Or you can do probably faster solution:
$result = $con->query("SELECT * FROM Users WHERE id % 2");
DEMO
You can do,
SELECT * FROM Users WHERE MOD(id,2) = 1
SELECT *
FROM YOUR_TABLE
WHERE (ID_COLUMN % 2) = 1

PHP SQL Select From Where

I am having some difficulty running some SQL code.
What I am trying to do is, find a row that contains the correct username, and then get a value from that correct row.
This is my SQL in the php:
mysql_query("SELECT * FROM users WHERE joined='$username' GET name")
As you can see, it looks for a username in users and then once found, it must GET a value from the correct row.
How do I do that?
You need some additional PHP code (a call to mysql_fetch_array) to process the result resource returned by MySQL.
$result = mysql_query("SELECT name FROM users WHERE joined='$username'");
$row = mysql_fetch_array($result);
echo $row['name'];
mysql_query("SELECT `name` FROM users WHERE joined='$username' ")
Just select the right column in your 'select clause' like above.
Edit: If you are just starting out though, you might want to follow a tutorial like this one which should take you through a nice step by step (and more importantly up to date functions) that will get you started.
mysql_query("SELECT name FROM users WHERE joined='$username'")
$q = mysql_query("SELECT * FROM users WHERE joined='$username'");
$r = mysql_fetch_array($q);
$name = $r['user_name']; // replace user_name with the column name of your table
mysql_query("SELECT name FROM users WHERE joined='$username' ")
Read documentation : http://dev.mysql.com/doc/refman/5.0/en/select.html

MySQL SELECT WHERE returning empty with long numbers, although they are there

Alright, so basically the most simple query ever... I've done this a million times...
SELECT *
FROM purchased_items
WHERE uid = '$uid'
if $uid == 123 It works fine and returns all data in rows where uid is 123
if $uid == 351565051447743 It returns empty...
I'm positive 351565051447743 is a possible uid in some rows, i literally copied and pasted it into the table.
$uid is a string, and is being passed as a string.
This is something i've done a million times, and i've never had this simple query not work.
Any ideas why this is not working?
You're probably getting an E{some_power} representation as a string from the double.
What I mean is
$query1 = "SELECT * FROM purchased_items WHERE uid = '$uid'";
Produces:
SELECT * FROM purchased_items WHERE uid = '3.5156505144774E+14'
One way to fix it is:
$query = sprintf("SELECT * FROM purchased_items WHERE uid = '%d'", $uid);
Not sure if sql supports E format so this may or may not be the issue.
http://viper-7.com/v6MhVe
dit: Quick workaround
$format = (is_numeric($uid)) ? '%d' : '%s';
$query2 = sprintf("SELECT * FROM purchased_items WHERE uid = '{$format}'", $uid);;
What is the datatype of uid on your table? How about casting uid to another datatype?
SELECT *
FROM purchased_items
WHERE CAST(uid AS VARCHAR(25)) = '$uid'
Alright, so if you use AMFPHP apparently when you use the browser for testing it doesn't matter if you 'cast' the value as a string in the query. You need to pass it with quotes in the string in the browser interface.

Categories