PHP Fetch All Data Only 1 No - php

I want to fetch all users but not this user (id = 12) how to make this ?
$MyID = '12';
$sql_query = mysqli_query($Conn, "SELECT * FROM users WHERE country='$MyCountry'");
while ($fetch_data = mysqli_fetch_array($sql_query)) {
$firstname = $fetch_data['firstname'];
echo $firstname;
}

Well in it's simplest form:
$sql_query = mysqli_query($Conn, "SELECT * FROM users WHERE country='$MyCountry' and userid <> $MyID");
But this is inadvisable because your values are not being escaped properly. Better to use prepared statements or mysqli_real_escape_string
$stmt = $mysqli->prepare("SELECT * FROM users WHERE country = ? and userId <> ?")
$stmt->bind_param("sd",$MyCountry, $MyId);
$stmt->execute();

select * from user WHERE ID!=$mycountry;

You can do it easily like that
lets say your field is an id = 12
then
SELECT * FROM your_table WHERE id <> 12

Related

Query in php returns null or empty value

I wrote the following query:
$query1 = "SELECT * FROM session WHERE Id_session IN (SELECT * FROM students_in_session WHERE Username = '$email')";
$res = mysqli_query($conn,$query1);
$query2 ="SELECT * FROM students_in_session WHERE Username='$email'";
$res2 = mysqli_query($conn,$query2);
if (!$res) {
die(mysqli_error($conn));
}else{
while ($row = mysqli_fetch_array($res)) {
print_r($row);
$course = $row['Degree'];
$date = $row['Date'];
$hour = $row['Hour'];
$room = $row['Room'];
}
}
if(!$res2){
die(mysqli_error($conn));
}else{
while ($row = mysqli_fetch_array($res2)) {
print_r($row);
$prof = $row['Professor'];
$assis = $row['Assistent'];
}
}
return "\n\nDegree: ".$course."\n"."Date: ".$date."\n"."Hour: ".$hour."\n"."Room: ".$room."\n"."Prof: ".$prof."\n"."Assistent: ".$assis;
}
Currently using phpmyadmin and testing the query returns the expected result,but using the query in the code the variables are all empty.
These are DB's Table:
session
|Id_Session|Date|Hour|Room|Degree|
students_in_session
|Id_Session|Code|Name|Surname|Username|Professor|Assistent|
In query1 you are just try to compare Id_session with the whole table you need to compere with the session id only.
Just replace this
$query1 = "SELECT * FROM session WHERE Id_session IN
(SELECT * FROM students_in_session WHERE Username = '$email')";
with
$query1 = "SELECT * FROM session WHERE Id_session IN
(SELECT Id_Session FROM students_in_session WHERE Username = '$email')";
I thing it will work for you.
$query1 = "SELECT * FROM session WHERE Id_session IN (SELECT * FROM students_in_session WHERE Username = '$email')";
In this query you must need to select only one column like
$query1 = "SELECT * FROM session WHERE Id_session IN (SELECT Id_session FROM students_in_session WHERE Username = '$email')";
Check it.
when you use a IN over a nested query, the nested query can return only one column:
SELECT * FROM session WHERE Id_session IN
(SELECT Id_Session FROM students_in_session WHERE Username = '$email')
However a nested query is not the best approach in your case, because MySQL will have to execute 2 queries. You would better do an INNER JOIN :
SELECT S.*
FROM session S
INNER JOIN students_in_session SS ON SS.Id_Session=S.Id_Session
WHERE SS.Username = '$email'

Search multiple values if present

sorry my English is weak ....
how can i search multi values from db SQL So that there was any.
i can search name && family together but
I want when the user searched name And family leave empty Return result correctly
how can i write this
if (isset($_POST['searchname']) || isset($_POST['searchfamily'])) {
$sql = "select * from myinfo WHERE name='{$_POST['searchname']}' && family='{$_POST['searchfamily']}' ORDER BY id DESC";
}
else {
$sql = "select * from myinfo ORDER BY id DESC";
}
Your 3 main issues here..
the first being WHERE name= now.. name is already used by mysql therefore you shouldn't use it however.. If you do use it run it like this:
WHERE `name`=
You should always backtick database tables and columns to make life easier in the long haul.
The second issue being you used && where it should be AND
the third is you shouldn't be placing your variables straight into your query as you're left open for SQL Injection.
Now I'm running on the assumption you're using $mysqli as your variable however, this may need adjusting to suit the correct variable you are using:
if (isset($_POST['searchname']) || isset($_POST['searchfamily'])) {
$searchName = $_POST['searchname'];
$family = $_POST['searchfamily'];
$sql = $mysqli->prepare("select * from `myinfo` WHERE `name` = ? OR `family`= ? ORDER BY `id` DESC");
$sql->execute([$searchName, $family]);
} else {
$sql = $mysqli->prepare("select * from `myinfo` ORDER BY `id` DESC");
$sql->execute();
}
If you want to search with both then you need to change your if also. And change && to and in your query
if (isset($_POST['searchname']) && isset($_POST['searchfamily'])) {
$sql = "select * from myinfo WHERE `name`='{$_POST['searchname']}' AND family='{$_POST['searchfamily']}' ORDER BY id DESC";
}
else {
$sql = "select * from myinfo ORDER BY id DESC";
}
Edit
As per your comment try this:
if (isset($_POST['searchname']) || isset($_POST['searchfamily'])) {
$where="";
if(isset($_POST['searchname']))
$where=" WHERE `name`='{$_POST['searchname']}'";
if(isset($_POST['searchfamily']))
{
if($where=="")
$where=" WHERE family='{$_POST['searchfamily']}'";
else
$where=" AND family='{$_POST['searchfamily']}'";
}
$sql = "select * from myinfo $where ORDER BY id DESC";
}
else {
$sql = "select * from myinfo ORDER BY id DESC";
}

