Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to do a query by adding a "OR" statement. Using what i have below won't show all entries. What am i doing wrong?
//$q = search variable;
$sql = "SELECT name, id_code from codes WHERE (id_code = '$q') OR (name='$q')";
$sql = "SELECT name, id_code from codes WHERE (id_code = '$q') OR (name='$q')";
$sql = "SELECT name, id_code from codes WHERE (id_code = '$q' OR name='$q')";
$sql = "SELECT name, id_code from codes WHERE id_code = '$q' OR name='$q'";
All of these will produce the same result.
Have you tried SELECT name, id_code from codes WHERE id_code = '$q' OR name='$q' (i.e. without the parentheses)?
This code is correct. The reason why it doesn't work is because the needle hasn't been found in the haystack.
This may be because the id_code column its a numeric column, or the values of the column are zero padded or the variables aren't been passed to the query.
Check the variables to see if they are being passed by echoing the query and take a look at the data type of the fields.
If a field is numeric, it can be assigned or compared without the quotes.
Also, if you want to search part of the name column, you will have to change the = operator to a like operator:
$sql = "SELECT name, id_code from codes WHERE (id_code = '$q') OR (name like '%$q%')";
The % means anything (zero or more chars), so the query will look for any name starting with anything, then your search therm and then ending with anything.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Consider the following script:
SELECT * FROM TABLE1 WHERE COLUMN1='$exb';
$exb is a PHP variable which is fed from an HTML select list with 3 values:0,1,2. Values 1 and 2 correspond to column1 values. In case of selecting value 0, I want to include all the values of COLUMN1. Is there a way to implement the above without changing the script? In other words is there any keyword to assign to $exb which will oblige the script to select all the rows of table TABLE1 in case the user selects 0 from HTML select list?
Thank you
It's a little unclear, but I think you are asking is there a special clause you can add to a where clause to return every single row from the database rather than specific criteria matched in a where clause.
You can put in a pretend where clause by saying col1=col1 which is effectively a bogus (though valid) syntax like this:
SELECT * FROM TABLE1 WHERE COLUMN1=column1;
Though it would have to be without the quotes to select every single row of the table.
Putting the quotes around the variable would be very simple in your php however.
Having said that, wouldn't it be much easier to simply omit the where clause entirely if the value selected is 0?
For this you require to build a dynamic query.
$query = "SELECT * FROM TABLE1";
$exb = isset($_GET['exb']) ? $_GET['exb'] : 0;
$goodParam = array(0,1,2);//array of allowed values
if($exb>0){
if (in_array($exb, $goodParam)) {
$query .= " WHERE COLUMN1 = '$exb'";
}
}
echo $query;
I don't think you can do that with MySql query only. You need to use php and ifelse statement where you can check for $exb value before executing query. For example:
if($exb == "0")
$query = "SELECT * FROM TABLE1";
else
$query = "SELECT * FROM TABLE1 WHERE COLUMN1='$exb'";
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
how can i use right code for using ORDER BY id DESC and WHERE
$sel = "SELECT * FROM items where portfolio_id=".$_GET['folio_id']."ORDER BY id DESC";
Add a space here as shown.
$sel = "SELECT * FROM items where portfolio_id=".$_GET['folio_id']." ORDER BY id DESC";
----^
Also, don't pass your parameters like $_GET or $_POST directly into the SQL query as it will definitely lead to SQL Injection Attacks. Filter those parameters or make use of Prepared Statements.
add a space before 'ORDER' at least
$sel = "SELECT * FROM items where portfolio_id=".$_GET['folio_id']." ORDER BY id DESC";
You need provide space before ORDER BY
$sel = "SELECT * FROM items where portfolio_id=".$_GET['folio_id']." ORDER BY id DESC";
Simply Use this :
$sel = "SELECT * FROM items where portfolio_id ='$_GET[folio_id]' ORDER BY id DESC";
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I have the following piece of code.
$sql = "SELECT TitreEvent, DescriptionEvent, MomentEvent, image_small, Confidentialite, ID, Creation, Username
FROM events_home
WHERE ID = '" . $dnn['IDcontact'] . "'
ORDER BY Creation DESC";
My problem is that I want to display the most recent events first (regardless of who the user is). But now, the events are displayed according to users (IDcontact), and new events are placed at the beginning of the portion of the user.
So if a user is like the second one in the table, the new events he will add will be displayed after the events of the first user. But I want to see the newest events at the top of the list, and I have no idea how.
EDIT So there is the missing part. I have tried to put SORT BY RAND () in the first query, but it dosen't change anyting. It just place the first user at the second place, and the second one in the first place.
<?php
$dn = mysql_query("SELECT IDcontact FROM contacts WHERE ID = '".$_SESSION['id']."'");
if(mysql_num_rows($dn)>0)
{
while ($dnn = mysql_fetch_array($dn)) {
$req = mysql_query("select TitreEvent, DescriptionEvent, MomentEvent, image_small, Confidentialite, ID, Creation, Username from events_home where ID = '".$dnn['IDcontact']."' ORDER BY Creation DESC");
if(mysql_num_rows($req)>0)
{
while($dnn = mysql_fetch_array($req)) {
?>
I suppose you're running this in a loop from a previous query result. In which case you need to change the 'parent' query. If there's no such thing then why are you sorting results by the user's ID and expecting all results regardless of the user?
EDIT:
You should never run queries in loops.
<?php
$dn = mysql_query("
SELECT e.TitreEvent, e.DescriptionEvent, e.MomentEvent, e.image_small, e.Confidentialite, e.ID, e.Creation, e.Username,c.IDcontact
FROM events_home e LEFT JOIN contacts c ON c.ID = e.ID
ORDER BY e.Creation DESC
LIMIT 0,30"); // Just in case
Here's your query, hope that helps.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Guys im having a issue with my mysql php problem ok i want this to pull table information out of my mysql database in it shows up the firstname by the number 10 but i want to add more too it like 10 100 1000 in other numbers how can i do this from this code
$result = mysqli_query($con,"SELECT * FROM tvshows
WHERE FirstName='10',100,1000");
SELECT * FROM tvshows WHERE FirstName IN (10,100,1000);
You can look here:
http://dev.mysql.com/doc/refman/5.0/en/any-in-some-subqueries.html
and here:
http://dev.mysql.com/tech-resources/articles/subqueries_part_1.html
If you want to select strings, you need to '', ex.:
SELECT * FROM tvshows WHERE FirstName IN ('10','100','1000');
If Numbers like this:
SELECT * FROM tvshows WHERE FirstName IN (10,100,1000);
The IN operator allows you to specify multiple values in a WHERE clause.
$result = mysqli_query($con,"SELECT * FROM tvshows
WHERE FirstName IN('10','100','1000')");
See Example
You have to change the = for IN
$result = mysqli_query($con,"SELECT * FROM tvshows WHERE FirstName IN(10,100,1000);
SQL IN clause documentation
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I trying to display all information submitted by a specific username(variable) in my case:
<?php
$Username = $_SESSION['VALID_USER_ID'];
$q = mysql_query("SELECT * FROM `article_table` ORDER BY FIELD (Username, '.$Username.') DESC LIMIT 1");
while($db = mysql_fetch_array($q)) { ?>
Your Articles: <?=$db['Subject'];?><br />
<? } ?>
But don't work, I'll display the last article subject(for example) from the table and not from the specific username, where I'm wrong?
That is not how ORDER BY works. You first need to filter the results by using the WHERE clause.
$q = mysql_query("SELECT * FROM `article_table` WHERE `Username` = '$Username' ORDER BY `other_column` DESC LIMIT 1");
Replace other_colum in this query by the name of your date column: ie. date_created
Here is your query:
SELECT *
FROM `article_table`
ORDER BY FIELD (Username, '.$Username.') DESC
LIMIT 1;
This should be returning the username you are looking for when it is available. The logic is that field returns 1 when the name matches and 0 otherwise. So the descending keyword puts the match first. If there is no match, then you will get another row.
Presumably the data does not exist. You can test that easily enough by running:
SELECT *
FROM `article_table`
WHERE Username = '.$Username.';