Row being skipped - php

I have checked all previous versions of this question and in those cases it was evident as to why it wasn't working but when I query my database it is returning no rows. There is only one row right now so I am completely confused as to why the query returns empty. Any advice would be helpful.
$prodSQL = "SELECT * FROM products INNER JOIN shopPage ON products.storePage_id = shopPage.storePage_id
INNER JOIN gallery ON products.gallery_id = gallery.gallery_id
INNER JOIN image ON products.image_id = image.image_id WHERE products.storePage_id = ".$_GET['shop_id'];
$productInfo = $conn->query($prodSQL) or die ("Couldn't execute query: ".mysqli_error($prodSQL));
$row = $productInfo->fetch_assoc();
$gallery_id = $row['gallery_id'];
$mainTitle = $row['storePage_title'];
if($productInfo->num_rows === 0)
{
echo 'No results';
}
$productInfo->close();

Related

Accessing rows from a mysqli join query in php

I have the following code:
// db connection info set up earlier
$sql= "SELECT `TABLE_1.ID`, `TABLE_2.ID`, `POTATO` FROM `TABLE_1.ID` LEFT JOIN `TABLE_2` ON `TABLE_1`.`ID` = `TABLE_2`.`ID_OF_OTHER_TABLE`;";
$rows = mysqli_query($connection, $sql);
foreach ($rows as $row){
$potato = $row["POTATO"];
$id = $row["TABLE_2.ID"];
}
I can't get TABLE_2.ID. I've tried to doing a print_r to get the proper format, but it says it's a mysqli object and I don't get much more info than that. However, I can get potato. So I'm guessing it's a calling syntax issue, but I've searched multiple sites (stack and google included) and not found a clear answer. So, what do I need to do instead of
$id = $row["TABLE_2.ID"];
?
Assign aliases to the columns with the same name.
$sql= "SELECT `TABLE_1`.`ID` AS t1_id, `TABLE_2`.`ID` AS t2_id, `POTATO`
FROM `TABLE_1.ID`
LEFT JOIN `TABLE_2` ON `TABLE_2`.`ID_OF_OTHER_TABLE` = `TABLE_1`.`ID`;";
$rows = mysqli_query($connection, $sql);
foreach ($rows as $row){
$potato = $row["POTATO"];
$id = $row["t2_id"];
}
You can't left join a table with itself, you also cannot have a table that isn't joined = something from another table that cannot be joined to itself.
This will work:
// db connection info set up earlier
$sql= "SELECT TABLE_1.ID, TABLE_2.ID, POTATO
FROM
TABLE_1.ID
LEFT JOIN TABLE_2 ON TABLE_1.ID = TABLE_2.ID";
$rows = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($rows)) {
echo ($row['ID']);
}
mysql_free_result($rows);

Displaying name from an inner joined table?

I've got two tables, one called category, which has the rows id and name, and another called placecategory, which has the tables id, place_id and category_id. I need to inner join these two to echo out the names of the categories where the placecategory.place_id is equal to a $GET[ID].
I've got this so far, but it echo's out nothing.
<?php
include('includes/connectdb.php');
$id = mysqli_real_escape_string($dbc,$_GET['id']);
$qry = 'SELECT id, name FROM category
INNER JOIN placecategory
ON category.id = placecategory.category_id
WHERE placecategory.place_id = '.$id.'';
$result = mysqli_query($dbc,$qry);
while ($row = mysqli_fetch_array($result))
{
echo ''.$row['name'].'';
};
?>
This wont fix your query, but it will display the error generated by the incorrect query. Its a start.
I cannot solve the query issue without a better understanding of your schema.
<?php
include('includes/connectdb.php');
$id = mysqli_real_escape_string($dbc,$_GET['id']);
$qry = 'SELECT id, name
FROM category
INNER JOIN placecategory ON category.id = placecategory.category_id
WHERE placecategory.place_id = '.$id;
$result = mysqli_query($dbc,$qry);
// test the status before continuing
if ( ! $result ) {
echo mysqli_error($dbc);
exit;
}
while ($row = mysqli_fetch_array($result))
{
echo $row['name'];
}
?>

PHP/MYSQL - Data retrieval

