JamesF
JamesF

Reputation: 67

Call to undefined function query() when it is defined

I have searched through similar questions and can't find the answer, though I am a beginner so may have missed it.

I am trying to call the review_create() function I have included the reviews.php which contains this function. However, I am getting the error

Fatal error: Call to undefined function query() in C:\xampp\htdocs\cafe\review.php

The PHP for user_pages.php:

<?php
        require_once 'review.php';
        require_once 'cafe.php';
        require_once 'logged_in.php';

        $RATING_MAP = array('5' => 'Excellent', '4' => 'Good', '3' => 'Ok', '2' => 'Bad', '1' => 'Awful', '0' => '---');
        $error = array();//holds multiple errors
        $id = $rating = $review = $action = '';
        $self = $_SERVER['PHP_SELF'];

        if (isset($_REQUEST['action'])) {
            $action = $_REQUEST['action'];
        }

        if ($action == 'delete') {
            if (isset($_REQUEST['id'])) {
                review_delete($_REQUEST['id'], $error);
            } else {
                $error[count($error)] = "Cannot delete the review, missing ID!";
            }
        } 

        elseif ($action == 'create') {
            if ((isset($_REQUEST['rating']))
                    && (isset($_REQUEST['review'])
                            && (isset($_REQUEST['review_cafe'])))) {
                $rating = $_REQUEST['rating'];
                $review = $_REQUEST['review'];
                $review_cafe = $_REQUEST['review_cafe'];
                if ($rating == '---' or $review == '' or $review_cafe == '') {
                    $error[count($error)] = "You must provide a cafe, rating and review!";
                } else {
                    review_create($review_cafe, $rating, $review, $error);
                }
                    }
            else {
                $error[count($error)] = "Unable to create review, missing parameters!";
            }
        }
?>

The PHP for reviews.php:

<?php
    require_once "sql.php";

    function review_create($review_cafe, $rating, $review, &$error) {
        $query = "INSERT INTO Reviews (cafe, rating, review) VALUES (". "'$review_cafe', $rating, '$review');";
        $success = query($query, $error);
        return $success;

    }
?>

Here is sql.php where $query is defined:

<?php //sql.php
require_once "sql_constants.php";

class Sql {

private static $connection = false;
private static $error;  // holds the last error.

static function getConnection() {
    if (!Sql::$connection) {
        Sql::setConnection(mysqli_connect(SQLHOST, SQLUSER, SQLPASSWORD, HOSTDB));
        if (!Sql::$connection) {
            Sql::setConnection(false);
            Sql::setError("Could not connect...");
        }
    }
    return Sql::$connection;
}

private static function setConnection($iConnection) {
    Sql::$connection = $iConnection;
}

private static function setError($iError) {
    Sql::$error = $iError;
}

static function getError() {
    return Sql::$error;
}

static function query($query) {
    $result = false;
    Sql::setError(false); // reset the error
    if ($link = Sql::getConnection()) {
        $result = mysqli_query($link, $query);
        if (!$result) 
            Sql::setError(mysqli_error($link));

        }
        return $result;
    }

    static function getInsertID() {
    // Returns the last id automatically inserted into the
    // database.
        return mysqli_insert_id(Sql::getConnection());
    }

    }
?>

Cheers James

Upvotes: 1

Views: 11570

Answers (3)

NoobEditor
NoobEditor

Reputation: 15871

Avoid using mysql, use mysqli_ instead and update your code to use mysqli_query in place of query (that is the error!!)

simply do :

return (mysqli_query( $conn, $query  )); //$conn is connection object


OR, if you are doubtfull of result, use mysqli_error()

    $success = mysqli_query($con, $query);

    if ( $success === false) 
    {
      printf("Error is : ", mysqli_error($con));
    }
    else 
    {
      return ($success);         
    }

Upvotes: 2

qaztype
qaztype

Reputation: 73

I prefer to use PDO or MYSQLI instead of that, which is easy to inject. (http://en.wikipedia.org/wiki/SQL_injection) see http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

Upvotes: 1

sunny
sunny

Reputation: 1166

error here- $success = query($query, $error);

It should be - $success = mysql_query($query, $error);

unless You do have a method query() which executes the query

Upvotes: 1

Related Questions