SQL Join doesn't show up correctly - php

i'm trying to find a solution, i'm trying to get a data from different tables using same id. Here is my code
"SELECT * FROM menucat LEFT JOIN vmenutab ON menucat.cat_id = vmenutab.menu_id";
I need to create sections, and fill them later with some content, table menucat is parent table with sections, vmenutab is child table with content.
But i have a problem, it doesn't show up correctly. It should be like this:
Section1
Link1
Link2
But it shows up like this:
Section1
Link1
Section1
Link2
I've been using search, GROUP BY and DISTINCT didn't worked.
tables:
menucat:
cat_id menu_cat_est menu_cat_ru menu_cat_en
vmenutab:
id (unique link id) menu_id (id to related section) menu_name_est menu_name_ru menu_name_en
menu_cat_xxx and menu_name_xxx are different languages data
PHP Code
<?php
$target = "SELECT * FROM menucat LEFT JOIN vmenutab ON menucat.cat_id = vmenutab.menu_id";
$mq = mysql_query($target);
while ($row = mysql_fetch_array($mq)) {
$menu_cat_est = $row['menu_cat_est'];
$menu_cat_ru = $row['menu_cat_ru'];
$menu_cat_en = $row['menu_cat_en'];
$menu_name_est = $row['menu_name_est'];
$menu_name_ru = $row['menu_name_ru'];
$menu_name_en = $row['menu_name_en'];
$cat_id = $row['cat_id'];
$id = $row['id'];
echo '<tr>
<td>' . $menu_cat_est . ''.$menu_name_est.'</td>
<td>' . $menu_cat_ru . ''.$menu_name_ru.'</td>
<td>' . $menu_cat_en . ''.$menu_name_en.'</td>
<td scope="col"><center><img src="style/stylesheet/images/edit.png"></center></td>
<td scope="col"><center><img src="style/stylesheet/images/delete.png"></center></td>
<td scope="col"><center>Добавить</center></td>
</tr>';
}
?>
Sorry for my english. Best Regards.
EDIT: Added entire PHP Code.
EDIT #2: Added table rows.

