Jump to content

Recommended Posts

I wrote this function and the query is returning a false every time. I am connected to the database. I ran the query in phpadmin to see if it was something that I wrote improperly and it runs just fine in there. I have looked it over and had rewritten it based on information that I had found while searching articles and other forums. Nothing is changing with the outcome.

 

What am I doing wrong? what am I missing?

function category($dbc, $type) {
       echo '<div class="form-group">
            <label for="category">Category</label>';
                $stmt = $dbc->prepare("SELECT * FROM `category` WHERE type = ?");
                $stmt->bind_param("i", $type);
                $stmt->execute();
                $stmt->store_result();
                $result = $stmt->get_result();


        echo  '<select class="form-control" id="category" name="category">';

            while ($row = $result->fetch_assoc()){
                echo '<option value="'.$row['id'].'">'.$row['category'].'</option>';
            }
 
           echo '</select>';
        echo '</div>';
        $stmt->free_results();
        $stmt->close();
    }
Link to comment
https://forums.phpfreaks.com/topic/304033-getting-a-false-boolean-on-a-function/
Share on other sites

to start with there is no - free_results(). you may also be on a system that doesn't have the ->get_result() method available. do you have php's error_reporting set to E_ALL and display_errors set to ON, so that php will help you by reporting and displaying all the errors it detects.

 

next you need to always have error handling for all your statements that can fail due to an error. the easiest way of adding error handling, that doesn't involve of adding conditional logic to every statement that can fail, is to use exceptions. to enable exceptions for the php mysqli extension, add the following two lines of code before you make the database connection (you can then also remove any connection error handling logic you may have in your code now) - 

// note: the $driver variable name in these two lines of code has no relation to any other variable name you have in your code
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT; // MYSQLI_REPORT_ALL <- w/index checking; w/o index checking -> MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;

enabling exceptions, combined with the error_reporting/display_errors settings will cause php to catch any errors and display the actual error information. 

 

when on a public/live server, where you wouldn't want to display the actual error information, set the display_errors setting to off, and set the log_errors setting to on to cause the actual error information to be logged instead.

You cannot combine store_result() and get_result(). Since you seem to have a lot of trouble with mysqli (which is understandable), I suggest you either give up and switch to the much more programmer-friendly PDO. Or spend time on actually learning mysqli.

 

Several other things:

  • You need to untangle the spaghetti code. You're wildly jumping from PHP to HTML to SQL to HTML to PHP and back again. This makes the code very difficult to read, even more difficult to maintain and increases the risk of defects. Just return an array and do the HTML rendering when you actually build the page.
  • “TYPE” is a MySQL keyword. While you can still use it as a name in some contexts, you should seriously consider renaming it to something which isn't used by MySQL.
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.