I'm trying to distinct a column for my autosuggest function. This is the query I have now:
$result=mysql_query("SELECT * FROM users WHERE firstname LIKE '%".mysql_real_escape_string($_GET['chars'])."%' ORDER BY firstname LIMIT 0, 10",$con) or die(mysql_error());
somehow just adding 'DISTINCT firstname' after select doesn't work. (Javascript gives an error.)
the * in the query is the trouble maker I guess, don't know exactly why..
Please assist with writing the right query! :)
Thanks in advance
You could use "group by firstname" in stead:
$result=mysql_query("
SELECT * FROM users
WHERE firstname LIKE '%".mysql_real_escape_string($_GET['chars'])."%'
GROUP BY firstname ASC
ORDER BY firstname
LIMIT 0, 10",$con)
or die(mysql_error());
Related
So I've been stuck on this for a while and I can't find anything on google for this specific thing.
I have this small snippet of code
$link = mysqli_connect("localhost", 'username','password',"database");
$sql = "SELECT * FROM `uploads` ORDER BY id DESC LIMIT 0, 1";
Which should select the latest table by order of id's right?
Well what I want to do is return this id. So if I have 5 items/rows I want to grab the latest (5 in this case) id of the table, and return it. With the eventual goal of using this returned id in javascript but that's a worry for later, right now I just want it in plaintext where the result should only be the id.
This is probably a duplicate question but I can't for the life of me find what I should google to get there
EDIT:
I guess I should clarify further. I know I'm able to do
$sql = "SELECT ID FROM `uploads` ORDER BY id DESC LIMIT 0, 1";
but whenever I try to actually retrieve it/print it its returned as a string instead of the ID.
EDIT 2: I, thanks to some comments, have managed to figure it out. Sorry for the badly worded everything, I'm new to this and as I said don't know how to word it.
SOLUTION:
After just throwing away the $sql thing I added:
$result = mysqli_query($link,"SELECT * FROM `uploads`");
Then I simply did
echo mysqli_num_rows($result);
To echo out the number of rows/what I called the "ID".
Sorry for all the confusion, thanks to those that tried to help. To the others there's no need to be rude.
If I understood your question correctly, you want to get the ID field only, so you have two options:
Option 1 (Recommended)
Given your code
$sql = "SELECT * FROM `uploads` ORDER BY id DESC LIMIT 0, 1";
Change it to:
$sql = "SELECT ID FROM `uploads` ORDER BY id DESC LIMIT 0, 1";
This way, your getting just that ID field you're after. Nothing else is returned from each row.
Option 2
Keep your sql query as it is, and get the ID field from each row in your results (it's an array, so you can retrieve only one field by using its index or name).
Of course, I assume there's an ID field in your table!
Just select the ID.
SELECT id
FROM uploads
ORDER BY id DESC
LIMIT 1;
Simply select what you want.
$sql = "SELECT id FROM `uploads` ORDER BY id DESC LIMIT 0, 1";
The * means you want to select every column there is. However, SQL gives you the possibility to select the specific columns you want. You could also do something like
$sql = "SELECT id, name, title, somethingelse FROM `uploads` ORDER BY id DESC LIMIT 0, 1";
and you'd receive these 4 fields as an array.
$active_sth = $dbh->prepare("SELECT * FROM user_campaign
WHERE status='blasting'
OR status='ready'
OR status='followup_hold'
OR status='initial_hold'
AND uid=:uid
ORDER BY status ASC");
$active_sth->bindParam(':uid', $_SESSION['uid']['id']);
$active_sth->execute();
I am positive $_SESSION['uid']['id'] = 7
but it will also pull results of id 10 or any other number.
Is my AND/OR clause written wrong?
Yes, query is wrong
SELECT * FROM user_campaign
WHERE (
status='blasting'
OR status='ready'
OR status='followup_hold'
OR status='initial_hold'
)
AND uid=:uid
ORDER BY status ASC
You have to group all ORs to make sure that row got one of this values, and separately check if it have given uid.
The proper way to write that is:
SELECT * FROM user_campaign
WHERE status IN ('blasting', 'ready', 'followup_hold', 'initial_hold')
AND uid =: uid
ORDER BY status ASC
You should use IN instead of that huge amount of ORs :)
I have a SQL query command. The problem is when I type the whole name of a person it won't appear. For example if I search for "ermel", it will show ermel. However, when I search for "ermel lopez" it will fail to output the query. Here's my query:
$query=mysql_query("select * from persons where firstname like '%$searchtext%' or lastname like '%$searchtext%' order by date desc
LIMIT $start,$per_page ");
I tried CONCAT, which works, but won't work on the executable:
SELECT *
FROM persons
WHERE CONCAT( firstname, ' ', lastname ) LIKE 'kaitlyn'
OR `LastName` LIKE 'pineda'
LIMIT 0 , 30
Try reversing the way you check your strings like this:
"select * from persons
where '$searchtext' like concat('%',firstname,'%')
or '$searchtext' like concat('%',lastname,'%')
order by date desc
LIMIT $start,$per_page "
This should result in a match on your searchtext containing either the firstname or lastname.
a LIKE without wildcards is an exact match!
you need
Like 'kaitlyn%'
I think your Sql Query is wrong
when you search Full name (ermel lopez)
Then from your query, you search it in both columns.
But whole text is not stored in a columns
Try this
$searchtext1="ermel";
$searchtext2= "lopez";
$query=mysql_query("select * from persons where firstname like '%$searchtext1%' or lastname like '%$searchtext2%' order by date desc
LIMIT $start,$per_page ");
following sql query was working fine, i don't whats wrong i did its stopped fetching records.
$data = mysql_query("SELECT * FROM product_table where pid=$saa1 OR gpid=$saa1 OR
category_id=$saa1 ORDER BY autoid desc limit $no2,20")
or die(mysql_error());
when i remove or clause its works fine for example
$data = mysql_query("SELECT * FROM product_table ORDER BY autoid desc limit
$no2,20")
or die(mysql_error());
please have a look and let me know where i am doing mistake....
regards,
It seems,your query is OK, but when you use WHERE clause, you limit the results , so perhaps there is no recored for display, especially when you use LIMIT for starting offset and number of results.
Your query is ok but there is no record to satisfy your where part. go to your database and create some new rows with your criteria.
Try:
$data = mysql_query("SELECT * FROM product_table where (pid=$saa1 OR gpid=$saa1 OR
category_id=$saa1) ORDER BY autoid desc limit $no2,20")
or die(mysql_error());
I have two tables which I need to select all rows from where the userid is $userid then sort them. I tried to use join but I couldn't even really get started on how to get it right. Can anybody point me in the right direction as to how to make these into one query?
$result1 = mysql_query("SELECT * FROM paypal_sub_info
WHERE userid='$THEuserid' ORDER BY cur_time DESC");
$result2 = mysql_query("SELECT * FROM paypal_pay_info
WHERE userid='$THEuserid' ORDER BY cur_time DESC");
while($row = mysql_fetch_array($result1)){
echo $row['txn_type'];
}
Solution:
SELECT *
FROM paypal_sub_info sub,paypal_pay_info pay
WHERE pay.userid = '$THEuserid'
AND sub.userid = '$THEuserid'
ORDER BY pay.cur_time DESC,sub.cur_time DESC
Try this:
SELECT * FROM paypal_sub_info sub, paypal_pay_info pay
WHERE pay.userid='$THEuserid' AND sub.userid='$THEuserid'
ORDER BY pay.cur_time DESC, sub.cur_time DESC
If you just want 'txn_type', you could make it a SELECT pay.txn_type AS txn_type
Use:
SELECT psi,*, ppi.*
FROM PAYPAL_SUB_INFO psi
JOIN PAYPAL_PAY_INFO ppi ON ppi.userid = psi.userid
WHERE psi.userid = $THEuserid
ORDER BY psi.cur_time DESC, ppi.cur_time DESC
I believe you want:
SELECT field1, field2, ... FROM paypal_sub_info WHERE userid='$THEuserid'
UNION
SELECT field1, field2, ... FROM paypal_pay_info WHERE userid='$THEuserid'
ORDER BY cur_time DESC
So first off consider using mysqli for for any serious project. OMG Ponies answer is how I would suggest doing it, thought you shouldn't have to specify the alias.wildcard fields separately in the select clause. It's also a best practice to actually specify the fields you are trying to fetch rather than *, though we all use * a lot when we're lazy.
Will A's answer makes me smile because it's technically what you asked for, though not what I expect you wanted.
Do you have a more detailed description of what data you're trying to extract, or was this just an example because you are having trouble figuring out joins?
-J