I have this problem regarding Drupal 7 Mysql queries especially on LEFT JOIN.
I found this solution but I can't seem it apply it on my problem since I'm not aware how the syntax goes.
https://drupal.stackexchange.com/questions/4317/how-do-i-write-a-left-join-query
This is the solution that I found on the link above.
$terms = db_select('taxonomy_index', 'ti')
->fields('ti', array('tid', 'name'))
->leftJoin('taxonomy_term_data', 'ttd', 'ti.tid = ttd.tid')
->condition('vid', 2)
->condition('nid', $nid)
->execute();
foreach ($terms as $term) {
// $term contains the object for the taxonomy term.
}
Yet I'm having a problem on how do I apply it to my query.
Here is my LEFT JOIN query on mysql.
$query = "SELECT sweep_table.end_offer, sweep_table.title, embed.fbp_id, embed.sweep_stat
FROM sweep_table, embed
WHERE sweep_table.uid=embed.uid AND sweep_table.promo_id=embed.sweep_id";
I already did the first few lines but the rest, I don't know how.
$terms = db_select('sweep_table', 'embed')
->fields('sweep_table', array('end_offer', 'title'))
->fields('embed', array('fbp_id', 'sweep_stat'))
->leftJoin('taxonomy_term_data', 'ttd', 'ti.tid = ttd.tid') //Don't know how to apply to my query.
->condition('vid', 2)
->condition('nid', $nid)
->execute();
foreach ($terms as $term) {
}
Also, was wondering how do I retrieve the data after I successfully LEFT JOIN it?
Would be glad if you help me guys.
Wouldn't have thought that this would work though. Thanks to rekire for the hint.
$query = "SELECT sweep_table.end_offer, sweep_table.title, embed.fbp_id, embed.sweep_stat FROM sweep_table, embed WHERE sweep_table.uid=embed.uid AND sweep_table.promo_id=embed.sweep_id";
$result = db_query($query);
foreach ($result as $row) {
echo $row->end_offer . " " . $row->title . " " . $row->fbp_id . " " . $row->sweep_stat . "<br>";
}
Related
I am just wondering where my mistake was , and if it is a simple fix or if it will be a bit harder to fix. You guys don't have to write the answers for me, just point me in the right direction and I think I should be fine, because I've been looking at this for half an hour now , and I can't seem to figure out where my mistake was..
Inserting comments is working wonderfully, I'm just having issues with getting them, and posting them on my page(?)
// INSERTING COMMENTS INTO THE DATABASE
<?php
function setComments($conn) {
if (isset($_POST['commentSubmit'])) {
$author = $_POST['cauthor'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "INSERT INTO comments (c_author, c_date, c_message)
VALUES ('$author', '$date', '$message')";
$result = mysqli_query($conn, $sql);
}
} ?>
// GETTING COMMENTS FROM THE DATABSE
<?php
function getComments($conn) {
$sql = "SELECT * FROM comments";
$result = mysqli_query($conn, $sql);
$row = $result->fetch_assoc();
echo "$row['c_message']";
}
?>
If the 'SELECT' statement is used to select data from a database then how do we echo specific rows to specific places on a page using php?
To explain this better - I am trying to SELECT * ALL FROM a table but to echo multiple rows to particular places on the html page using php.
So, imagine that my entire mark up and css has 20 thumbnails on a page and each thumbnail has data and an image that is unique to each thumbnail....do I have to replicate the below 20 times?
I am thinking that the best way to do this (which is probably completely wrong) is to use this statement
SELECT * FROM name_of_table WHERE ID = 4 >>> i.e. where I'd like that specific data echoed....
So, if I have 20 thumbnails do I do this 20 times?
<?php
// Connects to your Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT * FROM name_of_table WHERE ID = 4;")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['name'] . "</td> ";
Print "<th>Product:</th> <td>".$info['product_name'] . " </td></tr>";
}
Print "</table>";
?>
And, rinse and repeat but I change the below statement each time for each thumbnail (each thumbnail has unique data that comes from each row on the MySQL)
SELECT * FROM name_of_table WHERE ID = 4;
What is the best way of doing this?
Thanks!
Simple example.. First get the data with wanted ID:s. Create function for data request.
<?php
// Connects to your Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT * FROM name_of_table WHERE ID IN (2,3,4,5,6);")
or die(mysql_error());
// This holds all data rows
$data_array = array();
while($info = mysql_fetch_array( $data ))
$data_array[] = $data;
// Function for rendering data to html
function getItemHtml($id) {
$html = "";
foreach($data_array as $row) {
if ($row['ID'] == $id) {
$html = "<td>" . $row['title'] . "</td>";
// etc.. create item html here
break;
}
}
return $html;
}
// To create one item just call this with item id.
echo getItemHtml(4);
?>
In my quiz system I am trying to actually now make the page of the questions, but it shows nothing when I try to show the question rows and the answer rows to the page.
<?php
$q_qselect = mysql_query("SELECT * FROM `questions`");
$q_qnumrows = mysql_num_rows($q_select);
for($i=0;$i<$q_qnumrows;$i++){
$q_qselect = mysql_query("SELECT * FROM `questions` WHERE `id`='$i'");
$q_aselect = mysql_query("SELECT * FROM `answers` WHERE `question_id`='$i'");
$q = mysql_fetch_assoc($q_qselect);
$a = mysql_fetch_assoc($q_aselect);
echo $q['question'] . "<br />";
echo $a['answer'] . "<br />";
}
?>
And also, another question - how can I actually check that he selected the correct answer? (radio button near each answer) when the field in the answers table is correct?
<?php
$question = mysql_query("SELECT questions.*, answers.* FROM questions inner join answers on questions.qid=answers.id");
while($row = mysql_fetch_array($question)) {
echo $row['question_column_name_in_DB'].'<br />' .$row['answer_column_name_in_DB'].'<br />';
}
?>
change column names to their appropriate names. Please look into PDO's once you have gotten this to work.
You Can Try by using As Like Following....
<?php
$query_result = mysql_query("SELECT questions.*, answers.* FROM questions LEFT JOIN answers on questions.id=answers.`question_id`");
while($row = mysql_fetch_array($query_result)) {
echo $row ['question'] . "<br />";
echo $row ['answer'] . "<br />";
}
?>
I have a large database containing part numbers for a filter company. Several of the products have similar years and model numbers. I have a php script that is searching a text keyword section for each part.
So for a 1997-1999 Honda Crf 250 090987 19.95 the keywords are:
1997 1998 1999 honda crf 250 090987
I got the code from a tutorial and it's written to search the keywords section for everything separated by a space. The problem is that a lot of the listings have honda or similar year spreads.
I need to figure out how to alter the php to narrow the results so if someone types "1997 honda", all of the hondas and '97s don't show up but only ones that are 1997 hondas.
I'm new to php and I've looked all around and can't figure it out. Any help will be thoroughly appreciated.
here's the php:
$keywords = $_GET['keywords'];
$terms = explode(" ", $keywords);
$query = "SELECT * FROM search WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
// connect
mysql_connect("localhost", "root", "root");
mysql_select_db("catalog");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$year = $row['year'];
$manufacturer = $row['manufacturer'];
$product = $row['product'];
$partnumber = $row['partnumber'];
$price = $row['price'];
echo "<div id=\"results\">
<div class=\"words\">$year</div><div class=\"words\">$manufacturer</div><div class=\"words\">$product</div><div class=\"words\">$partnumber</div>$price<br /></div>";
}
}
else
echo "<div class=\"no_results\"><center>No results found for \"<b>$keywords</b>\"</center></div>";
// disconnect
mysql_close();
?>
Interesting problem.
I thought of 2 approaches:
(1) if you have control over the database structure, you could SELECT WHERE (make = 'Honda' AND Year IN (1997,1998,1999)) AND the "like" stuff you already have (without the make/year). But you would need to separate out the model years and make into separate fields.
(2) otherwise, if we can assume the input is always "year(s) make [other keywords], we could do what izuriel suggested while I was typing this, just "AND 'keywords = '1997' AND keywords = 'honda' AND (our existing list of "like" clauses)".
that way you'd get only 1997 Hondas that have any or all of the other keywords.
Use AND in the query instead of OR to enforce that all keywords must match, otherwise one keyword match will return a result always. Outside of that, you'll have to do some Relevance work in code which might slow down the application.
I recommend you to use one symbol percentage at the end of the word and you and instead of or and you limit in your query if the database is large
I've spent a considerable amount of time trying to track this one down. I've used a boxed autocomplete on my site (http://www.devbridge.com/projects/autocomplete/jquery/). As you type the word, it sends it as a GET variable to a page for processing where it is searched for in a mySQL database. The page looks like this:
$get = $_GET['query'];
$query = "SELECT title,author,id,isbn10,isbn13 FROM textbook
WHERE title LIKE '" . $get . "%'
OR author LIKE '" . $get . "%'
LIMIT 5
";
$result = mysql_query($query,$connection);
$resString = "";
$idString = "";
while($data = mysql_fetch_array($result)){
$resString .= "'" . title($data['title']) . " by " . $data['author'] . "',";
$idString .= "'" . $data['id'] . "',";
}
$resString = rtrim($resString, ',');
$idString = rtrim($idString, ',');
$code = "{\n";
$code .= "query:'" . $get . "',\n";
$code .= "suggestions:[" . $resString . "],\n";
$code .= "data:[" . $idString . "]\n";
$code .= "}";
echo $code;
This outputs a basic JSON dataset that is read and shot back as a result. The problem is that it has been REALLY slow. the "LIMIT 5" helped a ton to speed it up some. However, still running slow after the initial result set shows. I may type in "hum" and it will come back with a quick result set, but any letters/words after that take 4-6 seconds to show. The database is 300K records roughly. Attached is a picture of my database structure page:
database http://semesterkey.com/images/data.jpg
Im just trying to figure out how to speed things up. I think it might be my database structure, it could be how Im querying? I just have no idea. Thanks in advance for your help!
There is only solution is that use Indexing on column which you want to show in autocomplete in your Database, hope this will help you
Yes, the answer to your problem is an index on the (author, title) group of columns. It will grow the amount of space used by the DB and will decrease the performance on INSERT, UPDATE, DELETE in that table, but it will increase the interrogation performance.
make index on author and title may help you to improve the performance.
Syntax help for those who don't grasp which above answer
ALTER TABLEYour_table_nameADD INDEX (column_title) ;