an example as i don't know your db data. also sort db by cat id
' . $menu_cat_xxx . ''.$menu_name_xxx.' its a bit confusing
$target = "SELECT * FROM menucat LEFT JOIN vmenutab ON menucat.cat_id = vmenutab.menu_id" order by menucat.cat_id;
$mq = mysql_query($target);
while ($row = mysql_fetch_array($mq)) {
$menu_cat_est = $row['menu_cat_est'];
$menu_cat_ru = $row['menu_cat_ru'];
$menu_cat_en = $row['menu_cat_en'];
$menu_name_est = $row['menu_name_est'];
$menu_name_ru = $row['menu_name_ru'];
$menu_name_en = $row['menu_name_en'];
$cat_id = $row['cat_id'];
$id = $row['id'];
if(!$nocat[$cat_id]){ $nocat[$cat_id] = $cat_id; $section='<tr><td colspan="6"> section' . $cat_id . '</td></tr>
';}else{$section='';} // customise as i need sleep
echo $section.
'<tr>
<td>' . $menu_cat_est . ''.$menu_name_est.'</td>
<td>' . $menu_cat_ru . ''.$menu_name_ru.'</td>
<td>' . $menu_cat_en . ''.$menu_name_en.'</td>
<td scope="col"><center><img src="style/stylesheet/images/edit.png"></center></td>
<td scope="col"><center><img src="style/stylesheet/images/delete.png"></center></td>
<td scope="col"><center>Добавить</center></td>
';
}

Related

What can I do with this problem with mysql php and table result?

I have this query to list name categories and avg of each one, but the result is not what I want, how to sove this? if you look to image you see that the table resulta don't show correct
$query = $connMysqli->query("SELECT qc.name as cat_name
FROM fhrw_question_categories qc
...
group by Quiz, cat
ORDER BY u.id");
?>
<table>
<tr>
<?php
while($row = $query->fetch_assoc()){
echo '<th>' . $row['cat_name'] . '</th>';
}?>
</tr>
<!-- table continiuous avg-->
<?
$query = $connMysqli->query("SELECT avg(IFNULL(fraction, 0)) * 10 as Media, u.ud as student, qc.name as cat_name
FROM fhrw_question_attempt_steps qc
...
group by Quiz, u.id, cat
ORDER BY u.id ASC");
?>
<?php
while($row = $query->fetch_assoc()){
echo '<tr><td>' . $row['cat_name'] . ' - ' . round($row['Media'],2) . '</td></tr>';
}?>

PHP output from SQL into a table

I have a query that works (see query results) temporarily taken out I don't have enough points for more than two links
However, when I try to output in PHP the category name is the same for both the offeredcategory.categoryName and wantedcategory.categoryName for categoryName on output to a table (see screenshot):
I'm trying to use the alias in the query to output categoryName differently for offered and wanted.
I've also tried using $row["offeredcategory.categoryName"] and $row["wantedcategory.categoryName"] which yields an error:
Notice: Undefined index: offeredcategory.categoryName in C:\Program Files (x86)
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
$sql = "SELECT customers.*, ads.*, categoriesselected.categoryselectedID, categoriesselected.offeredcategoryID, offeredcategory.categoryID, offeredcategory.categoryName, categoriesselected.wantedcategoryID, wantedcategory.categoryID, wantedcategory.categoryName
FROM customers
INNER JOIN ads ON ads.customerId = customers.customerID
INNER JOIN categoriesselected ON categoriesselected.adID = ads.adID
LEFT OUTER JOIN categories AS offeredcategory ON offeredcategory.categoryID = categoriesselected.offeredcategoryID
LEFT OUTER JOIN categories AS wantedcategory ON wantedcategory.categoryID = categoriesselected.wantedcategoryID";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>
<tr><th></th><th colspan=2>OFFERING</th><th colspan=2>WANTING</th><th>Location</th></tr>";
//need to prevent SQL injection using ...
while($row = $result->fetch_assoc())
{
echo
'<tr>
<td><img src="images/'.$row["fileUploadLocation"]. '" width="80" height="80" class="descImage"/></td>
<td>' ."<h6>" . $row["categoryName"]. "</h6>" . "<br>"
. $row["servicesOfferedTitle"]. '</td>
<td>' . $row["servicesOfferedDescription"]. '</td>
<td>' . $row["categoryName"]. "<br>"
. $row["servicesWantedTitle"]. '</td>
<td>' . $row["servicesWantedDescription"]. ' </td>
<td>' . $row["location"]. '</td>
</tr>';
}
echo "</table>";
} else {
echo "0 results";
}
I've now tried as suggested changing alias from joins to Select but now the joins won't work
Haven't to the $row part yet.
I've now tried as suggested changing alias from joins to Select but now the joins won't work (see screenshots):
Haven't to the $row part yet.
from manasschlcatz
Next tried 2nd suggestion but yields error:
Notice: Undefined index: offeredName in C:\Program Files (x86)
$sql = "SELECT customers.*, ads.*, categoriesselected.categoryselectedID, categoriesselected.offeredcategoryID, offeredName.categoryID, offeredName.categoryName, categoriesselected.wantedcategoryID, wantedName.categoryID, wantedName.categoryName
FROM customers
INNER JOIN ads ON ads.customerId = customers.customerID
INNER JOIN categoriesselected ON categoriesselected.adID = ads.adID
LEFT OUTER JOIN categories AS offeredName ON offeredName.categoryID = categoriesselected.offeredcategoryID
LEFT OUTER JOIN categories AS wantedName ON wantedName.categoryID = categoriesselected.wantedcategoryID" ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>
<tr><th></th><th colspan=2>OFFERING</th><th colspan=2>WANTING</th><th>Location</th></tr>";
//need to prevent SQL injection using ...
while($row = $result->fetch_assoc())
{
echo
'<tr>
<td><img src="images/'.$row["fileUploadLocation"]. '" width="80" height="80" class="descImage"/></td>
<td>' . $row["offeredName"]. "<br>"
. $row["servicesOfferedTitle"]. '</td>
<td>' . $row["servicesOfferedDescription"]. '</td>
<td>' . $row["wantedName"]. "<br>"
. $row["servicesWantedTitle"]. '</td>
<td>' . $row["servicesWantedDescription"]. ' </td>
<td>' . $row["location"]. '</td>
</tr>';
}
You have duplicate field names: offeredcategory.categoryNameand wantedcategory.categoryName will both be retrieved $row['categoryName'] so only one will show up - categoryName is ambiguous but not rejected by MySQL because the SQL statement is clear and the problem is processing the results in PHP. Simple solution is:
offeredcategory.categoryName as offeredName
wantedcategory.categoryName as wantedName
and retrieve with $row['offeredName'] or $row['wantedName'] depending on what you actually want to display.
Complete SQL becomes:
SELECT customers.*, ads.*, categoriesselected.categoryselectedID, categoriesselected.offeredcategoryID, offeredcategory.categoryID, offeredcategory.categoryName as offeredName, categoriesselected.wantedcategoryID, wantedcategory.categoryID, wantedcategory.categoryName as wantedName
FROM customers
INNER JOIN ads ON ads.customerId = customers.customerID
INNER JOIN categoriesselected ON categoriesselected.adID = ads.adID
LEFT OUTER JOIN categories AS offeredcategory ON offeredcategory.categoryID = categoriesselected.offeredcategoryID
LEFT OUTER JOIN categories AS wantedcategory ON wantedcategory.categoryID = categoriesselected.wantedcategoryID";

Printing out each row from a MYSQL table individually in PHP

I am trying to individually print out each line from my SQL database in PHP. I am trying to do this so each line that is retrieved, it can act like a link which will direct the user to another page. For example, the current SQL query will output the Category names from the database Category, i would like it to output all the values from that table but have it so each one has a different redirect link to another page which clicked on.
$query = "SELECT CATEGORY_NAME
FROM CATEGORIES ORDER BY CATEGORY_ID ASC";
$results = #mysqli_query ($conn, $query);
$numrows = mysqli_num_rows($results);
if ($results) {
if ($numrows >0) {
echo '
<table>
<tr>
<td><strong>Categories</stong></td>
</tr>';
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td>' . $row['CATEGORY_NAME'] . '</td>
</tr>
';
}
mysqli_free_result ($results);
Such as,
Category
__________
PS4
XBOX
i can click on PS4 and it would take me to another page, i know how to do this with a href and then print out the row in sql however, i'm not sure how do print out each row individually without printing them out using $row['CATEGORY_NAME'].
Thank you for any help
You could just store the link to which it needs to be redirected in a new column CATEGORY_URL along with the CATEGORY_NAME and then print it out like so:
$query = "SELECT CATEGORY_NAME, CATEGORY_URL FROM CATEGORIES ORDER BY CATEGORY_ID ASC";
$results = mysqli_query($conn, $query);
$numrows = mysqli_num_rows($results);
if ($numrows > 0) {
echo '
<table>
<tr>
<td><strong>Categories</stong></td>
</tr>';
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '
<tr>
<td>
'.$row['CATEGORY_NAME'].'
</td>
</tr>';
}
mysqli_free_result ($results);
} else {
// no results found
}
OR if you just want to pass it as a url parameter, you just need to modify if like so:
'.$row['CATEGORY_NAME'].'
OR
'.$row['CATEGORY_NAME'].'
Something like? (I'm not 100% sure if I understand your question)
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td><a href="somelink.php?cat=' . $row['CATEGORY_NAME'] . '">' .
$row['CATEGORY_NAME'] . '</a></td>
</tr> ';
}
UPDATE
If a category is named playstation, playstation.php would be linked to...
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td><a href="' . $row['CATEGORY_NAME'] . '.php">' .
$row['CATEGORY_NAME'] . '</a></td>
</tr> ';
}
Though above would work a wild guess is that you're looking for something completely different. The thing you are looking for is essentially a way to present information for playstation, a way to present information about PS4 etc depending on what user clicks on!? In that case you should do something like this instead:
//Include ID of category in your sql-statement
$query = "SELECT ID, CATEGORY_NAME
FROM CATEGORIES ORDER BY CATEGORY_ID ASC";
....
....
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
echo '<tr>
<td><a href="presentinfo.php?id=' . $row['ID'] . '">' .
$row['CATEGORY_NAME'] . '</a></td>
</tr> ';
}
and create presentinfo.php file where you fetch information about the category, based on the id given in the url. (e.g.select from categories id={id given in url})

