Special keyword in mysql select statement [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Consider the following script:
SELECT * FROM TABLE1 WHERE COLUMN1='$exb';
$exb is a PHP variable which is fed from an HTML select list with 3 values:0,1,2. Values 1 and 2 correspond to column1 values. In case of selecting value 0, I want to include all the values of COLUMN1. Is there a way to implement the above without changing the script? In other words is there any keyword to assign to $exb which will oblige the script to select all the rows of table TABLE1 in case the user selects 0 from HTML select list?
Thank you

It's a little unclear, but I think you are asking is there a special clause you can add to a where clause to return every single row from the database rather than specific criteria matched in a where clause.
You can put in a pretend where clause by saying col1=col1 which is effectively a bogus (though valid) syntax like this:
SELECT * FROM TABLE1 WHERE COLUMN1=column1;
Though it would have to be without the quotes to select every single row of the table.
Putting the quotes around the variable would be very simple in your php however.
Having said that, wouldn't it be much easier to simply omit the where clause entirely if the value selected is 0?

For this you require to build a dynamic query.
$query = "SELECT * FROM TABLE1";
$exb = isset($_GET['exb']) ? $_GET['exb'] : 0;
$goodParam = array(0,1,2);//array of allowed values
if($exb>0){
if (in_array($exb, $goodParam)) {
$query .= " WHERE COLUMN1 = '$exb'";
}
}
echo $query;

I don't think you can do that with MySql query only. You need to use php and ifelse statement where you can check for $exb value before executing query. For example:
if($exb == "0")
$query = "SELECT * FROM TABLE1";
else
$query = "SELECT * FROM TABLE1 WHERE COLUMN1='$exb'";

Related

How to skip counting null or empty cells inside MySQL? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have this database table where one column is email and in front of that have different other columns used to store data for that email. When I use mysqli_num_rows() function it counts the empty fields as well.
How should I modify this query such that I don't get empty cells counted
<?php
$query = "SELECT * FROM user_info WHERE email='$email'";
$data = mysqli_query($connec,$query);
$total = mysqli_num_rows($data);
echo "Registered for ". $total. " Modules";
?>
Thanking in anticipation
"SELECT * FROM user_info WHERE email='$email' and UID IS NOT NULL AND SENT_DATE IS NOT NULL AND TOTAL_BOUNCE_COUNT IS NOT NULL;"
For each column for which a NULL make the record invalid, put in that syntax to exclude records that have a null from the count. Of course I just made up column names in my answer. You'd put the column names in your table in place of SENT_DATE/TOTAL_BOUNCE_COUNT/UID.

Can you use something like the WHERE clause after you did your query? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
So i end up with lots and lots of queries.
Im wondering if you can just run 1 query to get all the info you require for said page.
And then later use php to more or less split the query into different once like if you where to use the WHERE clause in SQL.
My case
I want to get the number of rows for 2 different tasks but from the same table.
1 being where the id is filtered and the other where lets say the user is filtered.
How i typically do it is just make 2 queries and use the where clause to so define the 2.
It just seems rather inefficient to me to make more queries while they all come from the same table.
If there is a way to achieve this that would be awesome to know, I did some tries my self but thus far failed to succeed.
Fiddled around with multidimensional arrays.
Example
$sql = "SELECT * FROM table1 WHERE type = 'type7' ORDER BY date DESC LIMIT 5";
$result = mysqli_query($conn, $sql);
$var = mysqli_fetch_all($result, MYSQLI_ASSOC);
$sql = "SELECT * FROM table1 WHERE type = 'type2' ORDER BY date DESC LIMIT 5";
$result = mysqli_query($conn, $sql);
$var1 = mysqli_fetch_all($result, MYSQLI_ASSOC);
$sql = "SELECT * FROM table1 WHERE type = 'type5' ORDER BY date DESC LIMIT 5";
$result = mysqli_query($conn, $sql);
$var2 = mysqli_fetch_all($result, MYSQLI_ASSOC);
Since its going to be taken from the same table anyways why have multiple queries just to filter it?
This is a pretty broad question and the most basic answer is not language-specific, i.e. it's true of PHP, Java, and other languages.
Imagine you have a page that displays all banking transactions. You would have a SQL statement along the lines of:
SELECT * FROM transactions
If you then wanted to display only a subset of these, you could simply use code (PHP/Java) to filter them and display the result. For instance, in Java you would use lists to store the entire data and streams to filter the ones you want to show. In PHP, you would use array maps.
But your approach has issues: you are going to be selecting a lot more data than need be (most likely). This won't perform well and you will hit UX issues such as pagination.
You can query everything at once and then filter with php using foreach. But I'd guess that is slower then the 2 queries.
Having two queries is not inefficient. Actually it is using the (usually very fast Database) how it is intended to.
If speed is a factor you could always combine your queries into one using subselects.
SELECT ( SELECT COUNT(*) FROM user WHERE name LIKE 'john%'), ( SELECT COUNT(*) FROM user WHERE id < 3)
Alternatively you could set up a (slow) view or have a database trigger that updates the counts in a type of caching table.
Honestly this is probably the wrong place to optimise.
You can do this in one query. Something like this:
select count(*) as total,
sum( <filtering conditions here> ) as filtered_total
from t;
MySQL treats boolean values as numbers with 0 for false and 1 for true, so the sum() counts the number of true values.
you can use UNION or UNION ALL
for example
SELECT userName, mail
FROM user
WHERE mail like '%gmail.com%'
UNION ALL
SELECT userName, mail
FROM user
WHERE userName like '%john%'
to remove duplicates use only UNION

How to drop a table with register form (SQL Injection) (closed) [duplicate]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to fetch bulk data from a website database but could not succeed. Can somebody suggest if SQL injection is possible and how to do in this case.
There are many ways to do SQL Injection to a website similar to the one you provided.
In the where clause it is expecting ac_no. I assume that this value is being passed from the browser as user input. In that case you can pass ac_no value along with or 1 = 1. e.g where ac_no = 123 or 1 = 1. It returns everything from the table RollPdf1.
For string comparison you can add "" = "" to the where clause.
If you want to perform other select operations ( if you know other table names) then you can append select statements delmited by ;.
UNION operator :
If you know the data types of the columns selected in the query then you can use UNION to get additional data from other tables.
e.g
original query : select name, age, sex from table1 where id = 1
sql injected query : select name, age, sex from table1 where id = 1 AND 1 = 2 UNION select username, id, password from userstable or someother table.

Select from database without duplicate [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to develop php page but I have a problem, I would like to get data from data base without duplicate.
$tsql = "SELECT COUNT(ID) FROM FactoriesViolations";
$rowsPerPage = 25;
$stmt = sqlsrv_query($conn, $tsql);
please help me.
thanks in advance.
what all columns are you expecting in your output. If its only ID
$tsql = "SELECT COUNT(DISTINCT(ID)) FROM FactoriesViolations";
if you want all the columns from the table and eliminate the duplicate records
this query will do the needful.
SELECT Col1, Col2,... ColN FROM FactoriesViolations GROUP BY Col1, Col2,... ColN;
here Col1, Col2,... ColN are column names of your FactoriesViolations table.
Use below query. It will work.
$tsql = "SELECT COUNT(DISTINCT ID) FROM FactoriesViolations";
use below way for count of unique records
SELECT COUNT(DISTINCT column_name) FROM FactoriesViolations; // column_name is column which contains duplicate
DISTINCT keyword tells the server to go through the whole result set and remove all duplicate rows after the query has been performed.
Format :
SELECT DISTINCT *
FROM TABLE_NAME
WHERE CONDITION(S)
In your case, the following query should work
$tsql = "SELECT COUNT(DISTINCT(ID)) FROM FactoriesViolations" ;
This will return the count of all unique IDs existing in the table.

How to count certain rows in a MySQL table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I currently have a table that looks like this.
http://i.stack.imgur.com/KFP6Q.png
This is a comment system. the column id gives the comment an ID. the server_id is the ID for the section the comment was posted on. The user_id is the ID for the person who posted it. And lastly, the comment is the comment itself. Here is how I created the comment:
http://pastebin.com/VHUDW6Dm
What I want to do is create a variable, $commentcount, that will count how many comments there are for a server and be able to display them on a page. If someone could direct me to a function that can help me with this or actually create the code here, it would be greatly appreciated.
Since you want the number of comments per server, you can use the SQL GROUP BY clause to aggregate the resulting rows by the unique server_id.
SELECT server_id, COUNT(id) FROM comments GROUP BY server_id;
This will return the count for each server_id group. If you are only displaying this for a single server_id at a time, you can simply use
SELECT COUNT(id) FROM comments WHERE server_id = <your server id>;
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
A very rough draft for you would be to iterate a count.
$q = mysql_query("MYSQL QUERY TO GET ALL THE COMMENTS FOR THE USER");
$count = 1; // Start the count, preferrably at 1
while ($comment = mysql_fetch_assoc($q)) {
$count++; //iterate the count
}
echo $count; // Echo's the count;
use following mysql query
select count(*) as count_comment from comments;

Categories