Not displaying data from mysql - php

I have this table,
--------------------------------------------
| products_id | related_products_ids |
| -----------------------------------------
| 1 | 1,2,3,4,6, |
| -----------------------------------------
| 2 | 1,2,3, |
| -----------------------------------------
| 3 | 1,2, |
-------------------------------------------
I want to display those related products in the product page. How can I do that?
Example, I'm in the product page where products_id is 1, i want to display the products from a table like table_name1 by related_products_ids.
I have used this code for displaying data,
$sql = "SELECT related_products_ids FROM ".TABLE_RELATED_PRODUCTS." where products_id = '" . (int)$_GET["products_id"]."'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$lst_rp = explode(',', $row['related_products_ids']);
echo '<ul>';
foreach($lst_rp as $rp_id) {
$res = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id='" . $rp_id . "'";
$result1 = mysql_query($res);
$row1 = mysql_fetch_array($result1);
echo '<li>'.$row1['products_name'].'</li>';
}
echo '</ul>';
However, it displays nothing.. Is there something wrong with my code? Do you have any solution for this?
Thank you.

You have placed the actual database query section outside of the loop.
You should include this within the foreach loop, so that every time, a new query runs and you will receive the results as you anticipated.
foreach($lst_rp as $rp_id) {
$res = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id='" . $rp_id . "'";
$result1 = mysql_query($res);
while($row1 = mysql_fetch_array($result1)) {
echo $row1['products_name'];
}
}
And one more thing, since you are using double quotes to wrap your query, you can shorten it two
$res = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id='$rp_id'";
Update
Use the following code to ensure the <li> is not empty
foreach($lst_rp as $rp_id) {
$res = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id='" . $rp_id . "'";
$result1 = mysql_query($res);
$row1 = mysql_fetch_array($result1);
echo !empty($row1['products_name']) ? '<li>'.$row1['products_name'].'</li>' : '';
}

Please try with this
$sql = "SELECT related_products_ids FROM ".TABLE_RELATED_PRODUCTS." where products_id = '" . (int)$_GET["products_id"]."'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$lst_rp = explode(',', $row['related_products_ids']);
foreach($lst_rp as $rp_id) {
$res = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id='" . $rp_id . "'";
$result1 = mysql_query($res);
$row1 = mysql_fetch_array($result1);
echo $row1['products_name'];
}

In your code
$lst_rp = explode(',', $row['related_products_ids']);
gives you an array of related product ids which is fine. But then in your loop:
foreach($lst_rp as $rp_id) {
$res = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id='" . $rp_id . "'";
}
you are overwriting the value of $res in each loop iteration so when you do the next query
$result1 = mysql_query($res);
it will only fetch the last related product id from the database.

please change only
$row1=mysql_fetch_assoc($result1);
i means
foreach($lst_rp as $rp_id) {
$res = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id='" . $rp_id . "'";
$result1 = mysql_query($res);
$row1 = mysql_fetch_assoc($result1);
echo $row1['products_name'];
}

Related

php apply rules from inside one table to other tables

