Needing only 1 row from PHP MySQL database - php

I'm sure I've done this in the past, but it's a few years ago and I don't remember how it's done and the online tutorials aren't helping.
I have a MySQL database. It has 1 table in it called 'data'. In the 'data' table, there are about 15,000 rows, and 31 columns. I need to extract the data from only 1 of these rows, based on a lookup referencing the string in column 1. When the mysql query finds the correct row, I need every single item read into variables that I can show on my page.
I believe this line is the problem:
$sql = "SELECT Mark,Manufacturer,Model FROM data";
Could someone please let me know what it needs to be changed to, to get the desired result? TIA! :)

you can set options of select query
$sql = "SELECT Mark,Manufacturer,Model FROM data WHERE Model (or manufacturer,mark) = 'some text'";

As my colleges have Explained "where' is your friend!
So you can always query as follows :
Select * from Data
Where column_1 = 'Your Desired String'
Alternatively you could use
Select Discinct Limit 1 Mark,Manufacturer,Model FROM data
Order By Mark Asc

Related

PHP MySQL query - select all users but only display the earliest logon times for each user

I want to be able to bring back the earliest logon time per user, so only 1 record (the earliest record) displays for each user
I've tried various ways of GROUP BY but can't seem to get it quite right (if that is actually the correct way of doing). username is the unique value which can be used to GROUP BY
Here's the code I'm currently working with..
SELECT username, name, logon, added FROM data WHERE (date(added) LIKE '$date') AND (logon = (SELECT MIN(logon) FROM data))
I've also tried (below) but only get one result back, only displaying one user
WHERE (date(added) LIKE '$date') AND logon = (SELECT MIN(logon) FROM data)
The first image is what I'm currently getting, the second image is how I want my results to display, please see below
Let me know if you require anymore information, I've tried to put as much as possible
Thanks,
Tom
You are close. Your query needs a correlation clause:
SELECT d.*
FROM data d
WHERE date(d.added) = '$date' AND
d.logon = (SELECT MIN(d2.logon) FROM data d2 WHERE d2.name = d.name);
Note: The logic for added is confusing. First, you should not use like with dates. And, for that matter, you should not be inserting parameter values into the string, you should be using query parameters. And, actually, I don't see the need for that column; your question doesn't mention added.
use this query , problem will be solved
SELECT username, min(logontime) FROM data where date(logontime) = '2016-08-10' group by username
do this:
<?php
$users = array();
$sql = "select * from users";
$result= mysqli_query($conn,$sql);//$conn is your connection object
while($user = mysqli_fetch_assoc($result)){
if(!in_array($user['name'],$users)){
$users[$user['name']]=$user['logOnTime'];//replace key names with your table row names
}
}
//$users is an array that has your user names as key and their last logon time as value
?>

What is the difference between while and between, and what is the better choice in my case?

I have a table that have hundreds of rows, and i want to get specific rows, I used the LIMIT and between id and id
In my application i have two text inputs, one is for START NUMBER and one is for END NUMBER, When i use the LIMIT i need to tell the user to make the right calculation to get the right start and end
So for example if i have a table of 3000 rows, and i want to select 100 rows above 2000 the query will be :
Select * from table LIMIT 2000,100
This will select 100 rows above 2000
The between method :
In the between method, i'm running a while function on all the table and i'm using IF statement to get the right id's here is what i'm doing :
Select * from table
$datastart = $_POST["datastart"];
$dataend = $_POST["dataend"];
$firstid = 0;
$lastid = 0;
$varcount6=1;
$sql = "select ID from users_info";
$sqlread = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($sqlread)){
if($datastart==$varcount6){
$firstid = $rowdirstid["ID"];
}
if($varcount6>=$dataend){
$lastid = $rowdirstid["id1"];
break;
}
$varcount6++;
}
So now i have the first id and the last id of the table, next i use another sql query :
Select * from table where id between $firstid and $lastid
Both worked
My question is: what should i use if i'm loading huge amount of data each time ?
Should i go with while ? or the LIMIT will make the job done ?
To begin with, you should never use PHP to get the data required, stick to doing that solely in SQL, as PHP is never needed.
The limit query you're using will not cut it for what you're trying to do, as it will not care what id's the entries has, so, if your id's are not 100% consecutive, you will not get the desired result.
You should use the between query you display at the bottom of your post.
But, since you haven't provided your full code I cannot say wether or not you sanitized that input, but that is always a good thing to keep in mind. It's preferable to use parameterized queries instead aswell.
If your sure the ID are consecutive, use SELECT * FROM t WHERE id BETWEEN a AND b ORDER BY ID ASC
If you use LIMIT, the SQL Engine have to scan and order all the first results.
(and index the id field)

Calculate number of columns where a row value is same

