Jump to content

Mysqli_fetch array


Xtremer360

Recommended Posts

The following code is giving me <b>Warning</b>:  mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given in <b>/home/content/y/a/n/yankeefaninkc/html/efedmanager/processes/menuitem.php</b> on line <b>52</b><br />

 

Line 52 is while ($row = mysqli_fetch_array($result)) {

 

if (isset($_POST['submitmenuitem'])) {
    $menuid       = (int) $_POST['menuid'];
    $itemname     = trim($_POST['itemname']);
    $itemnameSQL  = mysqli_real_escape_string($dbc, $itemname);
    $itemurl      = trim($_POST['itemurl']);
    $itemurlSQL   = mysqli_real_escape_string($dbc, $_POST['itemurl']);
    $sortorder    = (int) $_POST['sortorder'];
    $contentpage    = mysqli_real_escape_string($dbc, $_POST['contentpage']);
    $newscategory    = mysqli_real_escape_string($dbc, $_POST['newscategory']);
    $application    = mysqli_real_escape_string($dbc, $_POST['application']);

    $query = 
    'SELECT *' . 
    ' FROM menuitems' . 
    ' WHERE menu_id = ' . $menuid .
' AND (itemname = "' . $itemnameSQL . '"'  . 
    ($itemurlSQL   ? ' OR itemurl         = "' . $itemurlSQL  . '"' : '') . 
    ($contentpage  ? ' OR contentpage_id  = '  . $contentpage  : '') . 
    ($application  ? ' OR application_id  = '  . $application  : '') . 
    ($newscategory ? ' OR newscategory_id = '  . $newscategory : '') . 
    ')';
    $result = mysqli_query ( $dbc, $query ); // Run The Query  
     echo $query;
    if (mysqli_num_rows($result) == 0) {
        $query = "INSERT INTO `menuitems`
                      (menu_id, itemname, itemurl, sortorder, contentpage_id, newscategory_id,
                       application_id, creator_id, datecreated, enabled)
                  VALUES 
                      ('".$menuid."', '".$itemname."', '".$itemurl."', '".$sortorder."', '".$contentpage."',
                       '".$newscategory."', '".$application."', 1, NOW(), 0)";
        mysqli_query($dbc, $query);
        $result = "good";
    
    } else {
    
        $result = '';
        while ($row = mysqli_fetch_array($result)) {
            if ($row['itemname'] == $itemname)                             {$result .= 'bad1';} 
            if ($itemurl      && $row['itemurl']         == $itemurl)      {$result .= 'bad2';} else 
            if ($newscategory && $row['newscategory_id'] == $newscategory) {$result .= 'bad3';} else 
            if ($application  && $row['application_id']  == $application)  {$result .= 'bad4';} else 
            if ($contentpage  && $row['contentpage_id']  == $contentpage)  {$result .= 'bad5';} else
            if ($itemurl  && $row['itemurl']  == $itemurl && $itemname  && $row['itemname']  == $itemname)  {$result .= 'bad6';} else
            if ($contentpage  && $row['contentpage_id']  == $contentpage && $row['itemname']  == $itemname)  {$result .= 'bad7';} else
            if ($application  && $row['application_id']  == $application && $row['itemname']  == $itemname)  {$result .= 'bad8';} else
            if ($newscategory  && $row['newscategory_id']  == $newscategory && $row['itemname']  == $itemname)  {$result .= 'bad9';}
        }
    }
}

Link to comment
Share on other sites

Actually, the problem is here:

        $result = '';
        while ($row = mysqli_fetch_array($result)) {

 

You have assigned an empty string to $result. It is not a query result anymore, so you can't use the mysqli functions against it.

 

It looks like you are trying to use the same variable name for two different purposes. That's a bad idea, and it is impossible when the lifetime of the values overlap.

Link to comment
Share on other sites

What should I do?

 

Use a different name for one of the variables. Since all we can see is this small segment of code, I would suggest changing the name of the query result:

if (isset($_POST['submitmenuitem'])) {
    $menuid       = (int) $_POST['menuid'];
    $itemname     = trim($_POST['itemname']);
    $itemnameSQL  = mysqli_real_escape_string($dbc, $itemname);
    $itemurl      = trim($_POST['itemurl']);
    $itemurlSQL   = mysqli_real_escape_string($dbc, $_POST['itemurl']);
    $sortorder    = (int) $_POST['sortorder'];
    $contentpage    = mysqli_real_escape_string($dbc, $_POST['contentpage']);
    $newscategory    = mysqli_real_escape_string($dbc, $_POST['newscategory']);
    $application    = mysqli_real_escape_string($dbc, $_POST['application']);

    $query = 
    'SELECT *' . 
    ' FROM menuitems' . 
    ' WHERE menu_id = ' . $menuid .
' AND (itemname = "' . $itemnameSQL . '"'  . 
    ($itemurlSQL   ? ' OR itemurl         = "' . $itemurlSQL  . '"' : '') . 
    ($contentpage  ? ' OR contentpage_id  = '  . $contentpage  : '') . 
    ($application  ? ' OR application_id  = '  . $application  : '') . 
    ($newscategory ? ' OR newscategory_id = '  . $newscategory : '') . 
    ')';
# CHANGE THE NAME HERE
    $Qresult = mysqli_query ( $dbc, $query ); // Run The Query  
     echo $query;
# AND CHANGE THE NAME HERE
    if (mysqli_num_rows($Qresult) == 0) {
        $query = "INSERT INTO `menuitems`
                      (menu_id, itemname, itemurl, sortorder, contentpage_id, newscategory_id,
                       application_id, creator_id, datecreated, enabled)
                  VALUES 
                      ('".$menuid."', '".$itemname."', '".$itemurl."', '".$sortorder."', '".$contentpage."',
                       '".$newscategory."', '".$application."', 1, NOW(), 0)";
        mysqli_query($dbc, $query);
        $result = "good";
    
    } else {
    
        $result = '';
# AND CHANGE THE NAME HERE
        while ($row = mysqli_fetch_array($Qresult)) {
            if ($row['itemname'] == $itemname)                             {$result .= 'bad1';} 
            if ($itemurl      && $row['itemurl']         == $itemurl)      {$result .= 'bad2';} else 
            if ($newscategory && $row['newscategory_id'] == $newscategory) {$result .= 'bad3';} else 
            if ($application  && $row['application_id']  == $application)  {$result .= 'bad4';} else 
            if ($contentpage  && $row['contentpage_id']  == $contentpage)  {$result .= 'bad5';} else
            if ($itemurl  && $row['itemurl']  == $itemurl && $itemname  && $row['itemname']  == $itemname)  {$result .= 'bad6';} else
            if ($contentpage  && $row['contentpage_id']  == $contentpage && $row['itemname']  == $itemname)  {$result .= 'bad7';} else
            if ($application  && $row['application_id']  == $application && $row['itemname']  == $itemname)  {$result .= 'bad8';} else
            if ($newscategory  && $row['newscategory_id']  == $newscategory && $row['itemname']  == $itemname)  {$result .= 'bad9';}
        }
    }
}

 

You will have to review the rest of your code to see if anything else needs to be changed

Link to comment
Share on other sites

Thank you all for assistance. I'm getting somewhere and I can tell I almost have this completely corrected. However I know I have a mistake whether I need parenthesis around something or I'm not sure but when the the itemname and itemurl are found together as being already in the database it should be echoing out bad6 via my code. What is preventing it from doing so?

Link to comment
Share on other sites

if (isset($_POST['submitmenuitem'])) {
    $menuid       = (int) $_POST['menuid'];
    $itemname     = trim($_POST['itemname']);
    $itemnameSQL  = mysqli_real_escape_string($dbc, $itemname);
    $itemurl      = trim($_POST['itemurl']);
    $itemurlSQL   = mysqli_real_escape_string($dbc, $_POST['itemurl']);
    $sortorder    = (int) $_POST['sortorder'];
    $contentpage    = mysqli_real_escape_string($dbc, $_POST['contentpage']);
    $newscategory    = mysqli_real_escape_string($dbc, $_POST['newscategory']);
    $application    = mysqli_real_escape_string($dbc, $_POST['application']);

    $query = 
    'SELECT *' . 
    ' FROM menuitems' . 
    ' WHERE menu_id = ' . $menuid .
' AND (itemname = "' . $itemnameSQL . '"'  . 
    ($itemurlSQL   ? ' OR itemurl         = "' . $itemurlSQL  . '"' : '') . 
    ($contentpage  ? ' OR contentpage_id  = '  . $contentpage  : '') . 
    ($application  ? ' OR application_id  = '  . $application  : '') . 
    ($newscategory ? ' OR newscategory_id = '  . $newscategory : '') . 
    ')';
    $Qresult = mysqli_query ( $dbc, $query ); // Run The Query  

    if (mysqli_num_rows($Qresult) == 0) {
        $query = "INSERT INTO `menuitems`
                      (menu_id, itemname, itemurl, sortorder, contentpage_id, newscategory_id,
                       application_id, creator_id, datecreated, enabled)
                  VALUES 
                      ('".$menuid."', '".$itemname."', '".$itemurl."', '".$sortorder."', '".$contentpage."',
                       '".$newscategory."', '".$application."', 1, NOW(), 0)";
        mysqli_query($dbc, $query);
        $result = "good";
    
    } else {
    
        $result = '';
        while ($row = mysqli_fetch_array($Qresult)) {
            if ($row['itemname'] == $itemname){$result .= 'bad1';} 
            if ($itemurl      && $row['itemurl']         == $itemurl)      {$result .= 'bad2';} else 
            if ($newscategory && $row['newscategory_id'] == $newscategory) {$result .= 'bad3';} else 
            if ($application  && $row['application_id']  == $application)  {$result .= 'bad4';} else 
            if ($contentpage  && $row['contentpage_id']  == $contentpage)  {$result .= 'bad5';} else
            if ($itemurl  && $row['itemurl']  == $itemurl && $itemname  && $row['itemname']  == $itemname)  {$result .= 'bad6';} else
            if ($contentpage  && $row['contentpage_id']  == $contentpage && $row['itemname']  == $itemname)  {$result .= 'bad7';} else
            if ($application  && $row['application_id']  == $application && $row['itemname']  == $itemname)  {$result .= 'bad8';} else
            if ($newscategory  && $row['newscategory_id']  == $newscategory && $row['itemname']  == $itemname)  {$result .= 'bad9';}
        }
    }
}

Link to comment
Share on other sites

Okay this whole thing works except it'll only return the right error if say the itemname matched up with its newscategory, itemurl, contentpage, or application. I want it even if the name doesn't match up.

if (isset($_POST['submitmenuitem'])) {
    $menuid       = (int) $_POST['menuid'];
    $itemname     = trim($_POST['itemname']);
    $itemnameSQL  = mysqli_real_escape_string($dbc, $itemname);
    $itemurl      = trim($_POST['itemurl']);
    $itemurlSQL   = mysqli_real_escape_string($dbc, $_POST['itemurl']);
    $sortorder    = (int) $_POST['sortorder'];
    $contentpage    = mysqli_real_escape_string($dbc, $_POST['contentpage']);
    $newscategory    = mysqli_real_escape_string($dbc, $_POST['newscategory']);
    $application    = mysqli_real_escape_string($dbc, $_POST['application']);

    $query = 
    'SELECT *' . 
    ' FROM menuitems' . 
    ' WHERE menu_id = ' . $menuid .
' AND (itemname = "' . $itemnameSQL . '"'  . 
    ($itemurlSQL   ? ' OR itemurl         = "' . $itemurlSQL  . '"' : '') . 
    ($contentpage  ? ' OR contentpage_id  = '  . $contentpage  : '') . 
    ($application  ? ' OR application_id  = '  . $application  : '') . 
    ($newscategory ? ' OR newscategory_id = '  . $newscategory : '') . 
    ')';
    $Qresult = mysqli_query ( $dbc, $query ); // Run The Query  

    if (mysqli_num_rows($Qresult) == 0) {
        $query = "INSERT INTO `menuitems`
                      (menu_id, itemname, itemurl, sortorder, contentpage_id, newscategory_id,
                       application_id, creator_id, datecreated, enabled)
                  VALUES 
                      ('".$menuid."', '".$itemname."', '".$itemurl."', '".$sortorder."', '".$contentpage."',
                       '".$newscategory."', '".$application."', 1, NOW(), 0)";
        mysqli_query($dbc, $query);
        $result = "good";
    
    } else {
    
        $result = '';
        while ($row = mysqli_fetch_array($Qresult)) {
            if (($newscategory  && $row['newscategory_id'] == $newscategory) AND ($itemname  && $row['itemname']  == $itemname))  {$result .= 'bad9';} else
            if (($application   && $row['application_id']  == $application)  AND ($itemname  && $row['itemname']  == $itemname))  {$result .= 'bad8';} else
            if (($contentpage   && $row['contentpage_id']  == $contentpage)  AND ($itemname  && $row['itemname']  == $itemname))  {$result .= 'bad7';} else
            if (($itemurl       && $row['itemurl']         == $itemurl)      AND ($itemname  && $row['itemname']  == $itemname))  {$result .= 'bad6';} else
            if ($contentpage    && $row['contentpage_id']  == $contentpage)  {$result .= 'bad5';} else
            if ($application    && $row['application_id']  == $application)  {$result .= 'bad4';} else
            if ($newscategory   && $row['newscategory_id'] == $newscategory) {$result .= 'bad3';} else
            if ($itemurl        && $row['itemurl']         == $itemurl)      {$result .= 'bad2';} else 
            if ($itemname == $row['itemname']){$result .= 'bad1';}             
        }
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.