i don't know how to explain it so i will show you what i am trying to do.
mysql tables
Table Rules:
id| name|maker|model |type|price_rule
1 |client1|bmw |x5 | |24
2 |client2| | |2.0 |13
Table Products_client1:
id|maker|model |type|price
1 |bmw |x5 |2.4 |10
2 |opel |vectra|1.6 |5
3 |ford |mondeo|2.0 |15
now, i have a list of over 500k products in my mysql db, and need to update the price in the products table, with the rules from the rules table :
UPDATE products_client1,client2,... SET price=price+price_rule WHERE maker=maker or model=model or type=type
the thing is that not all clients in the Rule table have maker and model and type, some have only maker or only model or only type or a combination of the 3 and every client has a separate products table.
products_client1, products_client2.....
$sql = "SELECT * FROM Table_rule";
$Res = $conn->query ($sql) or die ("Error : " . $sql . "<br>" . $conn->error);
While($Row = $Res->fetch_assoc ()) { if (isset ($Row ["id"])) { $c_id[] = $Row ["id"]; if (isset ($Row ["name"])) { $c_name [] = $Row ["name"]; if (isset ($Row ["price_rule"])) { $c_price [] = $Row ["price_rule"]; }
$c_id_count = count($c_id);
For ($x=0; $x <> $c_id_count; $x++) { $sql_updt = "UPDATE Products_" . $c_name [$x] . " SET price=price+" . $c_price [$x] . " WHERE ";
$Conn->query ($sql_updt); }
Where what ? If this is = to this do this.....how to i check if every rule has 1...2....3....4...n maches or just 1 and hot do i apply price edit only to that item....there is always a match or more

Why is my 3 table JOIN MySQL query not working?

I have 3 tables:
Table: album
Columns: id, name, description, author, path, image
Table: albumconnect
Columns: id, imageid, albumid
Table: albumimages
Columns: id, path
And I'm trying to replace all those unnecessary queries with a single JOIN query:
<?php
$albumID = $_SERVER['QUERY_STRING'];
$realAlbumID = substr($albumID, 1);
$realestAlbumID = str_replace('%20', ' ', $realAlbumID);
$sql = "SELECT * FROM album WHERE id='$realestAlbumID'";
$result = mysqli_query($conn, $sql);
$getResult = mysqli_fetch_assoc($result);
$albumPath = $getResult['path'];
$sql2 = "SELECT * FROM albumconnect WHERE albumid='$realestAlbumID'";
$result2 = mysqli_query($conn, $sql2);
while ($row = $result2->fetch_assoc()){
$imageId = $row['imageid'];
$sql3 = "SELECT * FROM albumimages WHERE id='$imageId'";
$result3 = mysqli_query($conn, $sql3);
$getResult3 = mysqli_fetch_assoc($result3);
$imagePath = $getResult3['path'];
echo '<div class="imageContainerAlbums"><li class="listAlbums"><img class="specificAlbumThumnails" src="'.$albumPath.$imagePath.'" alt="Random image" /></li></div>';
};
?>
Now the JOIN query I've come up with based on stuff I've read online is this:
$sql5 = "SELECT * FROM album
JOIN albumconnect ON albumconnect.albumid=album.id
JOIN albumimages ON albumimages.id=albumconnect.imageid
WHERE id='$realestAlbumID'";
$result5 = mysqli_query($conn, $sql5);
However, when I try to var_dump the contents, it prints Null so I assume my query is incorrect but I can't figure out the correct way to do it.
Should be something like this. I didn't test it. As #Difster correctly said, SQL engine doesn't know which id should it reference. So, define table aliases and prefix the referenced columns with them. Then define unique aliases for the column names too. Otherwise your sql statement were almost perfect.
<?php
$albumID = $_SERVER['QUERY_STRING'];
$realAlbumID = substr($albumID, 1);
$realestAlbumID = str_replace('%20', ' ', $realAlbumID);
$sql = "SELECT
alb.name AS album_name,
alb.description AS album_description,
alb.author AS album_author,
alb.path AS album_path,
alb.image AS album_image,
ali.path AS image_path
FROM album AS alb
LEFT JOIN albumconnect AS alc ON alc.albumid = alb.id
LEFT JOIN albumimages AS ali ON ali.id = alc.imageid
WHERE alb.id = '$realestAlbumID'";
$result = mysqli_query($conn, $sql);
while ($row = $result->fetch_assoc()) {
$albumPath = $row['album_path'];
$imagePath = $row['image_path'];
echo '<div class="imageContainerAlbums">';
echo '<li class="listAlbums">';
echo '<img class="specificAlbumThumnails" src="' . $albumPath . $imagePath . '" alt="Random image" />';
echo '</li>';
echo '</div>';
}
EDIT 1:
The columns from the sql statement which you don't use later are optional. So, you don't need to select all columns if you don't need them later.
It maybe that you are also becoming rows with NULL values for alc or ali tables. It means that not all albums have images. Then you must give us values you have in the tables, so that we can provide you the proper further WHERE conditions like WHERE ali IS NOT NULL. This answer of me is just the starting point for you.
EDIT 2:
This version is ok too. I just changed the sql statement.
<?php
$albumID = $_SERVER['QUERY_STRING'];
$realAlbumID = substr($albumID, 1);
$realestAlbumID = str_replace('%20', ' ', $realAlbumID);
$sql = "SELECT
alb.name AS album_name,
alb.description AS album_description,
alb.author AS album_author,
alb.path AS album_path,
alb.image AS album_image,
ali.path AS image_path
FROM albumimages AS ali
LEFT JOIN albumconnect AS alc ON alc.imageid = ali.id
LEFT JOIN album AS alb ON alb.id = alc.albumid
WHERE alb.id = '$realestAlbumID'";
$result = mysqli_query($conn, $sql);
while ($row = $result->fetch_assoc()) {
$albumPath = $row['album_path'];
$imagePath = $row['image_path'];
echo '<div class="imageContainerAlbums">';
echo '<li class="listAlbums">';
echo '<img class="specificAlbumThumnails" src="' . $albumPath . $imagePath . '" alt="Random image" />';
echo '</li>';
echo '</div>';
}

How to get tags from the database?

My database structure:
News:
id | title |description | slug
Tags:
id | title | slug
Tags news:
id | id tags | id news
And the code:
$sqltagi = mysql_query("SELECT id_tags FROM tags_news WHERE id_news LIKE '%$id%' ORDER BY id_tagu DESC");
if (!$sqltagi)
{
echo 'Error: ' . mysql_error();
exit;
}
$row = mysql_fetch_array($sqltagi);
$lol=$row['id_tagu'];
$sql = mysql_query("SELECT title,slug FROM tags WHERE id LIKE '%$lol%'");
//$sqlq = mysql_fetch_array($sql);
while ($rowid = mysql_fetch_array($sql))
{
$aa1=$rowid['title'];
$aa2=$rowid['slug'];
$tags=''.$aa1.' '.$aa2.'';
}
how to?
Several things! First of all, please don't use mysql functions! They are not secure, prone to sql injections
Use mysqli or pdo, and you should research joins in sql.
But if you must use mysql functions, then you can do something like this:
<?php
$sql = mysql_query("SELECT title, slug FROM tags JOIN tags_news ON tags.id = tags_news.id_tags");
while ($rowid = mysql_fetch_array($sql)) {
$aa1 = $rowid['title'];
$aa2 = $rowid['slug'];
echo $tags = '' . $aa1 . ' ' . $aa2 . '<br />';
}
?>
Please try this...
$sqltagi = mysql_query("SELECT id_tags FROM tags_news WHERE id_news LIKE '%$id%' ORDER BY id_tagu DESC");
if (!$sqltagi)
{
echo 'Error: ' . mysql_error();
exit;
}
while($row = mysql_fetch_array($sqltagi))
{
$lol=$row['id_tags'];
$sql = mysql_query("SELECT title,slug FROM tags WHERE id LIKE '%$lol%'");
//$sqlq = mysql_fetch_array($sql);
while ($rowid = mysql_fetch_array($sql))
{
$aa1=$rowid['title'];
$aa2=$rowid['slug'];
$tags=''.$aa1.' '.$aa2.'';
}
}

How to pull the last 7 days from mysql database which includes count?

I am querying a table in mysql. At the moment the query works fine and counts the amount of repeated records which are in a table.
$sql = "SELECT COUNT(*) as c FROM toutcome WHERE affID = '" . $_SESSION['affID'] . "' ";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo $row['c'] ;
Now I need to display that same count, but it needs to display only for the last 7 days. I have tried following and it does not works:
$sql = "SELECT COUNT(*) as c FROM toutcome WHERE affID = '" . $_SESSION['affID'] . "' and CompletedDate > DATE_SUB(CURDATE(), INTERVAL 7 DAY) ";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo $row['c'] ;
Any advice would be great.

MySQL - Select differently

I have this query, which selects a distinct value for a column, but I need something else in that query too. I need it to fetch a different row associated with the main select.
Let me illustrate...
This is my query:
$sql = 'SELECT DISTINCT user_id FROM ' . DONATION_SECURITY_TABLE;
$result = mysql_query($sql);
$rows = mysql_fetch_assoc($result);
mysql_free_result($result);
return $rows;
As you see it returns this query returns the DISTINCT of user_id.
If I use a function like this in a double foreach loop created using the return of the query above:
public function get_donor_status($user_id)
{
global $db;
$sql = 'SELECT payment_status FROM ' .DONATION_SECURITY_TABLE .
" WHERE user_id = '" . (int) $user_id . "'";
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$payment_status = $row['payment_status'];
$db->sql_freeresult($result);
return $payment_status;
}
This function would return Completed for user_id 2, but I want it to say Pending instead. How would I change my query so it returns the last value for the corresponding user_id?
If I'm not clear enough, please let me know so I can reexplain.
Just select the last row for the user:
"SELECT payment_status FROM " . DONATION_SECURITY_TABLE . " WHERE user_id = '" . (int) $user_id . "' ORDER BY donation_id DESC LIMIT 1"
How about this?
$sql = 'SELECT payment_status FROM ' .DONATION_SECURITY_TABLE .
" WHERE user_id = '" . (int) $user_id . "' ORDER BY donation_id DESC LIMIT 1";
You can actually get everything in one SQL statement, no need for a double loop:
SELECT
user_id, payment_status
FROM
DONATION_SECURITY_TABLE t1
WHERE
donation_id = (select max(donation_id) from DONATION_SECURITY_TABLE t2 WHERE t2.user_id = t1.user_id)
ORDER BY
user_id

Categories