www.webdeveloper.com
Results 1 to 12 of 12

Thread: ]Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result

  1. #1
    Join Date
    Sep 2013
    Posts
    221

    ]Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result

    Hello Experts,

    I am stuck in database query. Well i am making a page which retrieve my database value.
    For which below is my code:-

    PHP Code:
    <?php 
    $con
    =mysqli_connect("localhost","root","","test"); 
    if (
    mysqli_connect_errno()) 

    echo 
    "Failed to connect to MySQL: " mysqli_connect_error(); 
    }

    $result mysqli_query($con,"SELECT * FROM testing where flag=1 ");

    foreach (
    $result as $results){

    while(
    $row mysqli_fetch_array($results)) 

    echo  
    $row['review'] ;
    echo  
    $row['name'] ;

    }
    mysqli_close($con); 
    ?>
    When i run the above code it shows me error:

    PHP Code:
    Warningmysqli_fetch_array() expects parameter 1 to be mysqli_result, array given in /home/stradsol/public_html/test1.php on line 14 

    I also tried by replacing with:

    PHP Code:
    while($row mysqli_fetch_array($resultsMYSQL_BOTH)) 
    But still the same error

    I am stuck. I dont know what I am missing.

    Any help is appreciated.
    strad solutionswww.stradsolutions.com

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    660
    Having a bad day?

    YOu are using a foreach to break out a single row from the query results resource/array. But then you feed a single row to the while loop which doesn't make much sense.

    Dump the foreach.

  3. #3
    Join Date
    Sep 2013
    Posts
    221
    Quote Originally Posted by ginerjm View Post
    Having a bad day?

    YOu are using a foreach to break out a single row from the query results resource/array. But then you feed a single row to the while loop which doesn't make much sense.

    Dump the foreach.

    Even after removing the foreach loop it showing the same "warning" .
    Actually i have to use some css in each of my review row records therefore i am using "foreach loop" .
    So please can some one help me out
    strad solutionswww.stradsolutions.com

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    660
    You CAN'T use a foreach and a while loop on the results of a query!!! The message is telling you exactly what is wrong - why can't you read it?

    And what the H... does a foreach loop have to do with CSS that your while loop won't accomplish?

    Drop the foreach and use this line:

    while ($row = mysqli_fetch_array($result))


    Trust me!

  5. #5
    Join Date
    Sep 2013
    Posts
    221
    Quote Originally Posted by ginerjm View Post
    You CAN'T use a foreach and a while loop on the results of a query!!! The message is telling you exactly what is wrong - why can't you read it?

    And what the H... does a foreach loop have to do with CSS that your while loop won't accomplish?

    Drop the foreach and use this line:

    while ($row = mysqli_fetch_array($result))


    Trust me!
    I trust u and the code runs exactly perfect. But i want to apply css to each of my records as in "quote css". For that what can i do ??


    I'l paste my css done code:

    PHP Code:

    <?php 
    $con
    =mysqli_connect("localhost","root","","test"); 
    if (
    mysqli_connect_errno()) 

    echo 
    "Failed to connect to MySQL: " mysqli_connect_error(); 
    }


    $result mysqli_query($con,"SELECT * FROM testing where flag=1 ");

    foreach (
    $result as $results){
    echo 
    "<div class=\"testimonial\">";
    while(
    $row mysqli_fetch_array($resultsMYSQL_BOTH)) 


    echo  
    $row['review'] ;
    echo 
    "<div class=\"arrow-down\">";
    echo 
    "</div>";
    echo 
    "<div class=\"testimonial-author\">";
    echo  
    $row['name'] ;
    echo 
    "</div>";


    echo 
    "</div>"."<br>";

    }


    mysqli_close($con); 
    ?>
    Is der any other way?? what must i do now
    I am stuck and really my brains not working for this.
    strad solutionswww.stradsolutions.com

  6. #6
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    660
    ok - I give up.

    I've told you twice YOU CAN'T USE FOREACH AND WHILE, but you insist that you know better.

    Good luck.

  7. #7
    Join Date
    Sep 2013
    Posts
    221
    Quote Originally Posted by ginerjm View Post
    ok - I give up.

    I've told you twice YOU CAN'T USE FOREACH AND WHILE, but you insist that you know better.

    Good luck.
    I dont know it therefore i am here with my doubts. I really appreciated while people are correcting me with my mistake and so do i appreciate your reply.
    Please correct me with my code. Paste the code so that can help me out.
    strad solutionswww.stradsolutions.com

  8. #8
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    660
    Do What I said to do.

    As for adding styling - So far you are just echoing out two data items. Change those lines to some real html code showing that data and include whatever css you want to in those lines. So simple. Try it. Then show us YOUR code.

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,241
    Note that mysqli_query() never returns an array (nor an iterable object), so using its result in a foreach() will never work. Here's a somewhat more robust version that should at least tell you where any errors are (though if I were doing it I'd use the object-oriented style, not the functional style):
    PHP Code:
    <?php 
    $con
    =mysqli_connect("localhost","root","","test"); 
    if (
    mysqli_connect_errno()) 

        die(
    "Failed to connect to MySQL: " mysqli_connect_error()); 
    }
    // define query separately so we can reference it in error message:
    $query "SELECT * FROM testing where flag=1";
    $result mysqli_query($con$query);
    // always make sure you get a valid result!!!
    if($result == false) {
        die(
    "Query failed: ".mysqli_error().PHP_EOL.$query);
    }
    // might as well limit results to associative array since that's how you're using it:
    while($row mysqli_fetch_assoc($result)) // $result, not $results 

            
    // example of using HTML in output:
        
    echo  "<h3>".$row['review']."</h3>\n";
        echo  
    "<p>".$row['name']."</p>\n";

    mysqli_close($con);
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  10. #10
    Join Date
    Sep 2013
    Posts
    221
    Quote Originally Posted by NogDog View Post
    Note that mysqli_query() never returns an array (nor an iterable object), so using its result in a foreach() will never work. Here's a somewhat more robust version that should at least tell you where any errors are (though if I were doing it I'd use the object-oriented style, not the functional style):
    PHP Code:
    <?php 
    $con
    =mysqli_connect("localhost","root","","test"); 
    if (
    mysqli_connect_errno()) 

        die(
    "Failed to connect to MySQL: " mysqli_connect_error()); 
    }
    // define query separately so we can reference it in error message:
    $query "SELECT * FROM testing where flag=1";
    $result mysqli_query($con$query);
    // always make sure you get a valid result!!!
    if($result == false) {
        die(
    "Query failed: ".mysqli_error().PHP_EOL.$query);
    }
    // might as well limit results to associative array since that's how you're using it:
    while($row mysqli_fetch_assoc($result)) // $result, not $results 

            
    // example of using HTML in output:
        
    echo  "<h3>".$row['review']."</h3>\n";
        echo  
    "<p>".$row['name']."</p>\n";

    mysqli_close($con);


    Thanks for your reply. It has really helped me.
    I tried out with the below code and my problem is now solved.

    PHP Code:
    <?php 
    $con
    =mysqli_connect("localhost","root","","test"); 
    if (
    mysqli_connect_errno()) 

    echo 
    "Failed to connect to MySQL: " mysqli_connect_error(); 
    }
    $result mysqli_query($con,"SELECT * FROM testing where flag=1 ");
    while(
    $row mysqli_fetch_array($resultMYSQL_BOTH)) 

    echo 
    "<div class=\"testimonial\">";
    echo  
    $row['review'] ;
    echo 
    "<div class=\"arrow-down\">";
    echo 
    "</div>";
    echo 
    "<div class=\"testimonial-author\">";
    echo  
    $row['name'] ;
    echo 
    "</div>";
    echo 
    "</div>"."<br>";

    mysqli_close($con); 
    ?>
    Thanks a lot all.
    strad solutionswww.stradsolutions.com

  11. #11
    Join Date
    Oct 2013
    Posts
    30
    You cannot do a foreach on the return value of mysqli_query.
    And there's no mysqli_query only mysql_query.

  12. #12
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,241
    Quote Originally Posted by williamforrest View Post
    You cannot do a foreach on the return value of mysqli_query.
    And there's no mysqli_query only mysql_query.
    Hmm...maybe you should write a bug report against the php manual page for mysqli_query(), then?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles