Hi,

I think by now I have the right syntax for joining 3 tables, but please correct me if it is wrong.

The tables I am joining a as follows:
PRODUKTER (GENERAL PRODUCT INFORMATION)
VARE_TYPE (SHIRTS, JEANS ETC)
SIZES (PRODUCT SIZES)

What I want to do is to let the admin update stock for his shop in one query where I am joining these 3 tables.
I am not sure what result-set i get in return or how to get it actually.

This is what I have: (which gives me an empty result set?)

function lager_styring($connection)
{
if (isset($_GET['produkt_id']))
{
$P_ID = mysqli_real_escape_string($connection, $_GET['produkt_id']);	
}

$get_produkt_info = mysqli_query($connection, "
SELECT produkter.id, produkter.brand_id, produkter.varetype_id, produkter.stock 
INNER JOIN vare_type ON (produkter.varetype_id = vare_type.id)
INNER JOIN sizes on (produkter.id = sizes.produkt_id) 
WHERE produkter.id = '".$P_ID."'");

while ($produkt_row = mysqli_fetch_array($get_produkt_info))
{
echo 'Produkt Id = '.$produkt_row['id'].'<br />'; // From produkter
echo 'Produkt Brand Id = '.$produkt_row['brand_id'].'<br />'; // From produkter
echo 'Produkt Vare type Id = '.$produkt_row['varetype_id'].'<br />'; // From produkter
echo 'Produkt On Stock = '.$produkt_row['stock'].'<br />'; // From produkter
echo 'Produkt name = '.$produkt_row['navn'].'<br />'; // From vare_type (Jeans, shirts)
echo 'Produkt Sizes = '.$produkt_row['size'].'<br />'; // All sizes from the sizes table
}

I get this warning, but need help to get the resutlset - Am I way off?

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\newshop\backend\functions\functions.php on line 317
}

Dani AI

Generated

The immediate problem was a SQL syntax error: the query lacked a FROM clause, so mysqli_query() returned false and mysqli_fetch_array() raised the warning. spotted the missing FROM, recommended checking the MySQL error, and confirmed adding FROM fixed the immediate failure. Below are reliable practices and a short example to avoid the same traps.

Always validate the input and check the query result before fetching. Prefer prepared statements (or at minimum cast numeric IDs) to avoid SQL injection and accidental empty/undefined variables. Also check for errors during development (use mysqli_error() or enable mysqli_report()), but avoid die() in production.

A compact procedural prepared-statement pattern (example):

$stmt = mysqli_prepare($connection,
  "SELECT p.id AS produkt_id, p.brand_id, p.varetype_id, p.stock,
          vt.navn AS varetype_navn, s.size
   FROM produkter p
   LEFT JOIN vare_type vt ON p.varetype_id = vt.id
   LEFT JOIN sizes s ON s.produkt_id = p.id
   WHERE p.id = ?"
);
$pid = (int) ($_GET['produkt_id'] ?? 0);
mysqli_stmt_bind_param($stmt, 'i', $pid);
mysqli_stmt_execute($stmt);
$res = mysqli_stmt_get_result($stmt); // requires mysqlnd; otherwise bind_result()
while ($row = mysqli_fetch_assoc($res)) {
  // handle $row['produkt_id'], $row['varetype_navn'], $row['size'], etc.
}

Notes and quick checklist:

  • Joins that include sizes will produce one row per size. If one aggregated row is needed, use GROUP_CONCAT(s.size) with GROUP BY p.id, or aggregate in PHP.
  • Alias columns (e.g., p.id AS produkt_id) to avoid associative-key collisions.
  • Validate/define $_GET['produkt_id'] before use; casting to (int) is simple and safe for numeric IDs.
  • For debugging enable mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT) during development, then handle exceptions or errors gracefully in production.

This complements the thread: the syntax fix is simple, but adopting prepared statements and careful error checks makes the result-set handling robust for admin stock updates.

Recommended Answers

All 4 Replies

Most likely there was an error, see the manual on how to check for them.

where is the `from` clause in your select query?

commented: Totally missed it... +14
commented: doh, he got it +3

Update line 8 to this and it will tell you if theres an error

$get_produkt_info = mysqli_query($connection, "
SELECT produkter.id, produkter.brand_id, produkter.varetype_id, produkter.stock 
INNER JOIN vare_type ON (produkter.varetype_id = vare_type.id)
INNER JOIN sizes on (produkter.id = sizes.produkt_id) 
WHERE produkter.id = '".$P_ID."'") or die(mysqli_error($connection));

Thanks karthik Pranas!

Im only at the stage of trying to learn how to use joins - putting in FROM sorted the problem out - Thank you for helping out :-)

I thought that when writing:

SELECT produkter.id etc etc

SQL would automatically take the table called produkter since I wrote produkter.column_name

But thank you for pointing that out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.