0

I am running a PHP script, and keep getting errors like:

*Notice: Undefined variable: result2 in D:\xampp-win32\htdocs\sites\bookstore\result_page.php on line 200 

*warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in D:\xampp-win32\htdocs\sites\bookstore\result_page.php on line 200 

Line 200 looks like this:
<?php while($row = mysqli_fetch_array($result2))

What do they mean?

Edited by happygeek: moved and code formatting added

5
Contributors
7
Replies
42
Views
3 Years
Discussion Span
Last Post by pzuurveen
-1

put '@' before mysqli_fetch_array

after your code look like this

<?php while($row = @mysqli_fetch_array($result2))

0

Just using following code you solve this problem.If you use '@' this is used to hide an error its not a solution of this error.Just using mysl_num_rows () function you solved this problem eg.

<?php

$result2 =mysql_query($qry);
$numrow = mysql_num_rows($result2);
if($numrow >0)
{
    while($row = mysqli_fetch_array($result2))
    {
        //Your code here.......
    }
}
else
{
    echo "Result Not Found !";
}

?>

0

this is my whole code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <?php 

    require_once('auth.php');

    $con=mysqli_connect("localhost","mag_install","mag123","mag_rocket");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    $author = $_POST['Author'];
    $title = $_POST['Title'];
    $category = $_POST['Category'];


    $result = mysqli_query($con,"SELECT * FROM category") or die("Error: ".mysqli_error($con));

    if ($category != 'None') {

        if ($title != NULL) {

    $result2 = mysqli_query($con,"SELECT * FROM issue2 WHERE TITLE LIKE '%$title%' AND CATEGORY = '$category'") or die("Error: ".mysqli_error($con));

        }

        else if ($author != NULL) {

    $result2 = mysqli_query($con,"SELECT * FROM issue2 WHERE NAME LIKE '%$author%' AND CATEGORY = '$category'") or die("Error: ".mysqli_error($con));

        }

    }

    else {

        if ($author != NULL) {

            $result2 = mysqli_query($con,"SELECT * FROM issue2 WHERE NAME LIKE '%$author%'") or die("Error: ".mysqli_error($con));

        }

        else if ($title != NULL) {

            $result2 = mysqli_query($con,"SELECT * FROM issue2 WHERE TITLE LIKE '%$title%'") or die("Error: ".mysqli_error($con));

        }
    }

    $result4 = mysqli_query($con,"SELECT * FROM category");
    ?>
    <html>
    <head>
    <title>Untitled</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    <link href="style.css" rel="stylesheet" type="text/css">
    </head>
    <body>

    <table cellpadding="0" cellspacing="0" border="0" style="width:721px" align="center"> 
        <tr>
            <td>
                <table cellpadding="0" cellspacing="0" border="0" style="height:30px; background :white; class="header">
                </table>
             </td>
        </tr>
        <tr>
            <td>
                <table cellpadding="0" cellspacing="0" border="0" style="height:565px; background:#ffffff ">
                    <tr>
                        <td><img alt="" src="images/spacer.gif" width="9" height="5"></td>
                        <td class="col_left">
                            <table cellpadding="0" cellspacing="0" border="0" class="box_width_left">
                                <tr>
                                    <td>
                                        <table cellpadding="0" cellspacing="0" border="0" class="box_heading_table">
                                            <tr>
                                                <td><img src="images/box_heading_tl.gif" alt="" border="0"></td>
                                                <td style="width:100%" class="box_heading_tall_t"></td>
                                                <td><img src="images/box_heading_tr.gif" alt="" border="0"></td>
                                            </tr>
                                            <tr>
                                                <td class="box_heading_tall_l"><img src="images/box_heading_l.gif" alt="" border="0"></td>
                                                <td style="width:100%" class="box_heading_td">Category</td>
                                                <td class="box_heading_tall_r"><img src="images/box_heading_r.gif" alt="" border="0"></td>
                                            </tr>
                                            <tr>
                                                <td><img src="images/box_heading_bl.gif" alt="" border="0"></td>
                                                <td style="width:100%" class="box_heading_tall_b"></td>
                                                <td><img src="images/box_heading_br.gif" alt="" border="0"></td>
                                            </tr>
                                        </table>
                                        <table cellpadding="0" cellspacing="0" border="0" class="box_body_table">
                                            <tr>
                                                <td style="width:100%" class="box_body">
                                                   <br style="line-height:11px">
                                                   <ul>
                                                        <li class="bg_list"> 

                                                        <?php while($row = mysqli_fetch_array($result))
                                                        { 
                                                            $name = $row["Name"];
                                                            $id2 = $row["Id"];

                                                        ?>

                                                        <a href="/sites/bookstore/features_products.php?category=<?php echo $id2; ?>">
                                                        <?php echo $name;
                                                        echo "<br>"; } ?></a>
                                                        <a href="/sites/bookstore/main_page.php">See all</a>

                                                        </li>  

                                                   </ul>
                                                  <br style="line-height:9px">
                                                 </td>
                                            </tr>
                                        </table> 
                                        <img alt="" src="images/line1.gif"><br><br style="line-height:11px">
                                        <br style="line-height:9px">
                                        <table cellpadding="0" cellspacing="0" border="0" class="box_heading_table_2">
                                            <tr>
                                                <td><img src="images/box_heading_tl_2.gif" alt="" border="0"></td>
                                                <td style="width:100%" class="box_heading_tall_t_2"></td>
                                                <td><img src="images/box_heading_tr_2.gif" alt="" border="0"></td>
                                            </tr>
                                            <tr>
                                                <td class="box_heading_tall_l_2"><img src="images/box_heading_l_2.gif" alt="" border="0"></td>
                                                <td style="width:100%" class="box_heading_td_2">Advance Search</td>
                                                <td class="box_heading_tall_r_2"><img src="images/box_heading_r_2.gif" alt="" border="0"></td>
                                            </tr>
                                            <tr>
                                                <td><img src="images/box_heading_bl_2.gif" alt="" border="0"></td>
                                                <td style="width:100%" class="box_heading_tall_b_2"></td>
                                                <td><img src="images/box_heading_br_2.gif" alt="" border="0"></td>
                                            </tr>
                                        </table>
                                        <table cellpadding="0" cellspacing="15" border="0" class="box_body_table_2">
                                            <tr>
                                                <td>
                                                    <form action="result_page.php" method="POST">
                                                    <b>By Author :</b> <br> <input type="text" name="Author" size="20"> <br><br> 
                                                    <b>By Title :</b> <br> <input type="text" name="Title" size="20"> <br><br>
                                                    <b>Which Category?</b><br><br>

                                                <select name="Category">

                                                <option value="None">None</option>
                                                <?php while($row = mysqli_fetch_array($result4)) {

                                                            $name = $row["Name"];

                                                ?>

                                                        <option value="<?php echo $name ?>"><?php echo $name ?></option>

                                                <?php } ?>

                                                </select>
                                                        <br><br>
                                                        <input type="submit" value="Search">
                                                    </form>
                                                </td>
                                            </tr>
                                        </table>
                                        <img alt="" src="images/line2.gif"><br>

                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td><img alt="" src="images/spacer.gif" width="9" height="1"></td>
                        <td><img alt="" src="images/spacer.gif" width="9" height="1"></td>
                        <td class="col_center">
                            <table cellpadding="0" cellspacing="0" border="0">
                                <tr>
                                    <td>
                                        <table cellpadding="0" cellspacing="0" border="0" class="cont_heading_table">
                                            <tr>
                                                <td><img src="images/cont_heading_tl.gif" alt="" border="0"></td>
                                                <td style="width:100%" class="cont_heading_t"></td>
                                                <td><img src="images/cont_heading_tr.gif" alt="" border="0"></td>
                                            </tr>
                                            <tr>
                                                <td class="cont_heading_l"><img alt="" src="images/cont_heading_l.gif"></td>
                                                <td style="width:100%" class="cont_heading_td cont_heading_td">Featured Products</td>
                                                <td class="cont_heading_r"><img alt="" src="images/cont_heading_r.gif"></td>
                                            </tr>
                                            <tr>
                                                <td><img src="images/cont_heading_bl.gif" alt="" border="0"></td>
                                                <td style="width:100%" class="cont_heading_b"></td>
                                                <td><img src="images/cont_heading_br.gif" alt="" border="0"></td>
                                            </tr>
                                        </table>
                                        <br style="line-height:20px">
                                        <table>

                                            <?php while($row = mysqli_fetch_array($result2))

                                            {   
                                                $id2 = $row["ID"];

                                                ?>

                                                <div id= 'item'>

                                                <P><class="vam"><a href="/sites/bookstore/product_info.php?ID=<?php echo $id2; ?>"><?php echo $row["TITLE"]; ?></a></p>
                                                <p>By <?php echo $row["NAME"] ?></p>
                                                <p><class="vam"><a href="/sites/bookstore/product_info.php?ID=<?php echo $id2; ?>"><img border = "0" alt = "" src="/sites/bookstore/magrocket/backend/source//media/images/<?php echo $row["COVER"]; ?>"></a></P>
                                                <p>RM<?php echo $row["PRICING"] ?></p>
                                                <p>Download File :<br><class="vam"><a href="/sites/bookstore/magrocket/backend/source//media/images<?php echo $row["DOWNLOAD"] ?>">Click Here</a></p>

                                                <?php
                                                $data = range(" "," ");
                                                for($count = 0; $count < count($data);)
                                                {
                                                    echo "<tr>\n";
                                                    for($i = 0; $count < count($data) && $i < 3; $count++, $i++) {
                                                        echo "\t<td>$data[$count]</td>\n";
                                                    }
                                                    for(; $i < 1; $i++) {
                                                        echo "\t<td>-</td>\n";
                                                    }
                                                    echo "</tr>\n";
                                                }
                                                ?>

                                      <?php } ?> 

                                        </table> 
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td><img alt="" src="images/spacer.gif" width="9" height="1"></td>
                    </tr>
                </table>
              </td>
        </tr>
    </table>
    </body>
    </html>
    <?php
    mysqli_close($con);
    ?>