Get all products from english version - WooCommerce & WPML

I'm developing a web application that works with WordPress database. In my website,(which works with WooCommerce and WPML) I have products in two languages - english (1st), serbian(2nd). I wrote an SQL code that get the products from database, with their regular and sale prices and etc., but I want to get ONLY products where lang = en (the products from english version). The problem is I don't know how to get them.
Simple SQL:
$sql = 'SELECT * from `wp_posts` WHERE post_type = product';
This is the method that SELECT from database:
// SELECT
public function select (
$table_1 = ' wp_posts ',
$table_2 = ' wp_postmeta ',
$rows = ' t1.id, t1.post_title, guid, post_type ',
$where = ' t1.post_status = "publish" AND t1.post_type = "product" OR t1.post_type = "product_variation" ',
$groupby = ' t1.ID, t1.post_title '
) {
// Connect to database and set charset
$this->connect();
$this->conn->set_charset('utf8');
// Published products
$sql = "SELECT $rows,
max(case when meta_key = '_regular_price' then t2.meta_value end) AS price,
max(case when meta_key = '_sale_price' then t2.meta_value end) AS sale,
max(case when meta_key = 'attribute_colors' then t2.meta_value end) AS colors,
max(case when meta_key = 'attribute_number' then t2.meta_value end)
FROM $table_1 AS t1
INNER JOIN $table_2 AS t2 ON ( t1.ID = t2.post_id )
WHERE $where
GROUP BY $groupby";
$result = mysqli_query($this->conn, $sql);
$published_products_count = mysqli_num_rows($result);
// Trashed products
$trashed_sql = "SELECT post_status FROM `wp_posts`
WHERE post_status = 'trash'";
$trashed_result = mysqli_query($this->conn, $trashed_sql);
$trashed_products_count = mysqli_num_rows($trashed_result);
// If results -> show them
if ($result) {
printf( "\t\t" . "<p>There are <strong>%d published</strong> products and <strong>%d trashed</strong>.</p>", $published_products_count, $trashed_products_count);
$table = '
<table class="pure-table pure-table-bordered">
<thead>
<tr>
<th>Row. №</th>
<th>ID</th>
<th>Product</th>
<th>Regular price</th>
<th>Sale price</th>
<th>Type</th>
<th>Edit</th>
</tr>
</thead>';
$row_number = 1;
while ($row = $result->fetch_assoc()) {
$table .= '
<tr>
<td>' . $row_number++ . '</td>
<td>' . $row["id"] . '</td>
<td>' . $row["post_title"] . '</td>
<td class="price">' . $row["price"] . '</td>
<td class="sale">' . $row["sale"] . '</td>
<td>' . $row["post_type"] . '</td>
<td>Edit</td>
</tr>';
}
$table .= '
</table>';
echo $table;
}
// If no results
else {
echo 'There isn't any products';
}
}
I hope somebody help me!
Thanks in advance! :)
P.S. The application is not based on WordPress!
WPML plugin creates several new tables to manage multilang, not affecting the standard WP posts tables. Specifically, it creates the wp_icl_translations table, where it stores relations between posts and languages.
You need to add two new JOIN statements to your query:
JOIN wp_icl_translations t ON wp_posts.ID = t.element_id AND t.element_type = 'post_product' JOIN wp_icl_languages l ON t.language_code=l.code AND l.active=1
Then, you can add a new condition to your WHERE statement like this:
AND t.language_code='en'
Hope this helps.
Regards.

