Mysql get query - php

Im not very good at mysql query thatswhy I ask here for a little help.
I use this query to select an anime tvshow
$getMovieCmd = "SELECT id,name,hoster,season,episode FROM topmovies.movies
WHERE name = '".mysql_real_escape_string($_GET['name'])."' ";
But I dont want to show all animes with only the same name. I want only to show those anime with the same name, same season and same episode.
Example:
tvshow: Naruto /
Season: 1 and 2 /
Episode: 1,2,3,4,5,6,7,8,9,10
I only want to show naruto with season 1 and ALL hoster which has episode 1.
Picture so you see what I want to do
http://s14.directupload.net/images/130819/g6bz3ef8.jpg
(Staffel = Season)
As you see on the picture the episode is selected. Now I want to make, that all different hosters with that episode will be selected.
Hope you understand my shitty english :/
Im searching for a query like this
$getMovieCmd = "SELECT id,name,hoster FROM topmovies.movies
WHERE name = '".mysql_real_escape_string($_GET['name'])."'
IN (SELECT season FROM topmovies.movies WHERE name = '".mysql_real_escape_string($_GET['name'])."') AS season
IN (SELECT episode FROM topmovies.movies WHERE name = '".mysql_real_escape_string($_GET['name'])."') AS episode
";

you need to add more parameters to your query
$getMovieCmd = "SELECT id,name,hoster,season,episode FROM topmovies.movies
WHERE name = '".mysql_real_escape_string($_GET['name'])."' AND season = '".mysql_real_escape_string($_GET['name'])."' AND episode= '".mysql_real_escape_string($_GET['episode'])."' ";

If I understand you correctly what you are looking for is:
$getMovieCmd = "SELECT id,name,hoster,season,episode FROM topmovies.movies
WHERE name = '".mysql_real_escape_string($_GET['name'])."'
AND episode = '".mysql_real_escape_string($_GET['episode'])."'
AND season = '".mysql_real_escape_string($_GET['season'])."'";

And season=$_GET['season'] and episode=$_GET['episode']
That is if you have them in $_GET

Related

SELECT COUNT with Group BY only return value of 2

I have 6 records 3 of which has identical School and I want to get the result of counting how many school there are inside my database but it only returns the value of 2
$tblnum1 = "SELECT COUNT(*) AS ttldata FROM engoralgrade3 WHERE Years = '$yrr' GROUP BY School";
$tblnum = mysqli_query($conn, $tblnum1);
$tblnm = mysqli_fetch_array($tblnum);
echo $tblnm['ttldata'];//input should be 3
This what my data base looked like
I have checked your table, every school do have 2 rows.
maybe u want to count how many distinct school there are, so change the sql to:
select count(distinct School )from engoralgrade3
or u want to distinct the school name, try :
select distinct School from engoralgrade3
You can try this query it will work
$tblnum1 = "SELECT * FROM engoralgrade3 WHERE Years = '$yrr' GROUP BY School";
$tblnum = mysqli_query($conn, $tblnum1);
$tblnm = mysqli_num_rows($tblnum);
echo $tblnm ;
it may be the var $yrr is not identical for all six records in database which cause make returnred value is 2 not 3 .

How can I make a list grouped by different variables?

I'm trying to make a list where the format goes as follows:
CLUB
name
name
name
CLUB 2
name
name
So far, I've managed to autogenerate tables but for each name it displays the name of the club above, even if it's the same club as another one above it.
I've tried to do it through mysqli, I don't know if it would be better through PHP. Here's my query so far:
$sql = "SELECT DISTINCT soc.nombre, soc.apellido, club.nombreClub FROM socios as soc
INNER JOIN club4h as club
ON soc.nombreClub = club.nombreClub";
$result =NULL;
foreach($conn->query($sql) as $row){
$club[] = $row["nombreClub"];
$nombreSoc[] = $row["nombre"];
$apellidoSoc[] = $row["apellido"];
}
And here's how it's looking with that query:
You can just use order by statement in your SQL query
$sql = "SELECT DISTINCT soc.nombre, soc.apellido, club.nombreClub FROM
socios as soc
INNER JOIN club4h as club
ON soc.nombreClub = club.nombreClub
ORDER NY club.nombreClub DESC, soc.nombre DESC";
So this way you will get your data sorted out of the database and all you need to do is display it.