I am doing the coding in PHP 5.2.17
I want to calculate the number of users whose value for the column named vote is 1
There are 5 things to vote for
when a user votes of a image, the id of the image gets added to the vote (in the above case 1)
So I want to display the statistics i.e. the number of people that voted for images separately.
i.e. the value of the vote should be shown just next to the corresponding image.
help me with the MySQL code and PHP
I tried all types of combinations with count and couldn't get the required result!
this is my latest try
$q = mysqli_query("SELECT count(vote) as Users_who_voted_count FROM data WHERE vote='2'");
$res = mysql_fetch_assoc($q);
echo $res['Users_who_voted_count'];
window.jQuery
First:
do NOT mix mysqli and mysql functions!
Look into the documentation of mysqli and try again.
Second:
Your mysql query should look like this:
SELECT count(*) as Users_who_voted_count,vote FROM data WHERE vote = '2' GROUP BY vote

Mysql query forward and back

I'm wondering what is command for database query that is querying data forward or back between rows? Just like forward and back button. Can someone give me an example? Thanks.
For examp:
$sql_xxxx = mysql_query("SELECT * FROM xxxx WHERE id='$xxx'") or die (mysql_error());
$row_xxxx = mysql_fetch_array($sql_xxxx);
Now I need the two same query but for one forward and in the other backward the data.
Since you tagged PHP and MySQL I think I can guess what you are trying to do;
Usually you can control this with the ID of a record in MySQL, for example:
SELECT * FROM MYTABLE WHERE ID = 1;
In PHP, you can construct dynamic queries to get different results while just changing the value of a variable.
Lets say we now want to take the same query and make it dynamic:
$id = 1;
$query = "SELECT * FROM MYTABLE WHERE ID = $id ";
as long as you execute this query, it will give you the row with ID = 1 , cause now it is taking the value from the $id variable.
If you want the next row, then you execute the same query but now $id must have the next value.
$id = 2;
Having a "NEXT" and "BACK" button can be matter of just adding or substracting 1 to the $id variable.
Still, this is only an example. In most of the cases you should not play with id's like that cause you should not assume that all id's exist, remember that "DELETE" exist;
So you can try to execute better a little query to find the next value like this:
to go foward:
$query= "SELECT MIN(ID) FROM MYTABLE WHERE ID > $id"
// This will get you for sure the closest id next to the current one
and to go back:
$query= "SELECT MAX(ID) FROM MYTABLE WHERE ID < $id"
I hope this helps,
Regards
You can specify a LIMIT in your query, which results in a specific number of rows. For instance, when you end your query with LIMIT 100, 10, you will get 10 results starting at row 100.
This way, you can build prev/next buttons, that when clicked, retrieve the previous or next page of results.
But in most other cases, you will just query everything you need and run through the result set. You do this by querying the result, and fetching each next row of the result.
If you use the LIMIT keyword, the database will happily return a "page" of data, for example;
SELECT * FROM posts ORDER BY post_time LIMIT 40,20; -- will return rows 41-60.
You can make use of that from PHP by just setting the first LIMIT number to page number * page size and the second number to page size (with page number being 0 for the first page). In the example, the page size would be 20 and the page number would be 2 (the third page) and the results will be all the posts from the third page.
There's nothing more to it from the database side, you'll just have to make good use of it from PHP.
if i'm understanding the question correctly, it sounds like you're trying to implement pagination in your queries and you would utilize the 'limit' clause, like so:
SELECT * FROM `your_table` LIMIT 5, 5
This will show records 6, 7, 8, 9, and 10
here's more documentation

"how many search results from mysql" help

I got some help with gettin the number of rows returned from mysql, and it works fine...
BUT, how do I get the number of rows with a certain field value?
Do I have to make a new Mysql search query?
Here is the code where I query mysql and display in a table using fetch_array... Also, Im using mysql_num_rows to get number of rows.
So how do I get number of rows with certain field value also?
$qry_result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($qry_result);
while($row = mysql_fetch_array($qry_result))
Thanks for all help
OBSERVE: Im trying to avoid using another SELECT WHERE clause...
Is there a way to do what I want withouth another search?
In your query, you can use the where clause. (select * from table where column1 = 'value')
Another option would be to have a counter variable that you increment in your while loop:
$counter = 0;
while($row = mysql_fetch_array($qry_result))
{
if($row[0] == "value")
$counter++;
}
After you have this counter, reset the result set using mysql_data_seek($qry_result, 0); and then continue with your original while loop.
There are several ways to reach this, either run additional queries against MySQL or use programm logic to calculate what you need while iterating over the array.
Fetching the number of rows from MySQL is a task that has several solutions as well. You could blindly call SELECT count(*) FROM table WHERE foo = bar, or use the more advanced SQL_CALC_FOUND_ROWS variable of the database.
If you could explain yourself better, I would be glad to provide a good solution!
OBSERVE: Im trying to avoid using another SELECT WHERE clause... Is there a way to do what I want withouth another search?
I'm curious why you don't want to use another SELECT WHERE clause?
From what I interpret of your question, you are asking to have the number of rows of a given query AND, a count of unique variables?
ex:
NAME AGE
Joe 15
Simon 13
Simon 16
Joe 21
Mary 15
Joe 28
Your row count would be 6 and your count (that you are requesting) would be:
Joe x 3
Simon x 2
Mary x 1
If that is what you are asking, why not use 2 queries, 1 for your set of data, and another query where you GROUP BY 'name' and return only UNIQUE 'name' results? That would get you a count of your "certain fields".
Then again correct me if I miunderstood your question.

Categories