mysql query 2 tables with count

Hi i have the following query/table from a local bookstore
$queryadmin ="SELECT last_name, first_name, user_id FROM users";
$recordz = #mysqli_query ($dbc, $queryadmin);
while ($row = mysqli_fetch_array($recordz, MYSQLI_ASSOC)) {
echo '<tr>
<td align="left">' . $row['first_name'] . '</td>
<td align="left">' . $row['last_name'] . '</td>
<td align="left">' . $row['user_id'] . '</td>
</tr>'
;}
now i want an additional column from another table where the numbers of books each user has lend out are displayed.
So the query if nested seperately would go something like this
$query2 = mysql_query("SELECT FROM mirror3 WHERE userid='".$row['user_id']."'", $link);
$anzahl = mysql_num_rows($query2);
placing this query nested inside the while query (right after while starting) from above does not work. How to do that?
supplied argument is not a valid MySQL
result resource
thanks
Your query is wrong. Specify a field name:
$query2 = mysql_query("SELECT
SOMETHING FROM mirror3 WHERE userid='".$row['user_id']."'", $link);
You could probably do this in one query:
$query = mysqli_query('SELECT u.last_name, u.first_name, u.user_id, m.PUT_SOMETHING_HERE
FROM users u
LEFT JOIN mirror3 m ON u.user_id = m.userid
WHERE m.PUT_SOMETHING_HERE IS NOT NULL');
But Parkyprg has a point, you need to be selecting something from that second query.

Categories