I have build a query to search on text using n_make and n_month using if, where and or clause
However - unsure why is n_month not getting executed, search only happening for text if its in n_make and not in n_month
I want a search to display output if its shown in either n_make or n_month
// Filter by search in title
$search = $this->getState('filter.search');
if (!empty($search))
{
if (stripos($search, 'id:') === 0)
{
$query->where('a.id = ' . (int) substr($search, 3));
}
else
{
$search = $db->Quote('%' . $db->escape($search, true) . '%');
$query->where('( a.n_make LIKE ' . $search . ' )')
or
$query->where('( a.n_month LIKE ' . $search . ' )')
;
}
}
else
{
$search = $db->Quote('%' . $db->escape($search, true) . '%');
$query->where('( a.n_make LIKE' . $search . ') OR ( a.n_month LIKE' . $search . ')');
}
Related
$files = $request->file('attachment');
$attachmentDataPush = NULL;
if ($request->hasfile('attachment')) {
foreach ($files as $file) {
$message->attach($file->getRealPath(), [
'as' => $file->getClientOriginalName(),
'mime' => $file->getMimeType()
]);
$filename = 'AttachmentsBox/' . $message->getId() . '/' . $file->getClientOriginalName();
Storage::put($filename, $file->get());
$attachmentDataPush = $attachmentDataPush . "<" . $file()->getClientOriginalName() . ">";
}
}
I want to add the code I wrote in $ attachmentDataPush side by side in <> but it gives an error. "Function name must be a string" What do you think may be the problem. ?
$attachmentDataPush = $attachmentDataPush . "<" . $file()->getClientOriginalName() . ">";
Line gives an error.I will insert the transaction into the database.
$attachmentDataPush = $attachmentDataPush . "<" . $file->getClientOriginalName() . ">";
I functioned as file (). I accidentally put brackets. The top one is correct.
I have the following query:
$venues = Venue::select(['id', 'name'])
->where('name', 'LIKE', "%{$query}%")
->orderByRaw("CASE " .
"WHEN name like '{$query}%' THEN 0 " . // start with
"WHEN name like '% {$query}%' THEN 1 " . // start of a later word
"ELSE 3 " .
"END"
)
->limit(5)
->get();
The issue is the above query is vulnerable to SQL injection.
How can I fix this?
Parameter bindings is explained here:
https://laravel.com/docs/5.6/queries#raw-expressions
But if I do:
$venues = Venue::select(['id', 'name'])
->where('name', 'LIKE', "%{$query}%")
->orderByRaw("CASE " .
"WHEN name like '?%' THEN 0 " . // start with
"WHEN name like '% ?%' THEN 1 " . // start of a later word
"ELSE 3 " .
"END",
[
$query,
$query,
]
)
->limit(5)
->get();
I get different results.
Try adding the percent to the query param, like this:
...
->orderByRaw("CASE " .
"WHEN name like ? THEN 0 " . // start with
"WHEN name like ? THEN 1 " . // start of a later word
"ELSE 3 " .
"END",
[
"{$query}%",
"% {$query}%",
]
)
...
I get this above mentioned error when i was trying to write a search and filter query for gridview,here is code:
Store Model: Relation
public function getStoreNamesCombo(){
return $this->storeName->classId . ' ' . $this->storeName->platformId . ' ' . $this->storeName->familyId . ' ' . $this->storeName->subFamilyName . ' ' . $this->storeName->variantName;
}
}
StoreSearch Model:
$query->joinWith(['author',
'storeNamesCombo']);
if(!empty($this->storeNamesCombo)){
$query->andWhere('storeNames.classId LIKE "%' . $this->storeNamesCombo . '%" ' .
'OR storeNames.platformId LIKE "%' . $this->storeNamesCombo . '%" ' .
'OR storeNames.familyId LIKE "%' . $this->storeNamesCombo . '%" ' .
'OR storeNames.subFamilyName LIKE "%' . $this->storeNamesCombo . '%" ' .
'OR storeNames.variantName LIKE "%' . $this->storeNamesCombo . '%" ' .
'OR CONCAT(storeNames.classId, " ", storeNames.platformId, " ", storeNames.familyId, " ", storeNames.subFamilyName, " ", storeNames.variantName) LIKE "%' . $this->storeNamesCombo . '%"'
);
Gridview
'value' => function($q) use ($storeFamilies, $storeClasses, $storePlatforms){
return implode('.', array_filter([
$storeClasses[$q->storeName->classId] ?? null,
$storePlatforms[$q->storeName->platformId] ?? null,
$storeFamilies[$q->storeName->familyId] ?? null,
$q->storeName->subFamilyName,
$q->storeName->variantName,
]));
Error
Trying to get property of non-object
Error Line: Store Model Relation.
UPDATE:
public function getStoreName()
{
return $this->hasOne(StoreNames::className(), ['id' => 'storeNameId'])->via('transaction');
}
thanks in advance,
Make sure relation is fetched first to be safe.
public function getStoreNamesCombo()
{
if ($this->storeName) {
return $this->storeName->classId . ' ' . $this->storeName->platformId . ' ' . $this->storeName->familyId . ' ' . $this->storeName->subFamilyName . ' ' . $this->storeName->variantName;
}
return null;
}
EDIT:
BUT in your code one thing does not make sense. You are using value of getStoreNamesCombo() as the condition for query searching through storeNames table which is something like an inception idea.
I guess what you want to do is:
Set field in form model that will be filled with search term for the query condition.
public $storeNameSearch;
User fills that through the form for example. Now you can use it for the query.
if (!empty($this->storeNameSearch)){
$query->andWhere([
'or',
['like', 'storeNames.classId', $this->storeNameSearch],
['like', 'storeNames.platformId', $this->storeNameSearch],
['like', 'storeNames.familyId', $this->storeNameSearch],
['like', 'storeNames.subFamilyName', $this->storeNameSearch],
['like', 'storeNames.variantName', $this->storeNameSearch],
]);
}
BTW checking concatenated fields here seems redundant.
You can use getStoreNamesCombo() (->storeNamesCombo) on the prepared model in the gridview.
Edit 2:
If some of the columns are integers it sounds more reasonable to build conditions differently. If you can filter each column separately and there are filter fields user can fill it would be something like:
$query
->andFilterWhere(['storeNames.classId' => $this->storeClassId])
->andFilterWhere(['storeNames.platformId' => $this->storePlatformId])
->andFilterWhere(['storeNames.familyId' => $this->storeFamilyId])
->andFilterWhere(['like', 'storeNames.subFamilyName', $this->storeFamilyName])
->andFilterWhere(['like', 'storeNames.variantName', $this->storeVariantName]);
where first 3 columns are integers and last two are strings.
I'm trying to build a members-system in which you can view certain values from a database.
It worked, so that was great, but I'm having a problem now because I'm using a NULL value on 'age'.
When viewing a profile page, it looks like this:
$id = $_GET["id"];
$result = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember`=$_GET[id]");
$row = mysql_fetch_array($result);
echo "<b>" . $row['userusername'] . "</b>: </p>"; ?>
<?php
$id = $_GET["id"];
$result = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember`=$_GET[id]");
$noage = mysql_query("SELECT ISNULL([age]) FROM membersfromsite WHERE `idofmember`=$_GET[id]");
while ($row = mysql_fetch_array($result))
{
echo "<p class='middle'>id-number: " . $row['idofmember'] . "<br>";
echo "Username: " . $row['userusername'] . "<br>";
if ($noage)
{
echo "Age not specified";
}
else
{
echo "Age: " .$row['age'] ;
}
}
I have tried all kinds of other things, but the problem which I 'm having is that it either returns 'Age not specified' on every userpage or the age on every userpage, including the pages with a NULL value, which makes it look like:
Age:
The code which you can see above returns the age on every page, including the pages with an age which is set to NULL. What I don't understand is if I change the code to this:
$noage = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember`=$_GET[id] AND age IS NULL");
it simply doesn't work. Since I'm using IS NULL instead of = NULL I don't really see why this shouldn't work, but I guess it has to do with the IF which is inside the 'while' thing, I don't really see in what other way I could fix this though...
I'm having an idea what the problem is, because I think that there is already a MyQS, Query done with Results and $noage is maybe ignored because of this, but I don't know how to solve this.
You don't need to do a whole separate $noage query.
Just do:
if(!$row['age'])
{
echo "Age not specified";
}
else
{
echo "Age: " .$row['age'] ;
}
Instead of if($noage) use if(!$row['age']) and skip the second query.
The other reason your code does not work is that the second query returns an array with something like array('expr1' => 0) which is not false. You only get a false result if nothing is found.
The reason why = NULL does not work? There are books written about it, it is just as it is.
Rather than relying on MySQL to tell us if no age was found or not, we can programatically determine whether the age value is Null right in PHP.
Here is an example:
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember` = '" . mysql_real_escape_string($id) . "'");
while($row = mysql_fetch_array($result)) {
echo '<p class='middle'>id-number: ' . $row['idofmember'] . '<br>';
echo 'Username: ' . $row['userusername'] . '<br>';
if($row['age'] === Null) {
echo "Age not specified";
} else {
echo "Age: " .$row['age'] ;
}
}
I am using a 'While' loop to run through an array of users' first names, last names, and emails. When my 'While' loop executes, and I echo the data, it successfully shows the row data from the database, but it does not show Row 1. It starts at row 2 and spits everything out properly, but why is it skipping row 1?
$query= "SELECT * FROM email_list";
$result=mysqli_query($dbc, $query);
$row=mysqli_fetch_array($result);
while ($row=mysqli_fetch_array($result)) {
echo $row['first_name'] . ' ' . $row['last_name'] . ' : '
. $row['email'] . '<br />';
}
mysqli_close($dbc);
One thing I have found, is if I put this section:
$row=mysqli_fetch_array($result);
echo $row['first_name'] . ' ' . $row['last_name'] . ' : '
. $row['email'] . '<br />';
above the 'While' loop, then it does successfully show the first row of data, and all the other rows too, so, if that's confusing, coding it like this (the long way - below) actually works, but it seems redundant and I want to use best DRY practices:
$query= "SELECT * FROM email_list";
$result=mysqli_query($dbc, $query);
$row=mysqli_fetch_array($result);
echo $row['first_name'] . ' ' . $row['last_name'] . ' : '
. $row['email'] . '<br />';
while ($row=mysqli_fetch_array($result)) {
echo $row['first_name'] . ' ' . $row['last_name'] . ' : '
. $row['email'] . '<br />';
}
mysqli_close($dbc);
You have 2 calls to $row=mysqli_fetch_array($result); before you print your result. One before the loop and the other as the expression in the while statement.
Try deleting the one before the while loop.
$result=mysqli_query($dbc, $query);
// kill this line $row=mysqli_fetch_array($result);
while ($row=mysqli_fetch_array($result)) {
echo $row['first_name'] . ' ' . $row['last_name'] . ' : '
. $row['email'] . '<br />';
}