1

I am trying to fetch the column of a table "category" which is linked with table "subcategory" to the table "item".

I am stuck somewhere coz of silly mistake as I'm not sure where i am going wrong.

I am not able to fetch the value of the last "while". I am using category id in subcategory to get a link to category name in category master.

This is the warning i'm getting :

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs**\itemtable.php on line 43

This is line 43 : while($cat_name=mysqli_fetch_array($rowvalcatname));(The second last line)

Here is my code:

    <form name='update' action='updateitemtable.php'>
<input type='hidden' name='itemid' value='$item_id'>
    <tr><td>$item_id</td>
    <td><input type='text' name='itemtablename' value='$row[1]'></td>");
    $rowval=mysqli_query($conn, "SELECT subcatname from subcategorymaster WHERE subcatid = $row[2]");
        while($row2=mysqli_fetch_array($rowval))
        {
            echo"<td>$row2[0]</td>";
        }

    $rowvalcatid=mysqli_query($conn,"SELECT categoryid from subcategorymaster WHERE subcatid=$row[2]");
        while($row3=mysqli_fetch_array($rowvalcatid))
        {

            $rowvalcatname=mysqli_query($conn,"SELECT categoryname from categorymaster WHERE catid=$row3[0]");
            while($cat_name=mysqli_fetch_array($rowvalcatname));
            echo"<td>$cat_name[0]</td>";
        }
Nehal
  • 1,542
  • 4
  • 17
  • 30
  • qry="SELECT categoryname from categorymaster WHERE catid=$row3[0]"; echo $qry;die(); i think $row3[0] doesn't have a value – Sugumar Venkatesan Jun 07 '16 at 09:26
  • Echo `$row3[0]`, check its value. – Blank Jun 07 '16 at 09:32
  • @Abhinav Malhotra, what is value of $orw[2], from where it value is appearing. Also try to add mysqli_num_rows to check whether it has results or not – Nehal Jun 07 '16 at 09:49
  • Your major issue is that you read through all the results of a query, after which it has returned false and there is no details held of the last row. Then you try and use a value from that last row. – Kickstart Jun 07 '16 at 10:08

4 Answers4

0

This error is for When No Data found after query.

You can change the Query Line like as follow

$rowval=mysqli_query($conn, "SELECT subcatname from subcategorymaster 
  WHERE 
subcatid = ".$row[2]."");
Jm Redwan
  • 1
  • 3
0

This error comes when your query returns no result. Check your result before while loop.

if($rowvalcatname){
 while($cat_name=mysqli_fetch_array($rowvalcatname));
            echo"<td>$cat_name[0]</td>";
}
vartika
  • 494
  • 2
  • 12
0

Slightly different answer here.

You have a couple of issues.

The major issue is that in each case you loop around the results of a query until the end of the result set. At that stage the value you are assigning the rows into is set to FALSE. However you then try and use a value from that last row after the loop (at which stage it isn't even an array to get a value from).

I am not sure why you are doing a query within the loop for a queries results rather than doing a JOIN.

Very basically something like this:-

$rowvalcatid=mysqli_query($conn,"SELECT subcategorymaster.categoryid ,
                                                categorymaster.categoryname
                                        FROM subcategorymaster 
                                        LEFT OUTER JOIN categorymaster
                                        ON subcategorymaster.categoryid = categorymaster.catid
                                        WHERE subcatid=$row[2]");

If you specify the format you want output I will modify this with a script to loop around the results fully.

Assuming you want just the category id and category name (and there is a 1 to 1 relationship) then something like this:-

<form name='update' action='updateitemtable.php'>
<input type='hidden' name='itemid' value='$item_id'>
    <tr><td>$item_id</td>
    <td><input type='text' name='itemtablename' value='$row[1]'></td>");
    $rowvalcatid=mysqli_query($conn,"SELECT subcategorymaster.categoryid,
                                            subcategorymaster.subcatname,
                                            categorymaster.categoryname
                                    FROM subcategorymaster 
                                    LEFT OUTER JOIN categorymaster
                                    ON subcategorymaster.categoryid = categorymaster.catid
                                    WHERE subcategorymaster.subcatid=".$row[2]);
    while($row3=mysqli_fetch_assoc($rowvalcatid))
    {
        echo"<td>".$row3['categoryid']."</td>";

        echo"<td>".$row3['categoryname']."</td>";
    }
Kickstart
  • 21,403
  • 2
  • 21
  • 33
0

Check whether your query returns the values or not. Change your code to this :

<form name='update' action='updateitemtable.php'>
<input type='hidden' name='itemid' value='$item_id'>
<tr><td>$item_id</td>
<td><input type='text' name='itemtablename' value='$row[1]'></td>");
$rowval=mysqli_query($conn, "SELECT subcatname from subcategorymaster WHERE subcatid = $row[2]");

$count1= mysqli_num_rows($rowval);
if($count1 > 0){ // check if value exist
    while($row2=mysqli_fetch_array($rowval))
    {
        echo"<td>$row2[0]</td>";
    }
} 

$rowvalcatid=mysqli_query($conn,"SELECT categoryid from subcategorymaster WHERE subcatid=$row[2]");
$count2= mysqli_num_rows($rowvalcatid);
if($count2 > 0) { // check if value exist
        while($row3=mysqli_fetch_array($rowvalcatid))
        {

            $rowvalcatname=mysqli_query($conn,"SELECT categoryname from categorymaster WHERE catid=$row3[0]");
            while($cat_name=mysqli_fetch_array($rowvalcatname));
            echo"<td>$cat_name[0]</td>";
        }
}
Nehal
  • 1,542
  • 4
  • 17
  • 30