I have query that selects goes something like this:
$query = "SELECT members.*
FROM members
JOIN $nombre
ON members.member_ID=$nombre.friends";
$result = mysql_query($query) or die(mysql_error());
$number = mysql_num_rows($result);
$i = 0;
while($msg = mysql_fetch_assoc($result))
{
//store data in arrays
$peer_id[$i] = $msg['member_ID'];
$peer_state[$i] = $msg['home_state'];
$pro_political_views[$i] = $msg['political_views'];
$peer_district[$i] = $msg['district'];
$peer_first[$i] = $msg['first_name'];
$peer_last[$i] = $msg['last_name'];
$peer_issue[$i] = $msg['first_issue'];
$peer_second[$i] = $msg['second_issue'];
$peer_third[$i] = $msg['third_issue'];
$peer_stand[$i] = $msg['iStand'];
$peer_mail[$i] = $msg['email'];
$peer_pic[$i] = $msg['photo'];
++$i;
}
What this essentially does is get all the values from columns in the rows in the members from the members table where the member_ID is present in the $nombre table.
In the members table, there are two columns called "state" and "district". I want to make it so that php could tell me how many different values there are in the state and district columns for this query.
So, how could I go about writing a query or use php to tell how many how many peers are from a given state. I don't want to have to query the db once for each of the fifty states because that would take way too much time for the page to load. So, is there an efficient way to do this?
Thanks
...to tell how many how many peers are
from a given state
For states:
SELECT state, COUNT(m.member_ID) cnt
FROM members m
JOIN $nombre n ON m.member_ID=n.friends
GROUP BY state
and for districts:
SELECT 'district', COUNT(m.member_ID) cnt
FROM members m
JOIN $nombre n ON m.member_ID=n.friends
GROUP BY 'district'
This queries returns distinct values for states and districts and count of peers from them
You could use two queries, like so:
SELECT DISTINCT members.state
FROM members
JOIN $nombre
ON members.member_ID=$nombre.friends
and
SELECT DISTINCT members.district
FROM members
JOIN $nombre
ON members.member_ID=$nombre.friends
This will give you all of the unique states and districts back, which you can use or count or whatever.
Alternatively, add this somewhere in your while loop:
$all_states[$msg['state']] = 1;
$all_districts[$msg['district']] = 1;
Then, after the loop you can:
$state_count = count($all_states);
$district_count = count($all_districts);
The query approach would be faster by itself (assuming an index on the state and district columns), but the latter approach will be faster if you are already executing this loop anyway.
EDIT: To count unique districts in each state, replace
$all_districts[$msg['district']] = 1;
with something like
$all_districts[$msg['state'] . '--' . $msg['district']] = 1;
This will include the state in the array key too, and the count() call later will return all of the unique district-state combinations.
you can save the district in additional array like
$district[] = $msg['district'];
after the loop print_r using a function array_count_values
print_r(array_count_values($district));
Related
I have updated my original post based on what I learned from your comments below. It is a much simpler process than I originally thought.
require '../database.php';
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM Orders WHERE id = 430";
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
echo 'Order Num: ' . $data['id'] . '<br>';
$sql = "SELECT * FROM Order_items
JOIN Parts ON Parts.id = Order_Items.part_id
WHERE Order_Items.orders_id = 430";
$q = $pdo->prepare($sql);
$q->execute(array($line_item_id));
$data = $q->fetch(PDO::FETCH_ASSOC);
while ($data = $q->fetch(PDO::FETCH_ASSOC))
{
echo '- ' . $data['part_num'] . $data['qty'] . "<br>";
}
Database::disconnect();
Unfortunately, only my first query is producing results. The second query is producing the following ERROR LOG: "Base table or view not found: 1146 Table 'Order_items' doesn't exist" but I am expecting the following results.
Expected Results from Query 1:
Order Num: 430
Expected Results from Query 2:
- Screws 400
- Plates 35
- Clips 37
- Poles 7
- Zip ties 45
Now that I understand where you are coming from, let's explain a couple of things.
1.PDO and mysqli are two ways of accessing the database; they essentially do the same things, but the notation is different.
2.Arrays are variables with multiple "compartments". Most typical array has the compartments identified by a numerical index, like:
$array[0] = 'OR12345'; //order number
$array[1] = '2017-03-15'; //order date
$array[2] = 23; //id of a person/customer placing the order
etc. But this would require us to remember which number index means what. So in PHP there are associative arrays, which allow using text strings as indexes, and are used for fetching SQL query results.
3.The statement
$data = $q->fetch(PDO::FETCH_ASSOC)
or
$row = $result->fetch_assoc()
do exactly the same thing: put a record (row) from a query into an array, using field names as indexes. This way it's easy to use the data, because you can use field names (with a little bit around them) for displaying or manipulating the field values.
4.The
while ($row = $result->fetch_assoc())
does two things. It checks if there is a row still to fetch from the query results. and while there is one - it puts it into the array $row for you to use, and repeats (all the stuff between { and }).
So you fetch the row, display the results in whatever form you want, and then loop to fetch another row. If there are no more rows to fetch - the loop ends.
5.You should avoid using commas in the FROM clause in a query. This notation can be used only if the fields joining the tables are obvious (named the same), but it is bad practice anyway. The joins between tables should be specified explicitly. In the first query you want the header only, and there is no additional table needed in your example, so you should have just
SELECT *
FROM Orders
WHERE Orders.Order_ID = 12345
whereas in the second query I understand you have a table Parts, which contains descriptions of various parts that can be ordered? If so, then the second query should have:
SELECT *
FROM Order_items
JOIN Parts ON Parts.ID = Order_Items.Part_ID
WHEERE Order_Items.Order_ID = 12345
If in your Orders table you had a field for the ID of the supplier Supplier_ID, pointing to a Suppliers table, and an ID of the person placing the order Customer_ID, pointing to a Customers table, then the first query would look like this:
SELECT *
FROM Orders
JOIN Suppliers ON Suppliers.ID = Orders.Supplier_ID
JOIN Customers ON Customers.ID = Orders.Customer_ID
WHERE Orders.Order_ID = 12345
Hope this is enough for you to learn further on your own :).
Hi there I have 2 tables
|id|musicname|url|image|type
and the second table
|id|user|songslist|
inside songsids theres an array like this
1,3,5,6,8 etc ...
What Im aiming to do is select * from table1 and echo out the table1 as in an array but instead of tables two array , the actual row of table1.
So basically To take out each row that contains the id in songslist and put them all into a php array.
I have learned a lot about PHP arrays , but I'm not that good with mysql , Any Idea of how can I do that ?
EDIT
$selectmusiclist = mysql_query("SELECT * FROM music");
$songslist = array();
while ($songs = mysql_fetch_assoc($selectmusiclist)){
$songslist[] = $songs;
}
and then table 2 select:
$username="user1";
$selectuser = mysql_query("SELECT * FROM usersmusic where user=$username");
$user = mysql_fetch_assoc($selectuser);
$songslist = $user['songslist'];
NOW I need to tell the array $songslist[] to output only the songs with id $songslist contained ids
I think running a join like this will give you the results you are after.
SELECT * FROM usersmusic as um
join music as m
on um.songslist = m.id
where user = '$username'
If $username is not a static value make sure you escape it; don't want to get SQL injected in the future.
Also note the mysql_ driver is now deprecated you should consider updating to mysqli or PDO.
How should I go if I wanted to query a query result in php?
Supose I have this..
$result = mysql_query("SELECT * FROM tbl_x WHERE tbl_x.attribute = y");
And now I need to query that $result to filter the results I got from its query. Note that I don't want to do this...
$result2 = mysql_query("SELECT * FROM ( SELECT * FROM tbl_x WHERE tbl_x.attribute = y ) AS tbl_x1 WHERE tbl_x1... etc");
The reason I want to avoid that is I'm getting a "Duplicate column" error when executing a query like that.
I'm looking for something like...
$result = mysql_query("SELECT * FROM tbl_x");
$result2 = mysql_query_result($result);
You can apply more than one term in your search conditions, separated by AND:
$result = mysql_query("SELECT * FROM tbl_x
WHERE tbl_x.attribute = y
AND tbl_x.attribute2 = z");
Re your comments above. It sounds like you are using the Entity-Attribute-Value design.
In order to match multiple attributes, you have to do some tricks. Normally, a WHERE clause can only apply to one row at a time. But since each of your attributes are stored on separate rows, you need to do either one of two solutions:
Join multiple rows into one row, so you can use WHERE on all attributes in one condition.
SELECT config_id
FROM attributes AS s
JOIN attributes AS c USING (config_id)
JOIN attributes AS l USING (config_id)
WHERE (s.attr, s.value) = ('size', 'M')
AND (c.attr, c.value) = ('colour, 'green')
AND (l.attr, l.value) = ('cloth', 'cotton);
Search for any of the several attributes, and then if the number of rows that match is equal to the number of attributes you were searching for, you've found them all.
SELECT config_id
FROM attributes
WHERE (attr, value) = ('size', 'M')
OR (attr, value) = ('colour', 'green')
OR (attr, value) = ('cloth', 'cotton')
GROUP BY config_id
HAVING COUNT(DISTINCT attr) = 3;
You can still have your query of query without getting "duplicate column" error. Just alias and name the columns
$result2 = mysql_query("SELECT tbl_x1.col1, tbl_x1.col2,...etc FROM
(SELECT * FROM tbl_x WHERE tbl_x.attribute = y) AS tbl_x1
WHERE tbl_x1... etc");
my foundation on SQL is pretty weak so I hope you could bear with me. I have three tables: contents, categories, and categorization. The setup was chosen since some content will belong to one or more categories.
I want to fetch contents and its corresponding categories.
This is an overly-simplified version of the current script, without error-checking routines:
$q = "SELECT * FROM contents WHERE contents.foo = 'bar'"
$resource = mysql_query($q);
$categoryFilter = array();
$q2 = "SELECT * FROM categorization WHERE ";
while($content = mysql_fetch_assoc($resource))
{
$categoryFilter[] = "content_id='" . $content["id"] . "'";
}
if(count($categoryFilter))
{
$q2 .= implode(" OR ", $categoryFilter);
mysql_query($q2);
}
That's the gist of it. I hope you get what I am trying to do. I don't know if I can actually use JOINS the content_id may be present in multiple rows in categorization. So what I did was to simply append multiple OR's, trying to fetch items one by one. I really would not like to use multiple queries in this scenario. I hope anyone could suggest an approach
Thanks for your time
One query should be enough to fetch data from all three tables:
SELECT categories.category_id #, other fields
FROM contents
INNER JOIN categorization ON contents.content_id = categorization.content_id
INNER JOIN categories ON categorization.category_id = categories.category_id
WHERE contents.content_id = 1 # AND other filters
Tweak the columns in the SELECT clause and/or conditions in WHERE clause according to your needs.
This should do the same thing as in your example:
$q = "
SELECT *
FROM
contents c
categorization ctg ON ctg.content_id = c.id
WHERE c.foo = 'bar'
";
$result = mysql_query($q);
If I understand it correctly, you can do this in one sql statement
SELECT *
FROM contents t1
JOIN categorization t2
WHERE t1.content_id = t2.content_id AND t1.foo = 'bar'
Also ensure that content_id is indexed both in 'content' and 'categorization'. You may find it worthwhile indexing 'foo' aswell, but it depends on how you are actually searching.
I have the following 3 tables in the database.
Programs_Table
Program_ID (Primary Key)
Start_Date
End_Date
IsCompleted
IsGoalsMet
Program_type_ID
Programs_Type_Table(different types of programs, supports a dropdown list in the form)
Program_type_ID (Primary Key)
Program_name
Program_description
Client_Program_Table
Client_ID (primary key)
Program_ID (primary key)
What is the best way to find out how many clients are in a specific program (program type)?
Would the following SQL statement be the best way, or even plausible?
SELECT Client_ID FROM Client_Program_Table
INNER JOIN Programs_Table
ON Client_Program_Table.Program_ID = Programs_Table.Program_ID
WHERE Programs_Table.Program_type_ID = "x"
where "x" is the Program_type_ID of the specific program we're interested in.
OR is the following a better way?
$result = mysql_query("SELECT Program_ID FROM Programs_Table
WHERE Program_type_ID = 'x'");
$row = mysql_fetch_assoc($result);
$ProgramID = $row['Program_ID'];
$result = mysql_query("SELECT * FROM Client_Program_Table
WHERE Program_ID = '$ProgramID'");
mysql_num_rows($result) // returns how many rows of clients we pulled.
Thank you in advance, please excuse my inexperience and any mistakes that I've made.
Here is how you can do it:
<?php
// always initialize a variable
$number_of_clients = 0;
// escape the string which will go in an SQL query
// to protect yourself from SQL injection
$program_type_id = mysql_real_escape_string('x');
// build a query, which will count how many clients
// belong to that program and put the value on the temporary colum "num_clients"
$query = "SELECT COUNT(*) `num_clients` FROM `Client_Program_Table` `cpt`
INNER JOIN `Programs_Table` `pt`
ON `cpt`.`Program_ID` = `pt`.`Program_ID`
AND `pt`.`Program_type_ID` = '$program_type_id'";
// execute the query
$result = mysql_query($query);
// check if the query executed correctly
// and returned at least a record
if(is_resource($result) && mysql_num_rows($result) > 0){
// turn the query result into an associative array
$row = mysql_fetch_assoc($result);
// get the value of the "num_clients" temporary created column
// and typecast it to an intiger so you can always be safe to use it later on
$number_of_clients = (int) $row['num_clients'];
} else{
// query did not return a record, so we have no clients on that program
$number_of_clients = 0;
}
?>
If you want to know how many clients are involved in a program, you'd rather want to use COUNT( * ). MySQL (with MyISAM) and SQL Server have a fast way to retrieve the total number of lines. Using a SELECT(*), then mysql_num_rows leads to unnecessary memory ressources and computing time. To me, this is the fastest, though not the "cleanest" way to write the query you want:
SELECT
COUNT(*)
FROM
Client_Program_Table
WHERE
Program_ID IN
(
SELECT
Program_ID
FROM
Programs_Table
WHERE
Program_type_ID = 'azerty'
)
Why is that?
Using JOIN make queries more readable, but subqueries often prove to be computed faster.
This returns a count of the clients in a specific program type (x):
SELECT COUNT(cpt.Client_ID), cpt.Program_ID
FROM Client_Program_Table cpt
INNER JOIN Programs_Table pt ON cpt.Program_ID=pt.Program_ID
WHERE pt.Program_type_ID = "x"
GROUP BY cpt.Program_ID