How to select [somename] from a table in my database - php

The code below will display John, Steve, and Pat from a table in my database. I want it to only display John and Steve.
$result = #mysql_query("SELECT * FROM name");
$line = #mysql_fetch_array($result);
$users = $line[user];
echo "$users";

SELECT * FROM name WHERE [column_name] = 'john' OR [column_name] = 'Steve'
Is that what you're looking for? A few notes:
You'll need to replace [column_name] with the name of the actual column being compared, since that's not specified in your code above.
Try not to use SELECT * where possible. It's generally better practice to select the actual column names. Slightly less work on the database, doesn't break as easily when schema changes, etc.
This comparison is case-sensitive.
UPDATE: Correction, MySQL isn't case-sensitive. So #3 above shouldn't be a concern in that environment.

You need a WHERE clause in your SQL query.
SELECT * FROM name WHERE somecolumn=somevalue;

For this odd example, you could change your query to:
select * from name where name in ('Steve','John')
or
select * from name where name <> 'Pat'

I'm not sure if this is what you want, but you can filter by a list of matching names in the SQL thusly:
SELECT * FROM name WHERE name IN ('John', 'Steve')
This assumes your table is called name and the column containing the user's name is also called name.
I have to wonder what you are trying to achieve here, is there some other useful information you are trying to get from the user record? Or should you be limiting the users returned based on some other criteria beyond their name?

$result = #mysql_query("SELECT * FROM name LIMIT 2");

Related

How to Select a keyword from username or city in single query

How to select data from two columns in same table in a single query.
for example select name "waseem" from username if not found check in city row.
select * from <table_name> where name like 'waseem' or city like 'waseem';
I agree with other's answers but with slight modification. If there is a record waseem ahemed and your only looking for waseem wont be searched in other answered query.
You need to use the wildcard % if you want to search for waseem.
I highly suggest you not to use * for selecting the column names because of the performance issues.
SELECT name, username, password WHERE name LIKE '%waseem%' OR city LIKE '%waseem%'
This is working for me
SELECT * FROM table WHERE row_name1 LIKE 'value' OR row_name2 LIKE 'value'

SELECT * FROM tbl name WHERE name LIKE 'ask'

Hy, i have a sql tabl name kill, with fields like
ID
name
lname.
There are several names are same, like ali, kiran etc, i want to show all the people with the name ali, so i tried this
SELECT * FROM ask WHERE name LIKE 'ali'
but it shows only the last added ali, please will you tell me the right way to do this. thanks
IF you are trying to find all values containing ali for e.g.
Bali
Alison
etc...
What you need to do is run a wildcard search query, so try this:
SELECT * FROM ask WHERE name LIKE '%ali%'
This will find all values where name contains part of ali in it.
If you want to find all names ending in ali, you can do this:
SELECT * FROM ask WHERE name LIKE '%ali'
If you want to find all names starting with ali, you can do this:
SELECT * FROM ask WHERE name LIKE 'ali%'
etc...
I prefer to use REGEXP, for example:
SELECT * FROM ask WHERE name REGEXP 'ali';
Your query should be as below:-
SELECT * FROM ask WHERE name LIKE '%ali%'
Check this link for detailed info.
SELECT * FROM ask WHERE name LIKE '%ali%';
The Syntax for query in your case is
SQL LIKE Syntax
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern;
You should use query below to selects all names starting with the letter "ali":
SELECT * FROM ask WHERE name LIKE 'ali%';
You should use query below to selects all names ending with the letter "ali":
SELECT * FROM ask WHERE name LIKE '%ali';
You should use query below to selects all names containing the letter "ali":
SELECT * FROM ask WHERE name LIKE '%ali%';
Refer This link for tutorials on Like.

How to query and select * where a value is a INT?

