I would like to get two of the same value when I Query my database.
I do not have amazing SQL knowledge so I wouldn't know where to start doing this so i'm asking for help from this friendly community.
To help me explain what I am looking for I will show you what I mean.
"SELECT * FROM $databasename WHERE type='post' and type='friend_post' ORDER BY time desc"
(i put the area I am looking at in darker font)
This is what I am trying to do but it dosen't seem to work. Thanks in advance for any help I get, it will be much apreciated.
This is because you check if type is both value, use IN instead.
Which would looks like
SELECT *
FROM $databasename
WHERE type IN('post','friend_post')
ORDER BY time desc
Related
I have a problem.
I have a mysql command as below.
The information in the column I call $birim is not synchronized with the value in the $birim variable.
It comes with other information.
What I want is this:
The birim is "that" and the search word is this or that or that.
I want to run a query of .
Please help me .
Thanks already for your help.
$aranacak=$_POST['aranacak'];
$birim="Kutuphane Adi";
$getir=$baglan->prepare("SELECT *
from eserler
WHERE birim=?
AND baslik like ?
OR esersahibi like ?
OR konu like ?
OR konum like ?");
$getir->execute(array($birim,'%'.$aranacak.'%',
'%'.$aranacak.'%',
'%'.$aranacak.'%',
'%'.$aranacak.'%'));
while ($getircek=$getir->fetch(PDO::FETCH_ASSOC)) {
#############################################
?>
First of all, thank you for your help and such a quick turnaround.
With the help of WOUNDEDStevenJones and RiggsFolly I found my problem.
Solution to the problem:
WHERE unit=? AND (title like ? OR author like ? OR subject like ? OR location like ?)");
This way it worked correctly.
Thank you guys, thank you so much for everything.
I was about to go crazy. :)
I have the following 2 mysql select queries within a PHP if statement:
if ($chooselocn =="") {
$query = "
SELECT $table.*, outcodepostcodes.lat, outcodepostcodes.lng
FROM $table
LEFT JOIN outcodepostcodes
ON UPPER($table.postcode)=outcodepostcodes.outcode
WHERE
$where_no_and
AND
(hide='0' OR hide IS NULL OR hide='')
ORDER BY rent $reihenach LIMIT $offset, $rowsPerPage
";
}
else {
$query = "
SELECT $table.*, outcodepostcodes.lat, outcodepostcodes.lng
FROM $table
LEFT JOIN outcodepostcodes
ON UPPER($table.postcode)=outcodepostcodes.outcode
WHERE
$where_no_and
AND
outcodepostcodes.lat <= $latpoint
AND
(hide='0' OR hide IS NULL OR hide='')
ORDER BY rent $reihenach LIMIT $offset, $rowsPerPage
";
}
The first query works but the second returns this error message:
Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND outcodepostcodes.lat <= 51.491239000000000 AND (hide='0' OR hide IS NULL OR' at line 8
Even if I remove the:
AND
outcodepostcodes.lat <= $latpoint
from the 2nd query making the two identical I still get similar error msg in the second.
Any ideas greatly appreciated.
You probably have an empty $where_no_and variable, thus your second query gets to contain ... WHERE AND ... which is not valid SQL
I bet the variable $where_no_and causes the issue.
It seems like it is empty causing the WHERE statement to be followed by the AND.
So this line:
WHERE $where_no_and AND outcodepostcodes.lat <= $latpoint AND (hide='0'...
is like this:
WHERE AND outcodepostcodes.lat <= $latpoint AND (hide='0'...
The WHERE AND is not valid syntax.
Even after you removed the part:
AND outcodepostcodes.lat <= $latpoint
Your line would look like this:
WHERE $where_no_and AND (hide='0'...
Which would result in this:
WHERE AND (hide='0'...
Again you have WHERE AND which is not valid syntax.
Try to make sure that the $where_no_and variable is not null or empty.
If your business logic says that this variable COULD be empty then you have to write a couple of more lines of code to handle this case.
Cheers!
This is not a solution but a way to find the bug.
Just put a var_dump($query) after the if(). It's impossible to tell without knowing what the variables actually contain. The dump will be before the query is executed, so you'll see very well what is going on there.
If the query is still apparently correct post the dump here and we'll see.
Why do if/then if the queries are supposed to be identical regardless of the condition?
That said, your variables that form the query are probably different depending on the value of $chooselocn. I would start by dumping the actual query into debugging log or the browser as well as the variables that compose it and it would shed some light.
I should also add the standard warning about watching out for SQL injections.
Thank you, thank you, thank you and a MASSIVE THANK YOU to every one who replied to my question!!!!
#Gordon Linoff, thank you for being the first to respond in the right direction
#David Lavieri, thank you for the useful suggestion
#Tudor Constantin, thank you for being the first to explain the crux of the problem
#pid, thank you for the useful suggestion
#user2399432, a massive thank you for the very lengthy, detailed and exhaustive explanation of EXACTLY what was going on.
While everyone pointed me in the right direction, and unfortunately I was busy elsewhere to follow up on those early suggestions, I must upvote #user2399432 for going to all that trouble to make absolutely sure that it all sunk in and there were no two ways about it.
Just as background info, this is an extremely old site I have come back to work on, after a three year absence from coding, and I noticed that I had the following lines of code in this particular script:
///THIS IS THE CRUCIAL LINE BELOW:
$where_no_and = rtrim($where, 'AND ');
///End of crucial line and then TEST
//var_dump($where_no_and);//VERY USEFUL DIAGNOSTIC! INDISPENSIBLE! MUST RETAIN! DO NOT DELETE!
//echo "#6 City is:".$lc_city;//USEFUL DIAGNOSTIC
So I must have had the same problem those many years ago and dealt with it accordingly using var_dump($where_no_and);
Be that as it may, I was well and truly stuck this time round and your valuable help has knocked down the barricades, SO THANK YOU TO ALL!
Can you please help me solve the problem I am having with my query. I am very new in Database programming and I thoroughly search the web for the help but none solve my issue. I am fed up and wondering if this is even possible.
What I want to accomplish is to execute a SELECT query where the column to search is to be supplied on runtime. Like this one:
SELECT *
FROM myTable
WHERE #columnToSeach = #_ColumnName
Advice is really appreciated as my brain is practically bleeding on this one.
A very quick solution would be:
execute ('SELECT * FROM myTable WHERE ' + #columnToSeach + '=' + #_ColumnName)
I am new to CodeIgniter, please help me to write a select statement in CodeIgniter equivalent of
$username='JOHN';
$query=SELECT id FROM table1 WHERE username=$username
Just the format, the rest I'll figure out. Thanks.
One liner to get the id..
return $this->db->select("id")->where("username", $username)->get("table1")->row();
OR
Since id is unique and you're retrieving only one row, add a limit 1 like,
return $this->db->select("id")->where("username", $username)->get("table1", 1)->row();
Hope this helps :)
$this-db->select('id');
$this->db->where('username', $username);
return $this->db->get('table1');
But, as fancyPants said, this is easily findable in all kinds of tutorials around the web. Not a good question.
refer this codeigniter link
visit: https://www.codeigniter.com/user_guide/database/query_builder.html
That would translate to Active Record:
$username='JOHN';
/* allows you to specify selecting a column */
$this->db->select('id');
/* does the FROM, and WHERE */
$query = $this->db->get_where('table1', array('username' => $username));
And to start off, please read the documentation.
I am having issues with my MySQL syntax. I would like to run a select query where either one of two options are true. However the following code does not work.
SELECT * FROM games WHERE genre="indie" OR title="indie"
I have been fooling around and look at other threads and have found out how to use OR to check the same column for multiple entries but not a way to check different columns for the same entries. When I do:
SELECT * FROM games WHERE genre="indie"
The query works fine. Any help would be greatly appreciated.
The only way I see this really would't work, is if you've mistyped the name of the column 'title' (if the second query you wrote works)
The assumptions about the case sensitivity are wrong, since the second query returns something, the first should return at least the same rows as the second one
In MySQL " " works just as ' ', so this assuption was wrong too.
If you post more information, it would be easier to help you
Maybe you ignoring the upper/lower case? Also use like
You can use this:
SELECT * FROM games WHERE (LOWER(genre) like 'indie') OR (LOWER(title) like 'indie')