Sorting using php, mysql - php

.Hi guys, i need a little help with a project that i'm currently working on.
I have this database where i have a table containing records of students from different departments namely:
Dept1
Dept2
Dept3
Dept4
Dept5
in my batch.php i have two dropdown menus for Batch(which is the name of the tables) and Department(a field used for sorting).
what i want to do is that when the user chooses a Batch and a Department and then clicks the submit button, it will automatically assign the values chosen to a MySQL query for the SELECT method. the SELECT query should now retrieve all records from the table sort it by department but will display first the Department that was chosen above.
.Any help would be much appreciated. Thanks in advance and more power!

You can sort by a boolean expression, which will return 1 when true and 0 when false.
<?php
$batch = mysql_real_escape_string($_POST['batch']);
$department = mysql_real_escape_string($_POST['department']);
$query = "SELECT * FROM `" . $batch . "`" .
" WHERE department = '" . $department . "'" .
" ORDER BY department = '" . $department . "' DESC, department;
?>
Update: You should validate the $batch argument that it is a valid table expected, the code as is is vulnerable to SQL injection.

Related

MySQL fastest way to query rows

My problem is that i don't yet have a good server to test this on but i'd like to know if it faster to use:
$sqlgetg = "SELECT assignments.id FROM assignments LEFT JOIN userassignments ON assignments.id = userassignments.assignmentid WHERE userassignments.userid = '" . $row['id'] . "' AND userassignments.assignmentid = '" . $assignmentid . "'";
or
$sqlgetg = "SELECT NULL FROM assignments LEFT JOIN userassignments ON assignments.id = userassignments.assignmentid WHERE userassignments.userid = '" . $row['id'] . "' AND userassignments.assignmentid = '" . $assignmentid . "'";
Since i have to check if there even is an assigment for the user with assignment id of x? I don't need anything else but this: if(mysqli_num_rows($resultgetg) > 0)? In both cases phpMyAdmin gave me the row number that i wanted. (I checked it with
without WHERE and it still worked.)
EDIT:
I don't know how and why NULL works but it does...
You can select any static value "from" a table if you just want to count the rows returned... SELECT NULL or SELECT 42 or whatever. But both of your strategies are in fact suboptimal, because they require unnecessary work by the server and unnecessary network traffic and unnecessary handling of the result set that you are only planning to discard.
If counting rows is what you want, then actually do that. you should SELECT COUNT(*) AS how_many_rows ... and let the server count the rows and return a single row with a single column ("how_many_rows" -- make up your own alias) containing the count.

Code to Query two Joomla3 / Virtuemart3 tables and get field values

My SQL and php skills are very limited especially when it comes to Joomla. For at least a day now I am trying to write a query for two Joomla3 Virtuemart3 tables and get the datas but I cannot.
The tables are #__virtuemart_order_userinfos and #__virtuemart_orders common fields for both are the virtuemart_order_id the fields that I need, to start with, from the two tables are:
__virtuemart_order_userinfos :
virtuemart_order_id
company
last_name
first_name
__virtuemart_orders :
virtuemart_order_id
order_number
order_total
The rest I can add, I think....
I probably need to have a JOIN for the two tables and select the correct fields based on virtuemart_order_id
Could you write for me the code for Joomla so I can add it to a php file I have created for Invoice and Receipt?
Thank you in advance
please try the query below in joomla ..
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array('a.virtuemart_order_id','a.order_total','a.order_number', 'b.company', 'b.last_name','b.first_name'))
->from($db->quoteName('#__virtuemart_orders', 'a'))
->join('Left', $db->quoteName('#__virtuemart_order_userinfos', 'b') . ' ON (' . $db->quoteName('a.virtuemart_order_id') . ' = ' . $db->quoteName('b.virtuemart_order_id') . ')');
$db->setQuery($query);
$results = $db->loadObjectList();
You can also apply conditions in where clause or can use order clause for ordering for the result. please check the link for further guidance - https://docs.joomla.org/Selecting_data_using_JDatabase

Sum 3 columns in row for a total column - MySQL & PHP

I have a database which has many columns, but 3 of them I need to sum up. a, b & c .... I need to add a total column for every row (40+ rows) so each time a new score is entered into a,b or c ... the total column is updated for that particular row.
At the moment, I have the following Update that runs to enter a new score.
mysql_query("UPDATE national_reqs SET " . $phase . " = '" .$score . "' WHERE dog_name ='" . $dog_name . "'");
Then when I pull the data back out, I use the following query (and I want to sort by a new "Total" column)
$result = mysql_query("SELECT regfor,cat_num, ipolevel,handler,dog_name,a,b,c,score_time FROM 2015_national_reqs WHERE (id<=190) ORDER BY cat_num ASC") or die(mysql_error());
I think I need a trigger to make this work properly ? I can create a new column for "Total" of type int.
Thoughts ?
update [table] SET a = [value], d = (a+b+c) where id = [id];
Cheers

Creating a comments system with showing authors

I want to create a simple comments system with PHP. I have a registration system so only logged users can add comment.
I have a row in my table comments called idauthor. I using this sql query to add a new comment:
$sql = 'INSERT INTO kawaly SET
tekst="' . $tekst . '",
autor="' . $autor . '",
datapasty=NOW(),
tytul="' . $tytul . '",
idauthor="' .$id.'"';
my variable $id in this query cames from my logging system:
$ilu_userow = $rezultat->num_rows;
if($ilu_userow>0)
{
$_SESSION['zalogowany'] = true;
$wiersz = $rezultat->fetch_assoc();
$_SESSION['id_user'] = $wiersz['id'];
Okey then. When i adds a new comment, i can see in row idauthor a correct value.
Now i can show it by simple relation query using INNER JOIN user ON user.id = idauthor.
But when i have two people logged in, it`s does not sending correctly value for idauthor. How can i modify it on correct way? How can i insert into my database ID of user which actually wrote a comment for a proper method? Thank you for any tips! Regards.

calling a row form MYSQL

I dont know much about MYSQL. But i need to fetch the number of review articles that a user uploaded.
To call this data, I have my database named "abc", inside I have a table named zxc_review_stats .... inside this table there are several columns, but the one I need to fetch is named "reviews"
So how can I fetch the number from the column reviews? this is what ive got so far:
SELECT COUNT(*) from `zxc_review_stats` WHERE `owner` = <<userid>>
That should work.
<?php
// ... your code
// Assuming $user_id is defined and holds the user's id.
$query = "SELECT reviews"
. " FROM zxc_review_stats"
. " WHERE user_id='" . $user_id . "'";
// do something with this query
?>
If all you need to pull is the value of the reviews field for that user:
Select `reviews` FROM `zxc_review_stats` WHERE `user_id`=user //replace user with the actual number

Categories