This is my SQL so far and nothing is working.
$st = DB::getInstance()->query("SELECT * FROM users WHERE group = 1 ORDER BY joined ASC");
All I want is to Select * from my table users where group = 1 (order by...)
Problem is my group column is a int. but I cannn't retrieve any data from it. Even if I try
group = '1'
Is there any function/way to get through this issue?
THanks!
group is a reserved word in many SQL implementation. Try using users.group = 1 .
Oracle SQL reserved words : https://docs.oracle.com/database/121/SQLRF/ap_keywd001.htm#SQLRF55621
MySQL reserved words : http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html
MSSQL reserved words : http://msdn.microsoft.com/en-us/library/ms189822.aspx
Yes, stop using select *
When writing your query, enter all the column names (or alias them if you want them to be called something different). For eg:
select
userID,
nickname,
email as loginEmail
from
// ... etc etc
It also seems that you are using a reserved word group as a column name. You need to either backtick this (mySQL uses the ` character) to tell the database that you mean it as a column/table name and not an actual function like this:
select
userID,
nickname,
email as loginEmail,
`group`
from
sometable
where
`group`=1

Utilizing two php variables in mysql select statement

I am not sure if I am apporaching this in the best way, but what I am trying to do is use two php variables for a IN clause in a mysql select query.
"select * from `user` where '$phpvar1' IN ('$phpvar2')"
Normally instead of $phpvar1 I would use a column name in the user table and it works fine, however in my case now I need the $phpvar1 variable to be used as a temporary column in this specific select query. Since $phpvar1 is dynamic in each situation, I do not want to store it. What can I do? Thanks!
EDIT: To clarify, $phpvar1 is not a column in the table, but I want it to act as one only for this one query. In this query I want $phpvar1 which is equal to for example: "cat", where as "cat" is the data that would be found in the column, and then this data ("cat") would be used in the IN clause to see if $phpvar2 contains "cat"
Basically what I want it to do is this:
SELECT * from `users` where 'mouse' IN ('cat', 'dog') and `userID` = '1'
the rows returned should have a userID = 1 AND meet the IN clause, since 'mouse' is not in ('cat,'dog') there won't be any rows returned in this case.
Rewrite your query to:
"SELECT * FROM `users` WHERE '$phpvar1' IN ('".implode("', '", $phpvar2)."') and `userID` = '1'"
Assuming that $phpvar2 is array containing values to check for.
Try
$sql = "select * from `user` where ".$phpvar1." in (".$phpvar2.")";
Your php variable should be in the select part
Select $phpvar,otherstuff, from user where x in ('$phpvar2');

Any way to select from MySQL table where a field ends in certain character/number?

I am hoping to run a mysql_query where I can select a row if a certain field ends in a number. For example:
<?php
$query = mysql_query("SELECT id FROM table WHERE [ID ENDS IN 0]");
$query = mysql_query("SELECT id FROM table WHERE [ID ENDS IN 1]");
$query = mysql_query("SELECT id FROM table WHERE [ID ENDS IN 2]");
$query = mysql_query("SELECT id FROM table WHERE [ID ENDS IN 3]");
//and so on...
?>
Is there a way to do this? Any help would be great. Thank you!
SELECT ...
WHERE somefield LIKE '%1'
SELECT id FROM table WHERE mod(id, 10) = 1
give it a go
SELECT id
FROM table
WHERE id LIKE '%0'
You can use regular expressions if you need to find if field is ending in a number or not as follows
SELECT id FROM table WHERE id REGEXP '[0-9]$'
Hope it helps...
You could do it with a cast and LIKE, but the performance is likely to be terrible for any non-trivial amount of data (I've not tested in your particular case, but in my experience, casting to string so you can use string operations really slows a query down).
A better way would be to use modulus.
For example, to get all the rows where the numerical field ends in a 4:
SELECT *
FROM table
WHERE MOD(column_of_interest, 10) = 4
Again, I've not benchmarked this, but it's probably going to perform better than casting would.
Of course, if the column type is already a string, then LIKE is the way to go, as using MOD on strings would also require a cast.
You can use LIKE and a wild card expression
it should be somthing like
SELECT id FROM table WHERE id REGEX '%0$'
SELECT id FROM table WHERE id REGEX '%1$'
and so on.
SELECT id FROM table WHERE id REGEXP '1[[:>:]]'

Categories