I have a question for my search bar:
In Account Management, I have list of accounts
And this is the Account table:
*AccountID (Example: 1)
*AccountName (Example: Test)
(...)
Now, When I type Test, or 1, I find my result.
My problem is how to search by typing one of character name, this is the characters table:
*CharacterName (Example: Charac)
*AccountID (1 [the same accountID])
I've tried INNER JOIN but it's not working, and i think it isn't the correct method, this is my SQL code
$sql = "SELECT * FROM account INNER JOIN characterrecord ON characterrecord.AccountId = account.AccountId WHERE account.AccountId LIKE '%" . $name . "%' OR account.Name LIKE '%" . $name ."%' OR characterrecord.Name LIKE '%" . $name ."%' GROUP BY account.accountid LIMIT ".$_GET['page'].",".$page_accountnumber;
$req = mysql_query($sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());
while ($acc = mysql_fetch_array($req)) {
....
What I want:
If I type Charac or char ... I must see 1/Test (Character account infos).
Sorry for my bad english, I'm french, and thank you so much!
Since the tables share certain field names, it may be necessary to specify or alias the fields in the SELECT clause to ensure whatever framework you are handling them with is capable of distinguishing them.
Related
Currently I have a query that searchs for sentences/words, it works almost as expected,
I have a regex expresion that searches for names in a table, expample:
function getNames($str){
$stmt = "SELECT * FROM users WHERE name = :name
OR name REGEXP :reg1
OR name REGEXP :reg2
OR name LIKE :lik1";
$query = self::$connection->prepare($stmt);
$query->execute(array(":name"=>$str,
":reg1"=>"^$str" . "[a-zA-Z\s]*$",
":reg2"=>"^[a-zA-Z]*[\s]*[$str][a-zA-Z]"
":lik1"=>"%" . $str . "%"
));
return $query->fetchAll(PDO::FETCH_ASSOC);
}
Let's suppose my table contains the following values
Bob
Peter
Mark
David
John
If I run my query with Bob as $name value it gets it but I would like to be able to find Bob when I run the query using BobsomeLettersExtra or Bob something as $name value
Is there a way to do this using REGEXP or LIKE ?
SELECT * FROM users WHERE name LIKE '%".$name."%'
above query should be enough to get the result. You should validate data before you enter data to the table if not please use the regex as well
"SELECT * FROM users WHERE name LIKE '%".$name."%' AND REGEXP ^".$name."[a-zA-Z]*$"
UPDATE
sorry if i have misunderstand the question please try this
"Select * from users WHERE '".$name."' LIKE CONCAT(name , '%')"
You may try below Query with where Clause for LIKE :
"SELECT * FROM users WHERE name = ".$name." OR name LIKE '%".$name."%' OR name REGEXP ^".$name."[a-zA-Z]*$"
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
I am retrieving data from a database with php and MySQL as follows
$query = mysql_query("SELECT * FROM pictures WHERE (title LIKE '%$Search%' OR keywords LIKE '%$Search%') AND approved = 'YES' ORDER BY title ASC");
The query is correct and there are no errors and the query works fine for "title LIKE '%$Search%'" but the parameter "OR keywords LIKE '%$Search%'" is not retrieving data. The parameter "AND" also works correctly.
The keywords are stored in the database for example "pizza, restaurants, take away" but I don't see that is a problem.
My question is "What is the correct syntax for applying the "OR" parameter?
Remove the brackets around (title LIKE '%$Search%' OR keywords LIKE '%$Search%')
Those are generally used for subqueries.
$query = mysql_query("
SELECT * FROM pictures
WHERE title LIKE '%$Search%'
OR keywords LIKE '%$Search%'
AND approved = 'YES'
ORDER BY title ASC
");
https://dev.mysql.com/doc/refman/5.0/en/subqueries.html
Here is an example of a subquery, and pulled from the manual on MySQL.com:
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
Edit:
Or try a different quoting method:
$query = mysql_query("
SELECT * FROM pictures
WHERE title LIKE '".%$Search%."'
OR keywords LIKE '".%$Search%."'
AND approved = 'YES'
ORDER BY title ASC
");
You could also try escaping your data:
$Search = mysql_real_escape_string($Search);
as an example. I don't know how you're assigning that variable.
phpMyAdmin test edit:
This is what I used inside phpMyAdmin:
SELECT * FROM table
WHERE col1 LIKE '%pizza%'
OR col2 LIKE '%pizza%'
AND col3 = 'YES'
ORDER BY col1 ASC
using pizza as the search keyword seeing that $Search will be based on the same keyword for you, where columns contain "large pizza" in one, and "pizza, take away, restaurants" in another.
Remember that, whatever you're using/assigning $Search to, must reside inside all your queried columns.
You may also want to make use of explode().
Here is an example pulled from https://stackoverflow.com/a/15289777/
<?php
$search = 'Gold Chain Shirt';
$bits = explode(' ', $search);
$sql = "SELECT name FROM product WHERE name LIKE '%" . implode("%' OR name LIKE '%", $bits) . "%'";
The above will generate this query:
SELECT name FROM product WHERE name LIKE '%Gold%' OR name LIKE '%Chain%' OR name LIKE '%Shirt%'
Sorry for taking some time but this is my working answer to my own question... not the prettiest syntax but it works without any string functions or explode functions. MySql can handle keywords quite well without any other functions being included:
$query = mysql_query("SELECT * FROM pictures
WHERE
title LIKE '%$Search%' AND featured IS NOT NULL AND streetview IS NOT NULL AND (id_user > '1') AND (status = '1')
OR
keywords LIKE '%$Search%' AND featured IS NOT NULL AND streetview IS NOT NULL AND (id_user > '1') AND (status = '1') ORDER BY title ASC");
Thank you all for your contributions
I need to get the MATCH AGAINST scores from multiple tables with a common key and combine their sums. The goal in the end is to create a search algorithm that will return rows with matching group_IDs. I have built a mysql query that has yet to run without error.
In case you want to know, this is the specific error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
result resource
These are the tables I wish to search:
table: jn_groups
group_ID, name
table: jn_chat
chat_ID, group_ID, name
table: jn_events
event_ID, group_ID, title, description
My current query looks like this:
SELECT
g.group_ID AS group_ID,
g.name AS name,
SUM(gscore + cscore + escore) AS score,
MATCH(g.name) AGAINST (" . $query . "') AS gscore,
MATCH(c.name) AGAINST (" . $query . "') AS cscore,
MATCH(e.title,e.description) AGAINST (" . $query . "') AS escore
FROM jn_groups g
LEFT JOIN jn_chat c ON g.group_ID = c.group_ID
LEFT JOIN jn_events e ON g.group_ID = e.group_ID
WHERE
MATCH(g.name) AGAINST (" . $query . "')
OR MATCH(c.name) AGAINST (" . $query . "')
OR MATCH(e.title,e.description) AGAINST (" . $query . "')
ORDER BY score
Any help making a query similar to that run would be greatly appreciated!
Two things which could be the reason for breaking this query:
variable $query
All your AGAINST-operators end with "', but start only with ". Make sure $query contains
valid data and
start with an '.
Summate the partial results
Your SUM won't work, because it doesn't know gscore, cscore and escore. It will look for field names instead. You can change this part of your statement like this (ofc replace hello world with your desired search term):
....
SUM(
(MATCH(g.name) AGAINST ('hello world'))
+ (MATCH(c.name) AGAINST ('hello world'))
+ (MATCH(e.title,e.description) AGAINST ('hello world'))
) AS score,
....
Two advices:
Try running the query against your database with a SQL
tool first (p.e. SQLyog, MySQL Workbench, phpMyAdmin or similar), to see if it is working like you want it to. There are a few other possibilities how to break this (p.e. no or wrong fulltext index definition, wrong field names and such), so you have better control about whats going wrong.
Be careful when using reserved variables like NAME in field names. Either escape them properly or - in my eyes better - avoid them.
I have two MySQL questions.
$query = " SELECT
stationname
FROM
stations
WHERE
stationname >= '". mysql_real_escape_string($_GET['letter']) ."'
ORDER BY
stationname
";
Here is the first query. In the URL is a parameter set $_GET['letter'] containing an Alphabetic character. I'm trying to select all the rows where stationname starts with $_GET['letter']. So i found this solution in an other Stackoverflow topic, but it doesn't seem to work, i get all my rows, and not just that single one. edit : seems it checks for all the characters in stationname, and not just the starting letter, how can i get that?
$query = " SELECT
stationname
FROM
stations
WHERE
stationname
LIKE
'". mysql_real_escape_string($_POST['search']) ."'
";
Second and final question. I want to make a search engine for my website, selecting all the rows where stationname contains $_POST['search']. But when i have 2 rows, one for example called cheese and the other one called cheese2, and i search for cheese, only cheese get selected, and when i search for cheese2, only cheese2 will get selected. Is there any way to select both cheese and cheese2?
LIKE supports wildcards. % means any number of characters (including zero), and _ means any one character`
stationname LIKE 'cheese%'
This would match cheese and cheese2.
You can use the % for the first issue too.
stationname LIKE 'a%'
This will find all words that start with 'a'.
I'm trying to select all the rows where stationname starts with $_GET['letter']
MySQL has a LEFT function which seems to be what you're looking for. So basically we extract the first letter of the stationname and compare it agains your letter:
where left(stationname, 1) = '" . mysql_real_escape_string($_GET['letter']) . "'";
Is there any way to select both cheese and cheese2?
Well here the solution is a little smelly, as you should check whether cheese is contained in cheese2 and also whether cheese2 is contained in cheese. Try this:
where stationname like '%" . mysql_real_escape_string($_POST['search']) .
"%' OR '" . mysql_real_escape_string($_POST['search']) .
"' like concat('%', stationname, '%')";
for second.
$query = " SELECT
stationname
FROM
stations
WHERE
stationname
LIKE
'". mysql_real_escape_string($_POST['search']) ."%'
";
The text wildcard in MySQL is %, so for your first query you would probably want:
$query = " SELECT
stationname
FROM
stations
WHERE
stationname LIKE '". mysql_real_escape_string($_GET['letter']) ."%'
ORDER BY
stationname
";
And for your second query:
$query = " SELECT
stationname
FROM
stations
WHERE
stationname
LIKE
'%". mysql_real_escape_string($_POST['search']) ."%'
";
FOR Tel Number
5 finds that the first character
$Sql="SELECT * FROM customers WHERE TEL REGEXP '^[5]' LIMIT 0,500";