Going by the advice that join are better than nested queries, I've converted all my nested queries to join. However, upon converting to join, I'm unable to retrieve data into my array from the SQL result.
Here are my queries :
Without join
$a="SELECT F_DATE, COUNT(F_DATE) as COUNT_F
from FWH
where FI_NAME IN
(
SELECT I_NAME from INS_W WHERE INSTANCE_ID IN
(
SELECT I_MAP_ID FROM T_MAP where T_MAP_ID =
(
SELECT T_ID FROM TWY WHERE T_NAME = 'abc'
)
)
)
AND F_DATE between '$S_D' AND '$E_D'
GROUP BY F_DATE";
With join
$a="SELECT t1.F_DATE AS DATE_F, COUNT(t1.F_DATE) as COUNT_F
from FWH t1
JOIN INS_W t2 ON(t1.FI_NAME = t2.I_NAME)
JOIN T_MAP t3 ON(t2.INSTANCE_ID = t3.I_MAP_ID)
JOIN TWY t4 ON(t3.T_MAP_ID = t4.T_ID)
WHERE t4.T_NAME = 'abc' AND
t1.F_DATE BETWEEN '$S_D' AND 'E_D'GROUP BY t1.F_DATE";
Here's the PHP code to retrieve data
$link = mysql_connect("ip", "user", "passs");
$dbcheck = mysql_select_db("db");
if ($dbcheck) {
$chart_array_1[] = "['F DATE','F COUNT']";
$result = mysql_query($a);
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$f_date=$row["DATE_F"];
$f_count=$row["COUNT_F"];
$chart_array_1[]="['".$f_date."',".$f_count."]";
}
}
}
mysqli_close($link);
The SQL queries themselves run fine when tested directly on MySQL DB.
For some reason, when I use joins, I'm forced to use row[0], row[1] etc instead of fetching values using the name of column. I do not understand the reason behind this. However, this is the only way out in my case. Code below for those who may get stuck in a similar situation as me.
$link = mysql_connect("ip", "user", "passs");
$dbcheck = mysql_select_db("db");
if ($dbcheck) {
$chart_array_1[] = "['F DATE','F COUNT']";
$result = mysql_query($a);
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$chart_array_1[]="['".$row[0]."',".$row[1]."]";
}
}
}
mysqli_close($link);

Display more than one row from SQL SELECT

I want to display all of the rows shown in the picture where CID = 1.
Here is my PHP code with SQL:
`
$contractCount = 1;
$sql = "SELECT categories.categoryID
FROM categories
LEFT JOIN link
ON categories.categoryID = link.categoryID
WHERE link.CID = '$contractCount'";
$res = $con->query($sql);
if (!$res) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while ($row = mysqli_fetch_array($res)) {
echo $row['categoryID'];
}
Here is an image showing the table in PHPMyAdmin called categories.
So I need output as ITSM, Mar and HrAd but I am only getting ITSM and not the rest.
EDIT 1: The LEFT JOIN makes no difference here, the link table has no bearing on the SELECT statement
EDIT 2: I have solved the problem, my mistake was that I had the table names the wrong way round in the SQL query.
You need to use the function mysql_fetch_row, this will fetch a row and move the pointer to the next one.
while ($row = mysqli_fetch_row($res)) {
echo $row['categoryID'];
}

Running if statement based on the results of multiple selections

this is probably going to require something very basic but I can't find an answer to do this in the way I want it to work. What I have is a mysql table of articles and a table of favourites where if you like an article the information is logged. So I need to select all of the articles that I like from one table and then all of the articles from another and display them all as a feed, which I have done. The part I need help with is saying, if I like one of the posts, do not display the like button (Alternatively if I haven't yet liked it, show the button). Can anyone please point me in the right direction with this? Here's the code I have but it only shows the last one I Liked:
<?php
$check_like_sql = "SELECT * FROM posts WHERE type = 'Like' && poster = '$yourid'";
$check_like_res = mysqli_query($con, $check_like_sql) or die (mysqli_error());
if(mysqli_affected_rows($con)>0){
while($likes = mysqli_fetch_array($check_like_res)){
$yourlike = $likes['media'];
}
}
?>
<?php
$get_posts_sql = "SELECT posts.*, members.username, members.avatar, fans.connection FROM posts LEFT JOIN members ON members.id = posts.poster LEFT JOIN fans ON fans.fan = '$yourid' WHERE posts.poster = fans.connection ORDER BY date DESC";
$get_posts_res = mysqli_query($con, $get_posts_sql) or die (mysqli_error());
if(mysqli_affected_rows($con)>0){
while($posts = mysqli_fetch_assoc($get_posts_res)){
$postid = $posts['id'];
etc
etc
if($yourlike == $postid){
$likethis = "Unlike . ";
}
else if($posttype == "Like"){
$likethis = "";
}
else{
$likethis = "Like . ";
}
$post .= "";
}
}
?>
You can left outer join from your articles table to your liked_articles table.
select a.*,
case when la.liked_article_id is null then 'Not Liked Yet' else 'Already Liked' as LikedStatus
from articles a
left outer join liked_articles la
on a.id = la.article_id
and #userId = la.user_id;
This may not be syntactically correct for MYSQL but you get the gist.

Categories