www.webdeveloper.com
Results 1 to 14 of 14

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

Hybrid View

  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
    Share on Google+

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,902
    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.
    Share on Google+

  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
    Share on Google+

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,902
    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!
    Share on Google+

  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
    Share on Google+

  6. #6
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,902
    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.
    Share on Google+

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,332
    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);
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework
    Share on Google+

  8. #8
    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
    Share on Google+

  9. #9
    Join Date
    Oct 2013
    Posts
    29
    You cannot do a foreach on the return value of mysqli_query.
    And there's no mysqli_query only mysql_query.
    Share on Google+

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,332
    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?
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework
    Share on Google+

  11. #11
    Join Date
    Apr 2016
    Location
    samuthprakarn thailand
    Posts
    1
    used followme can resolve this problem.See it for example.

    <?php
    include("connect.php");

    $sql = "SELECT id, event, sensor1, sensor2, sensor3 FROM tablearduino";
    $result = $conn->query($sql);
    if ($result->num_rows > 0)
    { // output data of each row
    while($row = $result->fetch_assoc())
    {
    echo"<tr>";
    echo "<td>".$row["id"]."</td>";
    echo "<td>".$row["event"]."</td>";
    echo "<td>".$row["sensor1"]."</td>";
    echo "<td>".$row["sensor2"]."</td>";
    echo "<td>".$row["sensor3"]."</td>";
    echo"</tr>";
    }
    } else
    {
    echo "0 results";
    }
    ?>
    Share on Google+

  12. #12
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,332
    *sigh*

    Why do people insist on digging up old, old threads like that?

    Closing....

    *sigh*
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework
    Share on Google+

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.15260 seconds
  • Memory Usage 3,340KB
  • Queries Executed 13 (?)
More Information
Template Usage (31):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (7)bbcode_php
  • (4)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (12)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (12)postbit
  • (12)postbit_onlinestatus
  • (12)postbit_wrapper
  • (1)showthread_list
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (27):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./includes/functions_threadedmode.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids_threaded
  • showthread_threaded_construct_link
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • bbcode_parse_start
  • postbit_imicons
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates