$user_res_get = do_mysql_query("SELECT /* forums.php */ userid, subject, " .
" forumid FROM topics WHERE id=$topicid") or sqlerr(__FILE__, __LINE__);
and:
$user_res_get = do_mysql_query("SELECT /* forums.php */ id, name, FROM " .
"forums WHERE id=$forumid") or sqlerr(__FILE__, __LINE__);
forumd and id are identical
How to "JOIN" them ?
I tryed create something like this:
$user_res_get = do_mysql_query("SELECT u.userid, u.subject, u.forumid, a.id, " .
"a.name FROM topics AS u JOIN forums as a ON u.forumid = a.id WHERE " .
"u.forumid=$topicid") or sqlerr(__FILE__, __LINE__);
But no luck :/
Any suggestions ?
EDIT:
Ant tryed to print with:
$user_row_get = mysql_fetch_assoc($user_res_get);
$user_row_get['subject']
$user_row_get['name']
and others...
Instead of:
WHERE u.forumid=$topicid
try:
WHERE u.id=$topicid
Related
I'm trying to show result from table that related to many tables but my problem is that the query is return only one product that is on special what I want to return are both products products on special and also products that are not.
My Php Query is :
$query = "SELECT DISTINCT p.product_id, p.price ,sp.date_end, f.percentage AS special_percentage , p.model, pd.name AS title, pd.description AS text, cd.name AS section, p.image, pd.tag, p.date_added AS created "
."FROM #__mijoshop_product AS p "
."JOIN #__mijoshop_product_special AS sp ON p.product_id = sp.product_id "
."JOIN #__mijoshop_flordeco_product_special_percentage AS f ON sp.product_id = f.product_id "
."INNER JOIN #__mijoshop_product_description AS pd ON p.product_id = pd.product_id "
."LEFT JOIN #__mijoshop_product_to_store AS ps ON p.product_id = ps.product_id "
."LEFT JOIN #__mijoshop_product_to_category AS pc ON p.product_id = pc.product_id "
."LEFT JOIN #__mijoshop_category_description AS cd ON (pc.category_id = cd.category_id AND cd.language_id = {$language_id}) "
."LEFT JOIN #__mijoshop_category_to_store AS cs ON (pc.category_id = cs.category_id AND cs.store_id = {$store_id}) "
."WHERE (LOWER(pd.name) LIKE '%" . $search_text . "%' OR
LOWER(pd.description) LIKE '%" . $search_text . "%' OR
LOWER(p.sku) LIKE '%" . $search_text . "%' OR ";
if( $model ) {
$query .= "LOWER(p.model) LIKE '%" . $search_text . "%' OR ";
}
$query .= "LOWER(pd.tag) LIKE '%" . $search_text . "%') "
."AND p.status = '1' "
."AND date(sp.date_end) >= date(NOW()) "
."AND p.date_available <= NOW() "
."AND ps.store_id = {$store_id} "
."AND pd.language_id = '" . $language_id . "' "
."GROUP BY p.product_id "
."ORDER BY {$order_by} "
."LIMIT ".$limit;
$db->setQuery($query);
$results = $db->loadObjectList();
Change the joins with #__mijoshop_product_special and #__mijoshop_flordeco_product_special_percentage to LEFT JOIN so that it won't restrict the results only to products that have matches in this table.
Also, you don't need to use SELECT DISTINCT when you use GROUP BY p.product_id; since there's only 1 row for each product ID, you can't get any duplicates. However, it also doesn't make sense to use GROUP BY when you're not using any aggregation functions, like SUM() or COUNT(). If all these tables are 1-to-1 correspondences, you shouldn't get any duplicates that need to be removed with either option.
I'm trying to execute this query:
Query 1:
$query = "SELECT a.*, b.title_wo
FROM `worksheet_master` AS a
INNER JOIN `work_order` AS b ON a.wo_number = b.wo_number
WHERE CONCAT (a.`wo_number` like '" . $_POST["keyword"] . "%',
`title_wo` like '" . $_POST["keyword"] . "%')
ORDER BY a.`wo_number` DESC LIMIT 0,50";
Query 2:
$query = "SELECT a.*, b.title_wo
FROM `worksheet_master` AS a
INNER JOIN `work_order` AS b ON a.wo_number = b.wo_number
WHERE CONCAT (a.`wo_number` like '" . $_POST["keyword"] . "%',
`title_wo` like '" . $_POST["keyword"] . "%')
AND a.`status` = 'NULL'
ORDER BY a.`wo_number` DESC
LIMIT 0,50";
The Query 2 didn't gave me any result with AND clause while the Query 1 gave me the result.
Can anyone help me with this? I need to sort out the result which has the empty status in my table, that's why I added AND clause in Query 2 hoping the result will be as expected, but it's not.
Thank You.
Unless NULL is an actual string, you need to use IS NULL instead.
$query = "SELECT a.*, b.title_wo
FROM `worksheet_master` AS a
INNER JOIN `work_order` AS b ON a.wo_number = b.wo_number
WHERE CONCAT (a.`wo_number` like '" . $_POST["keyword"] . "%',
`title_wo` like '" . $_POST["keyword"] . "%')
AND a.`status` IS NULL
ORDER BY a.`wo_number` DESC
LIMIT 0,50";
I have this vode for show result using PHP join method:
$DB_QUERY = mySqli::f("SELECT name,id, " . AUTHOR . ".id,author_id,book_id FROM " . AUTHOR . " JOIN " . AUTHORS . " ON
" . AUTHOR . ".id = " . AUTHORS . ".author_id WHERE " . AUTHORS . ".book_id = ? ORDER BY name ASC LIMIT 8 ", $id);
foreach($DB_QUERY as $row){
echo $row['id'];
}
Now I see this :
Column 'id' in field list is ambiguous
I now this error when I have two column with name id. how do fix this error without change id name in one column?!
EDIT:
author table:
id|name|
authors table:
id|author_id|book_id
$DB_QUERY = mySqli::f("SELECT author.name, author.id as a_id,
authors.id as as_id, authors.author_id, authors.book_id
FROM author INNER JOIN authors ON author.id = authors.author_id
WHERE authors.book_id = ? ORDER BY name ASC LIMIT 8 ", $id);
For instance.
Then your field real names haven't changed but in your php you can use their temporary "nicknames" a_id and as_id.
Though I am not sure you need authors.id for anything... if it is only your PK on the table maybe you should drop it and use authors.author_id and authors.book_id as your PK... or.. if you are not bringing content from other tables with it... just don't mention it on your select.
You have selected id in your query without any alias & you are applying join on Author & Authors table. I think as both tables contains id column you are getting the error.
Try this
$DB_QUERY = mySqli::f("SELECT name, " . AUTHORS . ".id, " . AUTHOR . ".id,author_id,book_id FROM " . AUTHOR . " JOIN " . AUTHORS . " ON
" . AUTHOR . ".id = " . AUTHORS . ".author_id WHERE " . AUTHORS . ".book_id = ? ORDER BY name ASC LIMIT 8 ", $id);
I am working on a php script that will get the required information and display it in an xml. For some reason my brin isn't work and I don't know how to do the query. The database has two tables that I want to pull information from. Both tables have one thing in common which is 'id_member'. Themes table has a bunch of junk in it - so it has 4 coulms - 'id_member', 'id_theme', 'varible', and 'value'. There are two items in 'varible' that I want to filter (show them and not the rest) "cust_armaus" and "cust_armamo" then value will show the value of course. So in the table Themes it will have id_member listed more than once to show the different varibles. This is what I got that isn't working for me:
<?php
header('Content-Type: text/xml');
$username="database_username";
$password="database_password";
$database="database_name";
mysql_connect('localhost',$username,$password);
#mysql_select_db($database) or die( "Unable to select database");
echo "<?xml version=\"1.0\"?>\n".
"<!DOCTYPE squad SYSTEM \"squad.dtd\">\n".
"<?xml-stylesheet href=\"squad.xsl?\" type=\"text/xsl\"?>\n";
?>
<squad nick="Team Tag">
<name>Team Name</name>
<email>contact#team.com</email>
<web>http://www.team.com</web>
<picture>teamlogo.paa</picture>
<title>This is the Team motto</title>
<?php
// smf_themes -> id_member =
// smf_themes -> variable for cust_armaus & cust_usermo
// smf_themes -> value <- get for cust_armaus & cust_usermo
// smf_members -> real_name = profile_name & name
// smf_members -> email_address = email - NO
$memberSQL = mysql_query("SELECT id_member AS id, real_name FROM smf_members");
$member = mysql_fetch_array($memberSQL);
$armausSQL = mysql_query("SELECT id_member, variable, value AS arma_id FROM smf_themes WHERE id_member='$member[id]' AND variable='cust_armaus'");
$armaid = mysql_fetch_array($armausSQL);
$armamoSQL = mysql_query("SELECT id_member, variable, value AS motto FROM smf_themes WHERE id_member='$member[id]' AND variable='cust_usermo'");
$armamo = mysql_fetch_array($armamoSQL);
$num=mysql_numrows($member, $armaid, $armamo);
mysql_close();
$i=0;
while ($i < $num) {
$profile_id = mysql_result($armaid,$i,"value");
$profile_name = mysql_result($member,$i,"real_name");
$profile_remark = mysql_result($armamo,$i,"value");
$profile_username = mysql_result($member,$i,"real_name");
//$profile_email = mysql_result($result,$i,"email");
//$profile_icq = mysql_result($result,$i,"icq");
echo "<member id=\"$profile_id\" nick=\"$profile_name\">" .
"<name>$profile_name</name>".
"<email>$profile_email</email>".
"<icq>$profile_icq</icq>".
"<remark>$profile_remark</remark>".
"</member>\n";
$i++;
}
?>
</squad>
The mysql interface is deprecated. New development should use either mysqli or PDO.
Assuming you only one one value of the cust_armaus and cust_usermo rows for each smf_members, you could use correlated subqueries in the SELECT list, and return just a single query that returns a single row for each row in smf_members. Something like this:
SELECT m.id_member AS id
, m.real_name
, (SELECT a.value
FROM smf_themes a
WHERE a.id_member = m.id_member
AND a.variable = 'cust_armaus'
ORDER BY a.value LIMIT 1
) AS cust_armaus
, (SELECT u.value
FROM smf_themes u
WHERE u.id_member = m.id_member
AND u.variable = 'cust_usermo'
ORDER BY u.value LIMIT 1
) AS cust_usermo
FROM smf_members m
ORDER BY m.id_member
If the (id_member,variable) tuple is guaranteed to be unique in smf_themes, you could use a simpler outer join:
SELECT m.id_member AS id
, m.real_name
, a.value AS cust_armaus
, u.value AS cust_usermo
FROM smf_members m
LEFT
JOIN smf_themes a
ON a.member_id = m.member_id AND a.variable = 'cust_aramus'
LEFT
JOIN smf_themes u
ON u.member_id = m.member_id AND u.variable = 'cust_usermo'
ORDER BY m.id_member
With either of those queries, you could use code something like this:
$sql = "SELECT ...";
$rs = $mysqli->query($sql)
if (!$rs) {
// handle error
echo 'query failed: (' . $mysqli->errno . ') ' . $mysqli->error;
die;
}
// loop through rows returned
while ( $row = $rs->fetch_assoc() ) {
echo '<member id="' . htmlspecialchars($row['id']) . '"'
. ' nick="' . htmlspecialchars($row['real_name']) . '">'
. '<cust_aramus>' . htmlspecialchars($row['cust_aramus']) . '</cust_aramus>'
. '<cust_usermo>' . htmlspecialchars($row['cust_usermo']) . '</cust_usermo>' ;
}
The select query below returns 1 row when it should be 3. I am pretty sure it is because of the AVG(k.sumtotal) field.
If I rewrite the query and take out that AVG(k.sumtotal) column and take out the FROM inv_ratings AS k, I get my 3 rows.
I can't figure out why this table (inv_ratings) or this AVG(k.sumtotal) column restrict the number of rows to 1. I looked online for hours trying to find information about returning results using the AVG clause and didn't find much. Do I have to use a group by clause, I tried that and only get errors.
$p = "SELECT i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name, AVG(k.sumtotal)"
. " FROM inv_ratings AS k INNER JOIN inventions AS i USING (invention_id)"
. " INNER JOIN categories AS c USING (category_id)"
. " INNER JOIN images AS u USING (invention_id)"
. " WHERE c.category_id = $cat AND i.approved = 'approved'"
. " HAVING u.image_name < 2"
. " ORDER BY date_submitted"
. " DESC LIMIT $start, $display";
$q = mysqli_query($dbc, $p) or trigger_error("Query: $p\n<br />mysqli Error: " . mysqli_error($dbc));
You are running into one of MySQL's gotchas:
http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html
I hate that MySQL ever allows this syntax because it only causes confusion. But what you probably want is (to use MySQL's hackish behavior, please note that if there are multiple values for any of the fields besides invention_id or sumtotal, you get a random value from that column):
$p = "SELECT i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name, AVG(k.sumtotal)"
. " FROM inv_ratings AS k INNER JOIN inventions AS i USING (invention_id)"
. " INNER JOIN categories AS c USING (category_id)"
. " INNER JOIN images AS u USING (invention_id)"
. " WHERE c.category_id = $cat AND i.approved = 'approved'"
. " GROUP BY i.invention_id "
. " HAVING u.image_name < 2"
. " ORDER BY date_submitted"
. " DESC LIMIT $start, $display";
Or, to not use MySQL's hackish behavior:
$p = "SELECT i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name, AVG(k.sumtotal)"
. " FROM inv_ratings AS k INNER JOIN inventions AS i USING (invention_id)"
. " INNER JOIN categories AS c USING (category_id)"
. " INNER JOIN images AS u USING (invention_id)"
. " WHERE c.category_id = $cat AND i.approved = 'approved'"
. " GROUP BY i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name "
. " HAVING u.image_name < 2"
. " ORDER BY date_submitted"
. " DESC LIMIT $start, $display";