Asked By
chesterhunt
330 points
N/A
Posted on - 10/21/2011
I need help with MySQL query. I want to create a function that will do a lot of sorting on my database. Considering the code below, can I change the default value of a WHERE argument, that will sum up to anything? So the default value of the said argument will find everything on the database.
Function get inventory ($order = 'id', $location = 'WA', $recalls = 'anything', $sold = '0') {
Â
$query = "SELECT * FROM v_inventory ";
Â
$query .= "WHERE location = '{$location}' and ";
Â
$query .= "recalls = '{$recalls}' and ";
Â
$query .= "sold = '{$sold}' ";
Â
$query .= "ORDER BY {$order} ASC";
Â
$result = mysql_query($query, $connection);
Â
}
Changing value of an argument using MySQL Query
I believe this is a PHP-MYSQL query.
I don't know if the codes you made is able to get the value of the variables. According to what I have read, you include a local variable in your query by concatenating it using the concatenating operator in PHP which is ".". In that case, you should not use "{ }" and concatenate it instead.
Ex.
$query = "select * from inventory where name = ".$name." order by ".$id." ASC";
And also, you can make the body of your function a single line query.
You can do this instead:
function get inventory ($order = 'id', $location = 'WA', $recalls = 'anything', $sold = '0') {
$query = "select * from v_inventory where location = ".$location." and recalls = ".$recalls." and sold = ".$sold." order by ".$order." ASC";
$result = mysql_query($query, $connection);
}
Â
I have here links the link where you can learn more about the query you are working on:
http://php.net/manual/en/function.mysql-query.php
https://www.w3schools.com/php/php_mysql_select.asp