How to use php variables in mysql query while php variable contains mysql query?

How can I implement something like this in mysql?
$query1 = "SELECT id FROM table WHERE username = 'John'";
$query2 = "SELECT id FROM table WHERE username= 'Parsa'";
$query = "SELECT * FROM table WHERE id BETWEEN $query1 AND $query2";
$result = mysql_query($query) or die('Query faild'.mysql_error());
$myrecord = mysql_fetch_assoc($result);
Try this
$query1 ="SELECT GROUP_CONCAT(id) FROM table WHERE firstname in('John','Parsa')";
$query = "SELECT * FROM table WHERE id IN ($query1)";
you have two identical queries , you could just have one . and use IN , not BETWEEN.
You can put those 3 queries in to one query:
$query = "SELECT * FROM table WHERE id
BETWEEN
( SELECT id FROM table WHERE firstname = 'John' GROUP BY id )
AND
( SELECT id FROM table WHERE firstname = 'Parsa' GROUP BY id )
";
although your query doesn't mean anything; you need "()" for subqueries to work.
$query1 = "(SELECT id FROM table WHERE username = 'John')";
$query2 = "(SELECT id FROM table WHERE username= 'Parsa')";
$query = "SELECT * FROM table WHERE id BETWEEN $query1 AND $query2";
u can use a subselection:
SELECT * FROM table WHERE id BETWEEN ($query1) AND ($query2)
But be careful: The Subselection result must be an Integer.

PHP: Variable into string query

I'm trying to do a query based into a value obtained in a previous query. Something like that:
$variableid = 100;
$query_prev = "SELECT query FROM queries_table WHERE id = 1";
$result_prev = pg_query($pg,$query_prev);
$row_prev = pg_fetch_array($result_prev);
$final_query = $row_prev['query'];
$row_prev['query'] value would be "SELECT * FROM other_table WHERE id = $variableid";
$final_query value at this point is: "SELECT * FROM other_table WHERE id = $variableid"
/* but that I want is this value: */ "SELECT * FROM other_table WHERE id = 100"
Use if statement before you do the next select so:
<?php if ($row_prev['query']= 100){
SELECT .......... etc.
}else
SELECT from other
?>
/Hope that's what you wanted
Solved:
$variableid = 100;
$query_prev = "SELECT query FROM queries_table WHERE id = 1";
$result_prev = pg_query($pg,$query_prev);
$row_prev = pg_fetch_array($result_prev);
$query = $row_prev['query'];
eval("\$final_query = \"$query\";");

Simple mysql query not working

I have this very simple function:
function getCatName($id){
$sql = "SELECT * FROM biznet_category WHERE ID ='".$id."';";
$res = mysql_query ($sql) or die (mysql_error ());
$row = mysql_fetch_assoc ($res);
$name = $row["Name"];
return $name;
}
So with this function I should be able to get the category name, but it doesn't work with the parameter. If I put 8 or 9, the categoryname is displayed correctly.
The id is also passed on like it should, when I print it out, it shows 8 or 9.
I know the solution is quite simple, I just don't see it.
To fix remove the quotes and check the column name for case id or ID. Since the query string is in double quotes you don't have to use the . join
$sql = "SELECT * FROM biznet_category WHERE ID = $id";
You can use curly brackets which I find easier to read
$sql = "SELECT * FROM biznet_category WHERE ID = {$id}";
If you were querying a string rather than an integer you can simply do
$sql = "SELECT * FROM biznet_category WHERE ID = '{$id}'";
$sql = "SELECT * FROM biznet_category WHERE ID ='".$id."';";
To
$sql = "SELECT * FROM biznet_category WHERE ID = ".$id;
Try this
$sql = "SELECT * FROM biznet_category WHERE ID = ".$id;
Is the column name ID spelt correctly?

Categories