Select first letter from firstname only - php

I have been battling with this for a while now can anyone offer a solution to this.
I want to only show results where the first letter of firstname is "blah".
$n=$_GET['n'];
("SELECT * FROM my_list
WHERE setid = '16-17'
and firstname like '$n%'
order by studentid asc")
The above is what is have so far but shows no results at all.
thanks all

Maybe use mysql's LEFT to get the first character.
SELECT
*
FROM my_list
WHERE
setid = '16-17'
AND LEFT(firstname, 1) = 'x'
ORDER BY
studentid ASC
Will return all firstnames starting with x.

$n = $_GET['n'];
$value = $n[0]; // will give first letter
("SELECT * FROM my_list WHERE setid = '16-17'and
firstname like '". $value ."%' order by studentid asc")

Related

How to count non-null values in table php

How do I count non-null variables in php database?(SQL)
This is my existing code and database in the picture:
$jack5 = mysql_fetch_array(mysql_query("SELECT * FROM z3 WHERE ID='$users_Names' AND Score IS NOT NULL "));
echo $jack5;
when I echo the result, I get:
Notice: Array to string conversion etc... error
I'm new to this so any help is appreciated!
z3 is my table name
Score is my column name
Try this:
$result = mysql_query("SELECT * FROM z3 WHERE ID='$users_Names' AND Score IS NOT NULL "));
$rowCount = mysql_num_rows($result); // It will return the number of rows in result set
Try this,
$jack5 = mysql_query("SELECT * FROM z3
WHERE ID='$users_Names' AND Score IS NOT NULL");
while ($row = mysql_fetch_array($jack5))
{
print_r($row);
}
Count the number of rows by using COUNT() function.
SELECT COUNT(*) FROM z3
WHERE
ID='$users_Names'
AND Score IS NOT NULL
This will return you the count of rows having the given criteria in WHERE clause.
Try this:
$jack5 = mysql_fetch_array(mysql_query("SELECT count(*) FROM z3
WHERE ID='$users_Names' AND Score IS NOT NULL "));
echo $jack5;

Get all rows not matching with another table

I've a tricky question. I have a table with numbers:
37823782
37823782
37823782
38478934
90003922
And another table with prefixes:
378
3847
384
001
I want to find all numbers matching the longest prefix. I succeded with this code:
$result = mysql_query("SELECT numbers FROM table1 GROUP BY numbers") or die ("Query error code 1");
while($row = mysql_fetch_array($result))
{
$numbers =$row["numbers"];
$result2 = mysql_query("SELECT * FROM table2 WHERE '".$numbers."' LIKE CONCAT(prefix, '%') ORDER BY CHAR_LENGTH(prefix) DESC LIMIT 1");
while($row2 = mysql_fetch_array($result2))
{
// That's it
}
}
Now what i want to simply make the opposite thing. I want to find all numbers not matching any prefix. In short in the above example i made i should get "90003922". I thought to use NOT LIKE CONCAT (prefix, '%') but it's not working. Any idea?
You can try this
SELECT * FROM table2 WHERE '".$numbers."' NOT LIKE 'prefix%'
One-query solution will look like this. Try it
SELECT * FROM table1 LEFT JOIN table2 ON table1.numbers LIKE CONCAT(table2.prefix,'%') WHERE table2.prefix IS NULL

SELECT * FROM table_name ORDER BY column_name?

ok so i coded in a news section and everytime i insert new news,its shows below the old one.
i want to make it ORDER BY id but make it start like backwards.i dont know how to explain but yeh
i want them to be ordered by the newest added by the id so if the 1st row that was inserted's id is 1 then i want it to show below the next id.
so row with id = 2 will be here
so row with id = 1 will be here
thats how i want it to be, instead of it being like this
so row with id = 1 will be here
so row with id = 2 will be here
.
Sorry for my bad explanation i hope this is understandable
heres my code so far
<?php
require("include/config.php");
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id");
while($row = mysql_fetch_array($sqlnews)) {
$dbdate = $row['date'];
$dbnews = $row['news'];
echo "<h1><strong>$dbdate</strong></h1>";
echo "<div class='content'>$dbnews</div><br><br>";
}
?>
add DESC in your ORDER BY clause
SELECT * FROM news ORDER BY id DESC
by default, it is in ASC mode.
SELECT * FROM news ORDER BY id DESC
DESC is the descending keyword ASC is ascending
If you specify neither then default behaviour is ascending
Just use DESC keyword in your sql query.
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id DESC");
However, it isn't really such a good idea to use id, because semantically, there is nothing preventing somebody from changing the sequence which counts up automatically assigning the ID.
Therefore, you should add a column created_at. Everytime you insert a row, you can use the SQL function NOW().
The advantage is that you can say:
SELECT * FROM news WHERE created_at <= NOW() ORDER BY created_at DESC
This means that you can schedule news items ahead of time, and it will automatically display when the date/time arrives!
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id DESC");
try this:
just have to add
order by id DESC
Just replace your code by this code:
<?php
require("include/config.php");
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id DESC");
while($row = mysql_fetch_array($sqlnews)) {
$dbdate = $row['date'];
$dbnews = $row['news'];
echo "<h1><strong>$dbdate</strong></h1>";
echo "<div class='content'>$dbnews</div><br><br>";
}
?>

Select multiple array from mysql

I'm trying to make a search for a property website i'm working on for a friend.
In the Database the property types are named by id numbers, ie: house = 30, flat = 8, terraced =1, and so forth..
How can i retrieve ALL properties from the database when some are detached houses with value of 2 and houses are value of 30 etc :)
It has got me stuck..lol
Here's what i have so far which isn't working...
$bedrooms = $_GET['bedrooms'];
$pricefrom = $_GET['pricefrom'];
$priceto = $_GET['priceto'];
$proptype = $_GET['proptype'];
if($proptype == 'house'){
$search_propsubid = array('1,2,3,4,5,6,21,22,23,24,26,27,30');
}elseif($proptype == 'flat'){
$search_propsubid = array('7,8,9,10,11,28,29,44');
}elseif($proptype == 'bungalow'){
$search_propsubid = array('');
}
$sql = mysql_query("SELECT * FROM `properties` WHERE `PROP_SUB_ID`='$search_propsubid' AND `BEDROOMS`='$bedrooms' AND `TRANS_TYPE_ID`='1' HAVING `PRICE` BETWEEN '$pricefrom' AND '$priceto' ORDER BY `UPDATE_DATE` DESC");
Thank you for your time i hope someone can point me in the right direction..
Regards
Steve
You can try to implode array:
$search_propsubid = array('1,2,3,4,5,6,21,22,23,24,26,27,30');
$comma_separated = implode(",", $search_propsubid);
$sql = mysql_query("SELECT * FROM `properties` WHERE `PROP_SUB_ID` in ($comma_separated) ...
Comme back with news if this don't works for you.
You can use the MySql IN() comparison operator to select all that match the list of values:
$sql = mysql_query("
SELECT *
FROM `properties`
WHERE `PROP_SUB_ID` IN (" .implode(",", $search_propsubid). ")
AND `BEDROOMS`='$bedrooms'
AND `TRANS_TYPE_ID`='1'
HAVING `PRICE` BETWEEN '$pricefrom' AND '$priceto'
ORDER BY `UPDATE_DATE` DESC
");
Assuming $proptype == 'flat', the output will be:
SELECT *
FROM `properties`
WHERE `PROP_SUB_ID` IN (7,8,9,10,11,28,29,44)
...

MySql return only one instance of duplicate entries

Hi I have a table with names and numbers entered along with other data . The table called events contains many instances where the name and numbers are the same but the other entries are different. I want to perform a search using names so I can display the number. But in my search I only need to return one instance of a name . The table is like this
Name Number Responisble Position
Paul 8455 Chorley t7
Dave 3821 PR south f5
Paul 8455 PR North p9
Paul 8455 Leyland t6
Dave 3821 Ribbleton r4
and my script is this
$condition = "name LIKE 'Paul' ";
$result = mysql_query("SELECT * FROM events WHERE $condition ") ;
while($row = mysql_fetch_array($result)) {
The script is not complete but I hope you can see that the results would return 3 results but what i want is a result with just one for each name
I hope this makes sense ! thanks for any help
$condition = "name LIKE 'Paul' ";
$result = mysql_query("SELECT * FROM events WHERE $condition GROUP BY name") ;
try using distinct -
SELECT DISTINCT (columns) FROM (table) WHERE (condition)
edit probably disregard this, mis-understood your question i think
MySql return only one instance of duplicate entries
You can either use group by or distinct in the select statement see http://dev.mysql.com/doc/refman/5.1/en/select.html
In your example it would be
$condition = "name LIKE 'Paul' ";
$result = mysql_query("SELECT * FROM events WHERE $condition GROUP BY name") ;
while($row = mysql_fetch_array($result)) {
SELECT name, number FROM events WHERE $condition GROUP BY name, number

Categories