Data display on PHP (Select Query)

I have a table which contains:
transaction_id | the_pet | name_of_the_owners
1 dog shiela
2 dog ben
3 dog alice
4 cat jonathan
and on my query:
$query="select * from table ORDER BY name_of_the_owner limit 5";
$r=mysqli_query($query);
while ($row=mysqli_fetch_array($r)) {
echo "<tr>";
echo "<td><a href='../php/ownersname.php?the_pet=".$row['the_pet']."'>".$row['the_pet']."</a></td>";
echo "</tr>";
}
However, when I use the $query, it shows all the data from the table.
What I need is this:
the_pet
dog (hyperlink)
cat (hyperlink)
So whenever I click on the hyperlink, the name_of_the owners will be shown in another page
for the dog when clicked
name_of_the_owners
shiela
ben
alice
I already used
$query = "SELECT MAX(transaction_id) as transaction_id, the_pet GROUP BY the_pet, name_of_the_owners;
but when I clicked on the hyperlink, it doesn't show the owners. :(
so if I understand it right you dont want to show duplicates right ?
I think the best way to do this, is to change your select query to
$query = "SELECT DISTINCT the_pet FROM table";
and if you dont want to do that you could filter a array for duplicates
like so:
$arr = array('php','jsp','asp','php','asp');
$unique = array_unique($arr);
print_r($dups);
both will do the trick!

SQL ERROR When i join 2 tables

Sorry let me revise. I have a three tables:
events_year
• EventID
• YearID
• id
Date
• YearID
• Year
Event
• EventID
• EventName
• EventType
i want to dispay a record from the three tables like so:
EventName - Year: Marathon - 2008
i linked it to a table called "members" which contains a ID number field (members-id)
so i can limit the results to members id = $un(which is a username from a session)
I need to join the three tables and limit the results to the specific ID number record
Here is my portion of the code:
$query = "SELECT * FROM members JOIN events_year ON members.id = events_year.id ";
"SELECT * FROM Event JOIN events_year ON Event.EventID = events_year.EventID WHERE username = '$un'";
"SELECT * FROM Date JOIN events_year ON Date.YearID = events_year.YearID WHERE username = '$un'";
$results = mysql_query($query)
or die(mysql_error());
while ($row = mysql_fetch_array($results)) {
echo $row['Year'];
echo " - ";
echo $row['Event'];
echo "<br>";
}
the notices are almost self-explaining. There are no 'Year' and 'EventName' fields in the resultset. It's difficult (or: impossible) to tell why this happens as you haven't given your table-structure, but i guess this: 'Year' is a field of the date-table, 'EventName' is a field of the event-table - you're only selecting from members so this fields don't occur.
I don't understand why there are three sql-statements but only one is assigned to a variable - the other two are just standing there and do nothing. Please explain this and put more information into your question about what you're trying to achive, what your table-structure looks like and whats your expected result.
I think what you really wanted to do is some kind of joined query, so please take a look at the documentation to see how this works.
finally, i think your query should look like this:
SELECT
*
FROM
members
INNER JOIN
events_year ON members.id = events_year.id
INNER JOIN
Event ON Event.EventID = events_year.EventID
INNER JOIN
´Date´ ON ´Date´.YearID = events_year.YearID
WHERE
members.username = '$un'
Does the field 'Year' exist in the query output ? I suspect not.
the string $query is only using the first line of text:
"SELECT * FROM members JOIN events_year ON members.id = events_year.id ";
and not the others.
The query itself is not returning any fields that are called Year or EventName.
Do a var_dump($row) to find out what is being returned.

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