Jack
Jack

Reputation: 950

Undefined connection variable in PHP

I've got a php script as follows:

function addPost(BlogPost $item, $tags) {
        $connection = mysql_connect('localhost', '***', '***') or die(mysql_error());
        mysql_select_db('jschaible1') or die(mysql_error());

        $queryString = "insert into BlogPost values ( null, '" . $item->Title . "', '" . $item->Body . "', " . "now());";

        $result = mysql_query($queryString) or die(mysql_error());

        $dbResult = mysql_query('select * from blogpost where Title = "' . $item->Title . '";') or die(mysql_error());
        while ($row = mysql_fetch_array($dbResult)) {
            $tableID = $row['BlogPostID'];
        }

        foreach($tags as $t) {
            $queryString = "insert ignore into Tag values('" . strtolower($t) . "');";
            mysql_query($queryString) or die(mysql_error());

            $queryString = "insert into blogposttag values (" . $tableID . ", '" . strtolower($t) . "');";
            mysql_query($queryString) or die(mysql_error());
        }

        echo $connection;
        mysql_close($connection) or die(mysql_error());
    }

The function is being called like this:

<?php
    session_start();

    $errors = '';

    if (!isset($_SESSION['dadfg6d5f6df54']))
        header('Location:admin.php');
    else {
        include('Classes.php');
        include('mySql.php');
        include('utils.php');

        if(isset($_POST['Submit'])) {
            if ($_POST['Title'] == '') {
                $errors = 'Post must have a title!';
            }
            else if ($_POST['PostBody'] == '') {
                $errors = 'Post must be something!';
            }
            else if (strlen($_POST['PostBody']) < 10) {
                $errors = "Write something substantial, c'mon!";
            }
            else if ($_POST['Tags'] == '') {
                $errors = "At least one tag must be entered";
            }
            else {
                $newPost = new BlogPost(NULL, sanitize($_POST['Title']), sanitize($_POST['PostBody']), NULL);

                $newPost->Title = addEmoticons($newPost->Title);
                $newPost->Body = addEmoticons($newPost->Body);

                $tags = str_replace(',', '', $_POST['Tags']);
                $tags = str_replace(';', '', $tags);
                $tags = explode(' ', $tags);

                error_reporting(E_ALL); ini_set('display_errors', 1);
                addPost($newPost, $tags) or die();

                $errors = 'Post added successfully';
            }
        }
    }
?>

When it gets to mysql_close(), the page just stops executing and I get a blank page. This is really frustrating me, I don't understand at all why it's happening, especially seeing as how the echo on the PREVIOUS line puts out "resource id#6". I get no error message, just a blank page! Please help!

Upvotes: 1

Views: 121

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270617

Your function has no return value. It therefore returns NULL which evaluates as a "falsy" value. Since you follow it with an or die() call, the false evaluation triggers the or die() and terminates your script.

// Don't do this:
addPost($newPost, $tags) or die();

// Do this:
addPost($newPost, $tags);

In the end of your function, you could return TRUE, but it is entirely unnecessary unless you wish to return a value based on the success or failure of your post addition. The way you have it, the die() is just causing undue harm. Since all the potential failing points in your function are already going to terminate the script on error, there is no great purpose to returning TRUE. Just remove the die() after the function call.

Upvotes: 2

Related Questions