Well im working on a small php script and i made a query to connect to db like this:
$id = mysql_real_escape_string(strip_tags($_POST['keyword']));
$query = mysql_query("select * from kalimat WHERE name LIKE '%$id%' OR content like '%$id%'");
while($row=mysql_fetch_assoc($query))
{
echo "<a href='interpretation-".$row['id'].".html'><li>".$row['name']."</li></a>";
}
Now i want to echo the result of select from names then echo the result of selecting from content thank you in advance.
Try that
select * from kalimat WHERE name LIKE '%$id%' OR content like '%$id%'
order by CASE WHEN name LIKE '%$id%' THEN 0
WHEN content LIKE '%$id%' THEN 1 END
Related
My query:
$sel = mysql_query("select * from categorydetails where productname LIKE '%$commonsearch%' ");
Where product name = tvs apache rtr:
commonsearch = rtr
but it's not working
is there a way to use like command when mysql column contain more than one word?
$sel = mysql_query("select * from categorydetails where productname LIKE '%___rtr%' ");
see the example
SELECT * FROM certificates where csr REGEXP 'tvs'
your query
$sel = mysql_query("select * from categorydetails where productname REGEXP '$commonsearch' ");
Please use below query this is working on single word and multipe string like "test is test"
$sel = mysql_query("select * from categorydetails where productname LIKE '%rtr%' or productname LIKE '%rtr' or productname LIKE 'rtr%' ");
Use this
LIKE '%".$commonsearch."%
I have a page where I want to display articles like the one you're reading (randomly chosen articles from same subcategory). I want to use a php script but the server says I have an error. Here is my script:
$article = mysqli_query($con,"SELECT * FROM sources WHERE ID = '$ID'");
while($row = mysqli_fetch_array($article))
{
code which works perfectly
$samecat = $row['Subcategory'];
}
$samecats = explode(', ', $samecat);
foreach($samecats as $similar){
$scat[] = "Subcategory LIKE %".$similar."%";
}
echo implode(' OR ',$scat);
$samearticle = mysqli_query($con,
"SELECT *
FROM sources
WHERE (".implode(' OR ',$scat).")
AND NOT ID='$ID'
ORDER BY Rand()
LIMIT 0,3 ");
while($row2 = mysqli_fetch_array($samearticle))
{
echo "<a href='article.php?ID=".$row2['ID']."'>» "
.$row2['Headline']."</a>";
}
The connection works perfectly because it works with other components but I have bug here :(((
Any alternative solutions will be fine, but I think this way is better.
error is:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result
Here is your problem:
https://eval.in/96729
I simulated your code and viewed the sql you're generating. It's like this:
"SELECT *
FROM sources
WHERE (Subcategory LIKE %test% OR Subcategory LIKE %foo% OR Subcategory LIKE %bar% OR Subcategory LIKE %baz%)
AND NOT ID=''
ORDER BY Rand()
LIMIT 0,3 "
You need singlequotes around your terms like:
Subcategory LIKE '%test%'
So you need to:
$scat[] = "Subcategory LIKE '%".$similar."%'";
Really you should enable error reporting for your querying.
I'm building a search function for my site, however the MySQl query won't read the PHP variables, and I don't mean errors, it just seems to think they're NULL.
My current code is:
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('library', $conn);
$sql = "SELECT * FROM Books";
if($_POST['find']!="")
{
if($_POST['field'] == "Books")
{
$sql = "SELECT *
FROM Books
JOIN bookauthor ON books.BookID = bookauthor.BookID
JOIN authors ON bookauthor.AuthorID = authors.AuthorID
WHERE books.BookName LIKE '%''".($_POST['find'])."''%'
GROUP BY books.BookName
ORDER BY authors.AuthorID";
}
else if ($_POST['field'] == "Authors")
{
$sql = "SELECT *
FROM Books
JOIN bookauthor ON books.BookID = bookauthor.BookID
JOIN authors ON bookauthor.AuthorID = authors.AuthorID
WHERE authors.Forename LIKE '%J.%'
AND authors.Surname LIKE '%%'
GROUP BY books.BookName
ORDER BY authors.AuthorID";
}
}
$result = mysql_query($sql, $conn) or die("Can't run query");
$loopnumber = 1;
if (mysql_num_rows($result) ==0 ){echo "No Results have been found";}
The POST variable does contain data as I've tested by echo'ing it, however my site just gives the "No Results have been found" message meaning the query retuned no results.
Even if I pass the POST into a normal variable I get the same results.
However if I remove the "LIKE '%%'" and have it look for and exact match from typing in the search on the site it works fine.
Edit: Hmmmm, just made it so I pass the POST into a variable like so..
$searchf = "%".$_POST['find']."%";
and having that variable in the WHERE LIKE makes it work, now I'm just curious as to why it doesn't work the other way.
I seems to love quotation marks too much, and should go to bed.
Well first of all, I am guessing you are getting a MySQL syntax error when trying to execute that first query. This line:
WHERE books.BookName LIKE '%''".($_POST['find'])."''%'
Should be
WHERE books.BookName LIKE '%".$_POST['find']."%'
Because right now you are getting
WHERE books.BookName LIKE '%''ABC''%'
when you should be getting
WHERE books.BookName LIKE '%ABC%'
I don't admit to understand what you are doing with your second query, which just hard codes and has %% as one of the search criteria, which is, in essence meaningless.
Its in your LIKE expression. If in $_POST['find'] the value is LOTR the query would be
WHERE books.BookName LIKE '%''LOTR''%'
and the resault would be empty. Just remove the double ' and it should be work.
Try this way:
$sql = "SELECT *
FROM Books
JOIN bookauthor ON books.BookID = bookauthor.BookID
JOIN authors ON bookauthor.AuthorID = authors.AuthorID
WHERE books.BookName LIKE '%".$_POST['find']."%'
GROUP BY books.BookName
ORDER BY authors.AuthorID";
It should be work
use this, worked for me:
$query_casenumber = "SELECT * FROM pv_metrics WHERE casenumber='$keyword' OR age='$keyword' OR product='$keyword' OR eventpreferredterm='$keyword' OR patientoutcome='$keyword' OR eventsystemclassSOC='$keyword' OR asdeterminedlistedness='$keyword' OR narrative LIKE '%".$_POST['keyword']."%' ";
I have a query like this
$sql = "SELECT SUM(CASE WHEN jr_softwarecheck LIKE \'%sony\' AND
jr_othersoftware LIKE \'%sony%\' THEN 2 ELSE 1 END) AS totalcount FROM
jos_jreviews_content WHERE jr_softwarecheck LIKE \'%sony%\' OR
jr_othersoftware LIKE \'%sony%\'";
I want to output results in HTML pages. I run a Joomla based site.
How can I do that?
Sorry but I'm not so skilled in PHP, I'm learning.
Expected result in HTML page (frontend), example:
SONY Products: 105
Thanks in advance to all!
In your case, use it like this:
$sql = "SELECT SUM(CASE WHEN jr_softwarecheck LIKE \'%sony\' AND jr_othersoftware LIKE \'%sony%\' THEN 2 ELSE 1 END) AS totalcount FROM jos_jreviews_content WHERE jr_softwarecheck LIKE \'%sony%\' OR jr_othersoftware LIKE \'%sony%\'";
$res = mysql_query($sql); // This will run the query on the connected datababse
if($row = mysql_fetch_array($res)){ // Since you are using just a SUM to count results, you don't need to loop
echo "Sony Products: ".$row['totalcount']; // $row['totalcount'] is the result of the totalcount from your MySQL query put into the $row variable
}
I hope this helps you out :)
$result = mysql_query($sql) or die (mysql_error());
while($row = mysql_fetch_assoc($result)){
//do something
}
I have searched high and low and cant find a similar issue to what i have.
I am a beginner so please forgive my clunky query structure.
I am trying to ( have attached screen grab below of output ):
Query the photos table to get the id based on category id and also start,limit because of pagination.
Query the photos tagged table based on the photo id i just got from the first query.
But my problem is that i cant group the tags, some photos have the same tag name. And the output just shows all the tags for each photo. I want restaurant to show only once etc...
<?php
// Get the file ideez and dont go beyond pagination start,limit eg:30,10
$queryFile = "SELECT id FROM $tableName WHERE cat_id=".$fileID." LIMIT $start, $limit";
$resultFile = mysql_query($queryFile);
while ($rowFile = mysql_fetch_array($resultFile)) {
// Get the tag names based on the file ideez retrived from the above query
$queryTagged = "SELECT tag_name FROM photoTagged WHERE file_id=".$rowFile['id']." GROUP BY tag_name";
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
while ($rowTagged = mysql_fetch_array($resultTagged)) {
$tagged = $rowTagged['tag_name'];
?>
<li><a href="#"><?php echo $tagged; ?></li>
<?php }} ?>
the above query is producing:
bar,cappucino,coffee,coffee machine,restaurant,bar,cappucino,coffee,coffee machine,restaurant,bar,coffee,restaurant,bar,coffee,coffee machine
restaurant,bar,cappucino,coffee,restaurant
what i need to show is:
bar,cappucino,coffee,coffee machine,restaurant
If anyone could help i would greatly appreciate it.
Thank you in advance.
John
My new code is
<?php
// Get the file ideez and dont go beyond pagination start,limit eg:30,10
$queryFile = "SELECT id FROM $tableName WHERE cat_id=".$fileID." LIMIT $start, $limit";
$resultFile = mysql_query($queryFile);
while ($rowFile = mysql_fetch_array($resultFile)) {
// Get the tag names based on the file ideez retrived from the above query
$queryTagged = "SELECT DISTINCT tag_name FROM photoTagged WHERE file_id=".$rowFile['id'];
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
$rowTagged = mysql_fetch_array($resultTagged);
$tagged = $rowTagged['tag_name'];
?>
<li><a href="#"><?php echo $tagged; ?></li>
<?php } ?>
I now get this: ( So i am close arent i? )
----------
cappucino
restaurant
bar
coffee machine
restaurant
coffee
coffee
restaurant
restaurant
restaurant
coffee
coffee
restaurant
restaurant
coffee machine
restaurant
coffee
I wonder if the spaces are something? i got that from copy and paste...
Any further help would be appreciated :-)
You should first perform a join between your photos and tags table, and THEN select the distinct tags.
I believe this query will let the database do all the work for you:
SELECT DISTINCT tag_name
FROM (SELECT file_id FROM $tableName WHERE cat_id=$fileID LIMIT $start, $limit) t1
LEFT JOIN photoTagged ON t1.id = photoTagged.file_id
You can also sort the tags in the database (ORDER BY tag_name).
Haven't tried it myself, so maybe the syntax is a bit off. But the idea should work.
distinct doesnt work if you are only getting one record at a time, so put the data in a PHP array and then use array_unique, which is PHPs way to do distinct
<?php
// Get the file ideez and dont go beyond pagination start,limit eg:30,10
$queryFile = "SELECT id FROM $tableName WHERE cat_id=".$fileID." LIMIT $start, $limit";
$resultFile = mysql_query($queryFile);
while ($rowFile = mysql_fetch_array($resultFile)) {
// Get the tag names based on the file ideez retrived from the above query
$queryTagged = "SELECT tag_name FROM photoTagged WHERE file_id=".$rowFile['id'];
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
$rowTagged = mysql_fetch_array($resultTagged)
$tagged[] = $rowTagged['tag_name'];
}
// Let PHP do the work.
$tagged=array_unique($tagged);
while (list(,$val) = each($tagged)) {
echo "<li><a href="#">$val</li>
}
?>
you need to do a sub-query to dodge the pagination problems with the photos. If you wish the selected tags to be a subset of the photos found in your first query, then you will need to do the following.
<?php
$queryTagged = "SELECT TAG.tag_name, count(TAG.tag_name) AS num FROM photoTagged as TAG JOIN (SELECT id FROM $tableName WHERE cat_id=$fileID LIMIT $start, $limit) as PHOTO ON (PHOTO.id = TAG.file_id) GROUP BY TAG.tag_name";
$resultTagged = mysql_query($queryTagged) or die(mysql_error());
while ($tagged = mysql_fetch_assoc($resultTagged)) {
echo "<li id="'.$tagged['TAG.tag_name'].'"><a href="#">".$tagged['TAG.tag_name']." (".$tagged['TAG.num'].")</li>";
}
?>
This way you will have two queries, on for finding the photos, and one for finding the tags for the photos on that page. This technically takes a little longer as MySQL has to load the query into a temporary table, but it should work fine.
SELECT DISTINCT tag_name FROM photoTagged WHERE file_id=".$rowFile['id'] ?