I don't want data where value =comedy and value=english
but this query is showing data where value is english
else if(($_POST['genre'] == "Action")&&($_POST['language'] == "Hindi")) {
$query = "SELECT movies.Title,movies.Description,movies.Movie_length,movies.Movie_release_date,movies.Featured_image
FROM((relationship INNER JOIN category ON relationship.Category_id=category.Id)
INNER JOIN movies ON relationship.Movies_id=movies.Id)
WHERE NOT category.value='Comedy' AND NOT category.value='English'";
$search_result = filterTable($query);
try this:
else if(($_POST['genre'] == "Action")&&($_POST['language'] == "Hindi")) {
$query = "SELECT movies.Title,movies.Description,movies.Movie_length,movies.Movie_release_date,movies.Featured_image
FROM((relationship INNER JOIN category ON relationship.Category_id=category.Id)
INNER JOIN movies ON relationship.Movies_id=movies.Id)
WHERE category.value NOT LIKE 'Comedy' AND category.value NOT LIKE 'English'";
$search_result = filterTable($query);
or:
else if(($_POST['genre'] == "Action")&&($_POST['language'] == "Hindi")) {
$query = "SELECT movies.Title,movies.Description,movies.Movie_length,movies.Movie_release_date,movies.Featured_image
FROM((relationship INNER JOIN category ON relationship.Category_id=category.Id)
INNER JOIN movies ON relationship.Movies_id=movies.Id)
WHERE category.value NOT IN ('Comedy','English')";
$search_result = filterTable($query);
Related
if(isset($_GET['id']) && $_GET['id'] != null) {
$id = $_GET['id'];
$sql = "SELECT
`maps.name`,
`maps.description`,
`maps.date`,
`maps.mcversion`,
`maps.mapid`,
`maps.category`,
`maps.format`,
`users.username`,
`users.rank`,
`users.verified`,
`users.mcusername`,
COUNT(`views.mapid`) AS `views`,
COUNT(`likes.mapid`) AS `likes`,
COUNT(`downloads.mapid`) AS `downloads`,
COUNT(`subscribes.channelid`) AS `subscribers`
FROM `maps` INNER JOIN `users` ON `maps.userid` = `users.id`
INNER JOIN `views` ON `maps.mapid` = `views.mapid`
INNER JOIN `likes` ON `maps.mapid` = `likes.mapid`
INNER JOIN `downloads` ON `maps.mapid` = `downloads.mapid`
INNER JOIN `subscribe` ON `mapid.userid` = `subscribe.channelid`
WHERE `maps.mapid` = '$id'";
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result) > 0) {
echo “success”;
} else {
header("LOCATION: index.php");
}
$sql = "SELECT * FROM `maps` WHERE `id`=$id";
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result) > 0) {
viewer($id);
} else {
header("LOCATION: index.php");
}
This worked, but I need data from more tables.
$sql = "SELECT
`maps.name`,
`maps.description`,
`maps.date`,
`maps.mcversion`,
`maps.mapid`,
`maps.category`,
`maps.format`,
`users.username`,
`users.rank`,
`users.verified`,
`users.mcusername`,
COUNT(`views.mapid`) AS `views`,
COUNT(`likes.mapid`) AS `likes`,
COUNT(`downloads.mapid`) AS `downloads`,
COUNT(`subscribes.channelid`) AS `subscribers`
FROM `maps`
INNER JOIN `users` ON `maps.userid` = `users.id`
INNER JOIN `views` ON `maps.mapid` = `views.mapid`
INNER JOIN `likes` ON `maps.mapid` = `likes.mapid`
INNER JOIN `downloads` ON `maps.mapid` = `downloads.mapid`
INNER JOIN `subscribe` ON `mapid.userid` = `subscribe.channelid`
WHERE `maps.mapid` = '$id'";
Is this sql join good? Why it does not return any results?
with the normal $sql = "SELECT * FROM maps WHERE id=$id"; everything works, but i need data from the other tables too.
The solution:
$sql = "SELECT
maps.name,
maps.description,
maps.date,
maps.mcversion,
maps.mapid,
maps.category,
maps.format,
users.username,
users.rank,
users.verified,
users.mc_username,
(SELECT COUNT(*) FROM likes WHERE likes.mapid = maps.id) AS likes,
(SELECT COUNT(*) FROM downloads WHERE downloads.mapid = maps.id) AS downloads,
(SELECT COUNT(*) FROM subscribe WHERE subscribe.channelid = maps.userid) AS subscribers,
(SELECT COUNT(*) FROM views WHERE views.mapid = maps.id) AS viewers
FROM maps
INNER JOIN users
ON maps.userid = users.id
WHERE maps.id = '$id'";
Thanks for the help!
IF you would like to secure a complex sql statment, how would you do it?
Is it an ok version?:
if(isset($_GET['id']) && $_GET['id'] != null) {
$id = $_GET['id'];
$stmt = $mysqli->prepare('SELECT id FROM maps WHERE id = ?');
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
if (mysqli_num_rows($result) == 1) {
$row = $result->fetch_assoc();
$secid = $row["id"];
} else {
echo "error2";
}
$sql = "SELECT
maps.name,
maps.description,
maps.date,
maps.mcversion,
maps.mapid,
maps.category,
maps.format,
users.username,
users.rank,
users.verified,
users.mc_username,
(SELECT COUNT(*) FROM likes WHERE likes.mapid = maps.id) AS likes,
(SELECT COUNT(*) FROM downloads WHERE downloads.mapid = maps.id) AS downloads,
(SELECT COUNT(*) FROM subscribe WHERE subscribe.channelid = maps.userid) AS subscribers,
(SELECT COUNT(*) FROM views WHERE views.mapid = maps.id) AS viewers
FROM maps
INNER JOIN users
ON maps.userid = users.id
WHERE maps.id = '$secid'";
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
echo $row["name"];
} else {
echo "error3";
}
} else {
echo "error1";
}
database connection:
$mysqli = new mysqli('127.0.0.1', 'root', 'pass’, 'db’);
So, I'm trying to make option that will sort results from database. I found a way to do this, but I'm not sure it's best.
if(!isset($_GET['sort']) || $_GET['sort'] == 0) {
$userquery = $DBH->query("SELECT users.id, users.username, users.name, users.joined, users.usergroup, user_groups.group_title FROM users INNER JOIN user_groups ON users.usergroup = user_groups.id ORDER BY users.id ASC");
$userquery->setFetchMode(PDO::FETCH_OBJ);
} else {
if($_GET['sort'] == 1) {
$userquery = $DBH->query("SELECT users.id, users.username, users.name, users.joined, users.usergroup, user_groups.group_title FROM users INNER JOIN user_groups ON users.usergroup = user_groups.id ORDER BY users.username ASC");
$userquery->setFetchMode(PDO::FETCH_OBJ);
} else if($_GET['sort'] == 2) {
$userquery = $DBH->query("SELECT users.id, users.username, users.name, users.joined, users.usergroup, user_groups.group_title FROM users INNER JOIN user_groups ON users.usergroup = user_groups.id ORDER BY users.name ASC");
$userquery->setFetchMode(PDO::FETCH_OBJ);
} else if($_GET['sort'] == 3) {
$userquery = $DBH->query("SELECT users.id, users.username, users.name, users.joined, users.usergroup, user_groups.group_title FROM users INNER JOIN user_groups ON users.usergroup = user_groups.id ORDER BY user_groups.group_name ASC");
$userquery->setFetchMode(PDO::FETCH_OBJ);
}
}
Is there any better way to do this?
There's no need to repeat the non-changing parts of the query over and over again. Just put that into a variable and append the ORDER BY clause to that string depending on the condition. Then use the string as the query parameter.
$querystring = "SELECT users.id, users.username, users.name, users.joined, users.usergroup, user_groups.group_title FROM users INNER JOIN user_groups ON users.usergroup = user_groups.id ORDER BY ";
switch($_GET['sort']) {
case 1:
$querystring.= "users.username ASC";
break;
case 2:
$querystring.= "users.name ASC";
break;
case 3:
$querystring.= "user_groups.group_name ASC";
break;
case 0:
default:
$querystring.= "users.id ASC";
break;
}
$userquery = $DBH->query($querystring);
$userquery->setFetchMode(PDO::FETCH_OBJ);
if user select anything besides "all" it will lead to a where statement involving KodDaerah
$where = '';
$sql= "SELECT * FROM maklumatakaun
LEFT JOIN detailakaun ON maklumatakaun.id = detailakaun.id
LEFT JOIN maklumatbilakaun ON maklumatakaun.NoAkaun = maklumatbilakaun.NoAkaun
LEFT JOIN kodjenisakaun ON detailakaun.KodJenisAkaun = kodjenisakaun.KodJenisAkaun
LEFT JOIN kodlokasi ON detailakaun.KodLokasi = kodlokasi.KodLokasi
LEFT JOIN kodkategori ON maklumatakaun.KodKategori = kodkategori.KodKategori
LEFT JOIN koddaerah ON maklumatakaun.KodDaerah = koddaerah.KodDaerah
WHERE maklumatakaun.KodKategori = '$KodKategori'
AND detailakaun.KodJenisAkaun = '$KodJenisAkaun'
AND maklumatbilakaun.BulanBil = '$BulanBil'
AND maklumatbilakaun.TahunBil ='$TahunBil'
".mysql_real_escape_string($where)."
ORDER BY koddaerah.NamaDaerah ";
if($KodDaerah != "all"){
$where = "AND maklumatakaun.KodDaerah = '$KodDaerah' "; //Add your where statement here
//Whatever you want to do
}
else {
$where = "";
}
if user select all then the system will just use the current sql statement that i provided, for now im not sure what's wrong, so here is where user select the KodDaerah from drop down list box
<?php include('dbase.php');
$sql = "SELECT KodDaerah, NamaDaerah FROM koddaerah";
$result = mysql_query($sql);
echo "<select name='KodDaerah' id='KodDaerah' class='input_field' required />
<option>Pilih Daerah</option>
<option value='all'>Seluruh Pahang</option>";
while ($kod = mysql_fetch_array ($result)){
echo "<option value=".$kod['KodDaerah'].">" .$kod['NamaDaerah']."</option>
<option value='all'>Seluruh Pahang</option>";
}
echo "<?select>";
?>
The problem is now even when i select a certain KodDaerah, it will just run the provided sql query without using the WHERE statement in the $where. Can anybody help?
EDIT:
$sql = $sql . $where;
$result = mysql_query ($sql);
$BilAkaun = mysql_num_rows ($result);
try to move up the if statement:, remove mysql_real_escape_string
(escape before)
$where = '';
if($KodDaerah != "all"){
$where = "AND maklumatakaun.KodDaerah = '$KodDaerah' "; //Add your where statement here
//Whatever you want to do
}
else {
$where = "";
}
$sql= "SELECT * FROM maklumatakaun
LEFT JOIN detailakaun ON maklumatakaun.id = detailakaun.id
LEFT JOIN maklumatbilakaun ON maklumatakaun.NoAkaun = maklumatbilakaun.NoAkaun
LEFT JOIN kodjenisakaun ON detailakaun.KodJenisAkaun = kodjenisakaun.KodJenisAkaun
LEFT JOIN kodlokasi ON detailakaun.KodLokasi = kodlokasi.KodLokasi
LEFT JOIN kodkategori ON maklumatakaun.KodKategori = kodkategori.KodKategori
LEFT JOIN koddaerah ON maklumatakaun.KodDaerah = koddaerah.KodDaerah
WHERE maklumatakaun.KodKategori = '$KodKategori'
AND detailakaun.KodJenisAkaun = '$KodJenisAkaun'
AND maklumatbilakaun.BulanBil = '$BulanBil'
AND maklumatbilakaun.TahunBil ='$TahunBil'
".$where."
ORDER BY koddaerah.NamaDaerah ";
and then do not append $where again
$result = mysql_query ($sql);
$BilAkaun = mysql_num_rows ($result);
I'm working on a advanced search function:
$colname_SokvansterImp = "-1";
mysql_select_db($database_Audiologiska, $Audiologiska);
if (isset($_POST['Personnummer_search']))
{
//Visa bara det som söks
$searchword = $_POST['Personnummer_search'];
$query_SokvansterImp = "SELECT * FROM patient left join person on person.Personnummer = patient.Patient left join vanster_implantat on vanster_implantat.Patv = patient.Patient WHERE vanster_implantat.patv LIKE '%".$searchword."%'";
}
else//Visa all data
{
$query_SokvansterImp = "select * from patient left join person on person.Personnummer = patient.Patient left join vanster_implantat on vanster_implantat.patv = patient.Patient";
}
$SokvansterImp = mysql_query($query_SokvansterImp, $Audiologiska) or die(mysql_error());
$row_SokvansterImp = mysql_fetch_assoc($SokvansterImp);
$totalRows_SokvansterImp = mysql_num_rows($SokvansterImp);
This is working for searching with one parameters, however, I'm wondering how to make it accepts multiple parameters? "Personnummer_search" is the name of my field. How can I make so it can search for example Name and/or Surname? I'm using Dreamweaver
Add a new conditional block ?
if (isset($_POST['Personnummer_search']))
{
//Visa bara det som söks
$searchword = mysqli_real_escape_string($_POST['Personnummer_search']);
$query_SokvansterImp = "SELECT * FROM patient left join person on person.Personnummer = patient.Patient left join vanster_implantat on vanster_implantat.Patv = patient.Patient WHERE vanster_implantat.patv LIKE '%".$searchword."%'";
}
else if(isset($_POST['Name_search']) && isset($_POST['Surname_search']))
{
$name = mysqli_real_escape_string($_POST['Name_search']);
$surname = mysqli_real_escape_string(($_POST['Surname_search']);
$query_SokvansterImp = "YOUR SQL QUERY...";
}
else//Visa all data
{
$query_SokvansterImp = "select * from patient left join person on person.Personnummer = patient.Patient left join vanster_implantat on vanster_implantat.patv = patient.Patient";
}
Check out the PHP Site, There is a function called "func_get_args"
http://php.net/manual/en/function.func-get-args.php
Ok, to search for all possible filters, you can do:
$where = array();
if (isset($_POST['Personnummer_search']))
{
$searchword = mysqli_real_escape_string($_POST['Personnummer_search']);
$where[] = "vanster_implantat.patv LIKE '%".$searchword."%'";
}
if(isset($_POST['Name_search']))
{
$name = mysqli_real_escape_string($_POST['Name_search']);
$where[] = "nameField LIKE '%".$name."%'";
}
if(isset($_POST['Surname_search']))
{
$surname = mysqli_real_escape_string($_POST['Name_search']);
$where[] = "surnameField LIKE '%".$surname."%'";
}
if(count($where))
{
$query_SokvansterImp = "SELECT * FROM patient left join person on person.Personnummer = patient.Patient
left join vanster_implantat on vanster_implantat.Patv = patient.Patient
WHERE ".implode(" AND ",$where);
}
else//Visa all data
{
$query_SokvansterImp = "select * from patient left join person on person.Personnummer = patient.Patient left join vanster_implantat on vanster_implantat.patv = patient.Patient";
}
I am using the following tables:
artists
related_artists
The idea is when I enter an artist's page with an artist_id I can use that id to load related artists. The following code works, but how do I put it in a single query? I can't figure it out.
To connect related_artists with artists I created the following code:
$sql = "SELECT related_artist_id FROM related_artists WHERE artist_id = 1";
$res = mysqli_query($db, $sql);
if (!$res) {
echo "Er is een fout opgetreden.";
exit;
} else {
while ($row = mysqli_fetch_array($res)) {
$query = 'SELECT * FROM artists WHERE artist_id = '.$row["related_artist_id"];
print_r($query."<br />\n");
$result = mysqli_query($db, $query);
if ($result) {
while ($test = mysqli_fetch_array($result)) {
echo $test["lastName"]."<br />\n";
}
} else {
echo "It doesn't work";
exit;
}
}
}
You can just try :
select *
from artists
where artist_id in (
select related_artist_id
from related_artists
WHERE artist_id = 1
);
You can use a LEFT JOIN, try this:
SELECT b.*
FROM related_artist a
LEFT JOIN artists b
USING(artist_id)
WHERE a.artist_id = 1
Should return * from artists, where I aliased artists as b and related_artist as a.
Didn't test, does it work for you / return the expected result?
SELECT * FROM artists where artists.arist_id = 1
INNER JOIN related_artist ON related_artist.artist_id = artists.artist_id
This provides a join on the artist_id columns of both tables, having artist_id = 1. I'm not sure if you need an Inner or a Left join