Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2013
    Location
    Finland/Estonia
    Posts
    53
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Database Looping and inserting errors

    Hey. I get an error when I try to loop my database and print the info i want. This is the index file whitch prints the data:
    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /location/censored/index.php on line 43
    (Line 43 should be the while loop's start)

    PHP Code:
    <?php
    include('./includes/db_connect.php');
    $con=($db);
    // Check connection
    if (mysqli_connect_errno())
      {
      echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
      }

        
    $result mysqli_query($con,"SELECT title, body AND category_id FROM posts  ORDER BY DESC");
        

    while(
    $row mysqli_fetch_array($result))
    {
    echo 
    $row['body'];
    echo 
    $row['title'];
    echo 
    $row['category_id'];
    }
    ?>
    This is what is supposed to insert the data:
    PHP Code:
    include('../includes/db_connect.php');
    if(isset(
    $_POST['submit'])){
        
    $newTitle $_POST['newTitle'];
        
    $newPost $_POST['newPost'];
        
    $my_date date("Y-m-d H:i:s");
        if(!empty(
    $newPost))
            if(!empty(
    $newTitle))
                if(!empty(
    $my_date)){
        
    $sql="INSERT INTO posts (body, title, posted) VALUES $newPost, $newTitle, $my_date";
        
    $query $db->query($sql);
        if(
    $query){
                echo 
    "Post entered to database";
            }else{
                echo 
    "Error Submitting the data";
            }
        }
    }
    ?> 
    Where are the errors? I have fiddeled around for 3 hours now and can't find them.
    Last edited by estXcrew; 08-25-2013 at 04:04 PM.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 593 Times in 592 Posts
    Assuming both php file or in the same folder
    include('./includes/db_connect.php');
    and
    include('../includes/db_connect.php');

    And
    $con=($db);
    maybe
    $con=$db;
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New Coder
    Join Date
    Jul 2013
    Location
    Finland/Estonia
    Posts
    53
    Thanks
    9
    Thanked 0 Times in 0 Posts
    They are in different folders.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    The problem is you are not properly handling your errors. There are clearly syntactical ones in there. The boolean refers to a failure in your query.
    You cannot ORDER BY nothing. You must provide it a property to ORDER BY. It should also be noted that the AND syntax within a field select will result in the boolean operation between the two. So long as nothing is false between the lhs and rhs of the operation, it will result in 1.
    The INSERT syntax is also incorrect. Refer to the documentation here: http://dev.mysql.com/doc/refman/5.6/en/insert.html
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,177
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    Sunfighter: Read the error message.

    mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
    When PHP does a SELECT query, if it succeeds (even if the query returns no records), the result will be a record resource, not a boolean value.

    If it fails (with an error!) then the result will be the boolean value false.

    And so that error message is really saying "Hey, the query failed, so there is no actual result here and so there is nothing for mysqli_fetch_array to work on."

    As Fou-Lu said: He isn't doing any error handlng.

    He should have coded something like
    Code:
    $result = $con->query("SELECT title, body AND category_id FROM posts  ORDER BY DESC"
              ) or die( "ERROR IN QUERY: " . $con->error() );
    (Notice that I used the more modern way of using mysqli, not the procedural style. And I don't even use PHP. <grin/>)

    The point being that when the query fails and returns false then the or die... is triggered. So if you indeed reach the next line after that, then $result will be usable.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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