Changing value of an argument using MySQL Query

Asked By 330 points N/A Posted on -
qa-featured

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);
 
}
SHARE
Answered By 0 points N/A #106481

Changing value of an argument using MySQL Query

qa-featured

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

Related Questions