This question already has an answer here:
Group by substring
(1 answer)
Closed 4 years ago.
how can i get count of all domain from users from database
example:
id user refurl
1 ultraman https://google.com/asd/a3sd/asd.html
2 Kingogthe https://google.be/asd/as2d/asd.html
3 biomanliv https://google.ttr/asd/a1sd/asd.html
4 amanrras https://google.mmo/asd/a4sd/asd.html
5 talllos https://yahoo.cosdm/asd/a5sd/asd.html
6 bicenoza https://yahoo.gd/asd/as6d/asd.html
7 Linliveagi https://www.bing.cp/asd/as6d/asd.html.
8 fkbareshuy https://www.bing.cdd/asd/as6d/asd.html
The output to be like:
4 from google
2 from yahoo
2 from bing
etc.
I tried "SELECT COUNT(*) ......" but didn't work how i wanted to
and i tried to use parse_url['host']
$url = parse_url['host'];
$getdomainonly = explode('.',$url);
echo $getdomainonly[0];
//here you get domain without com net Example: https://google.com/asd/aasd to only google
but i don't know how to use this php code with SQL to get the output
if you want to know:
I run script on php 5.6 but i can change it if there is answer for 7.0,7.1 or 7.2
I use MySql to saved data
Because you have tagged php, I'm going to guess that you are using MySQL. If so, you can do:
select substring_index(substring_index(refurl, '.', 1), '//', -1) as domain,
count(*)
from example
group by domain;
Related
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 1 year ago.
I'm trying to make a website to keep the best time records on my website. Currently I'm struggling to get the most popular levels.
I'm using MySQL in combination with phpMyAdmin (for the manual input) and PHP to manage my tables. Currently I'm using a table recordData to keep track of certain records. The table consists of a uniqueID(int) (A.I.), time(int), timeUsername(varchar) (the player with the time), timeLevelID(int) (the level played) and some other irrelevant data.
What I want is an output of the int data in timeLevelID that got used the most. Please see the following data to simplify this concept:
uniqueID
timeLevelID
1
6
2
2
3
31
4
31
5
6
6
6
Where the desired output is a sorted count table, descending by count data:
timeLevelID
count
6
3
31
2
2
1
What I've tried so far:
First attempt, I tried messing around with the SQL query, but somehow I never got it to work.
require_once "dbConnect.php";
$allRecordsDataSQL="SELECT timeLevelID COUNT(timeLevelID) AS timeLevelIDFrequency FROM recordData GROUP BY timeLevelID ORDER BY timeLevelIDFrequency DESC";
$allRecordsData = $conn->query($allRecordsDataSQL);
print_r($allRecordsData);
while($row=$allRecordsData->fetch_array(MYSQL_ASSOC)){
echo $row["timeLevelID"];
}
This creates the following error, and also doesn't return anything on the print_r - I assume $allRecordsData is false?
Fatal error: Uncaught Error: Call to a member function fetch_array() on boolean
In my second attempt I tried catching all the data in a new array. The new array would count the amount of levels each level has, After which I sort the array and output it's data.
require_once "dbConnect.php";
$allRecordsDataSQL="SELECT timeLevelID FROM recordData";
$allRecordsData = $conn->query($allRecordsDataSQL);
$arrayCounter = array_fill(1, $allRecordsData->num_rows, 0);
while($row = $allRecordsData->fetch_array(MYSQLI_ASSOC)){
$arrayCounter[$row["timeLevelID"]]++;
}
rsort($arrayCounter);
foreach($arrayCounter as $key => $val){
echo "<br>";
echo "$key = $val\n";
}
The second attempt does work PHP wise, but the output is the following, and I've got no clue what to do with this:
0 = 4
1 = 3
2 = 3
3 = 3 ..etc..
I assume my first attempt has a silly mistake but I just can't seem to spot it (I'm new to MySQL & PHP, sorry). Nevertheless, I do think the first attempt is the most efficient so I'd like to solve my issue this way.
You have not included the source code of your dbConnect.php file. It appears to be suppressing the errors which is why you did not get an exception thrown for the error in your SQL query. There's a comma missing after timeLevelID in the SELECT list -
require_once "dbConnect.php";
$allRecordsDataSQL="SELECT timeLevelID, COUNT(timeLevelID) AS timeLevelIDFrequency FROM recordData GROUP BY timeLevelID ORDER BY timeLevelIDFrequency DESC";
$allRecordsData = $conn->query($allRecordsDataSQL);
Using var_dump instead of just print_r will often tell you more (boolean false perhaps) -
var_dump($allRecordsData);
while($row=$allRecordsData->fetch_array(MYSQL_ASSOC)){
echo $row["timeLevelID"];
}
This question already has answers here:
SELECT COUNT(*) AS count - How to use this count
(5 answers)
Closed 1 year ago.
What does this code do? Image to show code
$q57 = mysqli_query($Link, 'select count(*) Registos from t57 where c18="1"') ;
$t57 = mysqli_fetch_array($q57) ;
if($t57['Registos'] == 0) {
what i don't understand is that variable "Registos" in front of the count. Is it doing what there?
That is simply an alias, to help you access the result of count(*).
Otherwise that would become the column name in the result set - remove Registos from the query, and then do a var_dump($t57);, you’ll see what I mean.
The as keyword between the two is optional; select count(*) as Registos would be the “long” way of writing this.
https://www.mysqltutorial.org/mysql-alias/
This question already has answers here:
Passing an array to a query using a WHERE clause
(17 answers)
Closed 6 years ago.
$department_id=19
$deptshead(17,19,3)
$sql_depthead ="SELECT * FROM nonaudit_employee WHERE status = 'Active' AND rights = 3 AND deptshead IN(".$department.")AND cand_id!= ".$cand_id;
its working fine but
it only match first dept head cant check the second dept value???
I'm not sure if I correct understood.
You can try to use:
$inClause = implode(",", $deptshead);
And after that use $inClause var in your query
This question already has answers here:
How can I make SQL case sensitive string comparison on MySQL?
(12 answers)
Closed 6 years ago.
$aa = India::find();
$players = $aa
->where('player LIKE :query', [':query'=>'S%'])
->orderBy('position, player')->all();
$countnumber = $aa->count();
This code returns results without case sensitivity.
Gives results where Player name starts with 'S' or 's'.
But I want to make, just to select Player names only with 'S'.
How to restrict it to select with case sensitivity in PHP Yii2 Framworok ?
In case you are using MySQL you can make your query with BINARY
So your Yii2 code should be something like this:
$players = India::find()
->where(['LIKE BINARY', 'player', $query_parameter])
->orderBy('position, player')->all();
This question already has answers here:
Find the parameter names of a stored procedure
(3 answers)
Closed 8 years ago.
When I was using asp, I was able to do the following - now I am using php and sqlsrv - how do I achieve the same in php
Set oCmd = Server.CreateObject("ADODB.Command")
Set oCmd.ActiveConnection = oConn
oCmd.CommandText = "dbo.spGetItem"
oCmd.CommandType = &H0004 'adCmdStoredProc
oCmd.Parameters.Refresh
For Each prmTemp In oCmd.Parameters
Response.Write( prmTemp.direction & "<br/>" & prmTemp.name)
Next
Set oCmd = Nothing
The simplest way regardless of client language is to just query the metadata / catalog views:
SELECT name, is_output
FROM sys.parameters
WHERE [object_id] = OBJECT_ID('dbo.spGetItem');
select ORDINAL_POSITION, PARAMETER_NAME, DATA_TYPE, PARAMETER_MODE from information_schema.parameters
where specific_name='Proc_Name'