I am trying to query based on the value of an enum field. Essentially I have 3 values (categories) for the enum field, a, b and c respectively. I am attempting to populate a select field based on the results of the following query:
SELECT * WHERE pid = $id AND group = 'a';
I have tried this with and without single quotes and get the error You have an error in your SQL syntax; and have narrowed it down to being a lack of knowing how to query based on a specific enum value. I assumed this would work and see no reason for it not to so if I can be enlightened I would greatly appreciate it.
Group is a reserved keyword use it like this
SELECT * FROM mytable WHERE pid = $id AND `group` = 'a';
Use tablename
SELECT * From tablename WHERE pid = $id AND `group` = 'a';
Try This..
SELECT * FROM tbname WHERE pid = $id AND `group` = 'a';
Related
I am creating a query and I used the LEFT JOIN to join two tables. But I'm having trouble in getting the fb_id value from the table, it gives me an empty result. Here is my code:
$sql = "SELECT * FROM tblfeedback a LEFT JOIN tblreply b ON a.fb_id = b.fb_id WHERE a.fb_status = 1 ORDER BY a.fb_id DESC";
$res = $con->query($sql);
....
....
The query above would give me a result like this :
I think that's why I don't get the fb_id value is because the last column is null. How do I get the value of fb_id from the first column? Thanks. I am really having trouble with this. I hope someone can enlightened my mind.
You should give an alias to the column in the parent table, because the column names are the same in both tables. When fetch_assoc() fills in $row['fb_id'], it gets the last one in the result row, which can be NULL because it comes from the second table.
SELECT a.fb_id AS a_id, a.*, b.*
FROM tblfeedback a
LEFT JOIN tblreply b ON a.fb_id = b.fb_id
WHERE a.fb_status = 1
ORDER BY a_id DESC
Then you can access $row['a_id'] to get this column.
More generally, I recommend against using SELECT *. Just select the columns you actually need. So you can select a.fb_id without selecting b.fb_id, and it will always be filled in.
Because you are using a left join, the 2 rows in your result set image are the rows from tblfeedback whose fb_id were not found in tblreply. We know this is true because all the tblreply columns in the result set are null.
With that said, its not real clear what you are asking for. If you are asking how you access the tblfeedback.fd_id column from your query via php, you can use the fetch_array method and use the 0 index.
$sql = "SELECT * FROM tblfeedback a LEFT JOIN tblreply b ON a.fb_id = b.fb_id WHERE a.fb_status = 1 ORDER BY a.fb_id DESC";
$res = $con->query($sql);
while($row = $res->fetch_array()) {
echo "fb_id: " . $row[0] . "<br>";
}
I have mysql column called categories. It can contain single or multiple values like this: 1 or 2 or 1,2,3 or 2,12...
I try to get all rows containing value 2.
$query = "SELECT * FROM my_table WHERE categories LIKE '2'";
$rows = mysql_query($query);
This returns row if column only has value 2 but not 1,2,3 or 2,12. How I can get all rows including value 2?
You can use either of the following:
% is a wildcard so it will match 2 or 1,2, etc. Anything on either side of a 2. The problem is it could match 21, 22, etc.
$query = "SELECT * FROM my_table WHERE categories LIKE '%2%'";
Instead you should consider the find_in_set mysql function which expects a comma separated list for the value.
$query = "SELECT * FROM my_table WHERE find_in_set('2', `categories`)";
Like #jitendrapurohut said, you can do it using
$query = "SELECT * FROM my_table WHERE categories LIKE '%2%'";
$rows = mysql_query($query);
But is really bad to store collections like this. A better aproach is as follow:
categories(id_c, name) => A table with each category
my_table(id_m [, ...])
categories_my_table(id_c, id_m)
Then use this query:
SELECT *
FROM my_table m
INNER JOIN categories_my_table cm ON m.id_m = cm.id_m
INNER JOIN categories c ON cm.id_c = c.id_c
WHERE
c.id_c = 2;
EDIT:
#e4c5 link explains why it is bad to store collections like this...
SELECT * FROM my_table WHERE categories LIKE '%2%' AND categories!='1,2,3' AND categories!='2,12';
I know how to select from a database, my question is i want to set a variable for a where clause in my query, so lets say select * FROM Foo WHERE A = '$a', but the variable has not been set previously and is from the table i am trying to select from.
So how would i do this.
PHP
$FC_ID = mysqli_real_escape_string($dbc,[]);
$FOOD_sel = $dbc->query("SELECT * FROM Food_Cat WHERE Food_Cat_ID = '$FC_ID'");
That is how far i have got so far.
Try to use sub query for that $FC_ID value
sub query method
$FC_ID = mysqli_real_escape_string($dbc,[]);
$FOOD_sel = $dbc->query("SELECT * FROM Food_Cat WHERE Food_Cat_ID = (select FC_ID from your table )");
I have the following SQL query, which has a subquery in it:
SELECT * FROM `statics` WHERE `mmsi`=
(SELECT `mmsi` FROM `positions`,`active`
WHERE `active.mmsi` = `positions.position_ID`);
But when I execute it, I get the following error:
1052 - Column 'mmsi' in field list is ambiguous
Please help me on adjusting my query.
Without seeing your table structure, this is a wild guess:
SELECT *
FROM `statics`
WHERE `statics.mmsi` = (SELECT `active.mmsi`
FROM `positions`,`active`
WHERE `active.mmsi` = `positions.position_ID`);
but I don't get why are you doing this with a subquery. This one should yield the same results
SELECT statics.*
FROM `statics`, `positions`
WHERE `statics.mmsi` = `positions.position_ID`;
I have this query
$query = "Select * FROM table WHERE table.firs_column = 1;
Select * FROM table WHERE table.second_column = 1;
Select * FROM table WHERE table.third_column = 1;
Select * FROM table WHERE table.column = 1";
$stmt = $db->prepare($query);
$result = $stmt->execute();
I want to have multiple results, each one have the result of one query!
how to do it?
It looks like you are using PDO, so you could do something like:
$first_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->nextRowset();
$second_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->nextRowset();
$third_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->nextRowset();
$fourth_set = $stmt->fetchAll(PDO::FETCH_ASSOC);
To get your 4 rowsets.
You can use UNION if table is same for your multiple queries
Select * FROM table WHERE table.firs_column = 1
UNION
Select * FROM table WHERE table.second_column = 1
UNION
Select * FROM table WHERE table.third_column = 1
UNION
Select * FROM table WHERE table.column = 1
After you finish with the first result set, use nextRowset() method to advance to the next result set.
You need to have a PHP MySQL driver extension that supports this method.
Not sure what you're aiming at, but did you try UNION?
Your SQL statement would look like this:
SELECT * FROM table WHERE table.firs_column = 1
UNION
SELECT * FROM table WHERE table.second_column = 1
UNION
SELECT * FROM table WHERE table.third_column = 1
UNION
SELECT * FROM table WHERE table.column = 1;
Please show your desired result if you think of something different.
Since we don't know anything about your database structure, I suggest looking into mysqli::multi_query().
If you're trying to pull related data, I highly suggest you look into doing MySQL JOINs instead. MySQL is another language unto itself that should be learned as a distinct language rather than just a string to be contatenated in PHP.