*error at line 200.

Edited by dyea.marcello

0

@dyea.marcello\

Add $con to here:

From this:

<?php while($row = mysqli_fetch_array($result2)){
$id2 = $row["ID"];}
?>

to this:

<?php while($row = mysqli_fetch_array($con,$result2)){
$id2 = $row["ID"];}
?>

What error did you get now?

0

error
*Notice: Undefined variable: result2 in D:\xampp-win32\htdocs\sites\bookstore\result_page.php on line 200

*warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in D:\xampp-win32\htdocs\sites\bookstore\result_page.php on line 200

0

warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in D:\xampp-win32\htdocs\sites\bookstore\result_page.php on line 200

@dyea.marcello

I want to know why you have 4 different $result2 running different query?

Why did you write it like this:

 $result2 = mysqli_query($con,"SELECT * FROM issue2 WHERE TITLE LIKE '%$title%' AND CATEGORY = '$category'") or die("Error: ".mysqli_error($con));
}
else if ($author != NULL) {
$result2 = mysqli_query($con,"SELECT * FROM issue2 WHERE NAME LIKE '%$author%' AND CATEGORY = '$category'") or die("Error: ".mysqli_error($con));
}
}
else {
if ($author != NULL) {
$result2 = mysqli_query($con,"SELECT * FROM issue2 WHERE NAME LIKE '%$author%'") or die("Error: ".mysqli_error($con));
}
else if ($title != NULL) {
$result2 = mysqli_query($con,"SELECT * FROM issue2 WHERE TITLE LIKE '%$title%'") or die("Error: ".mysqli_error($con));
}
}

On your table (issue2) does it have $id2 = $row["ID"]; or $id2 = $row["Id"];?

Also did you write this script?

Edited by LastMitch

1

if both $author and $title are NULL, $result2 will be undefined.
Use isset() before using it.

Edited by pzuurveen

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.