1

i have using mysqli_ and i want to use mysqli_query(). it need two parameters and connection is in another file. so how can i use that connection variable in it. in function code how can i write that mysqli_query() with two parameters.

connction db code

<?php
    $db_host = "localhost";
    $db_user = "root";
    $db_pass = "";
    $db_name = "chat";

    if($connection = mysqli_connect($db_host, $db_user, $db_pass)){
        echo ("connected to server");
        if($database = mysqli_select_db( $connection, $db_name )){
            echo "connected to database";
        }else{
            echo "dtabase error";
        }
    }else{
        echo "sever error";
    }
?>

function code

<?php
    global $connection;
    function get_msg(){
        $query = "SELECT Sender,Message from chat";

        $run = mysqli_query($query,$connection);

        $messages = array();

        while($message = mysqli_fetch_assoc($run)){
            $messages[] = array('sender'=>$message['Sender'],'message'=>$message['Message']);
        }

        return $messages;
    }

    function send_msg($sender,$message){
        if(!empty($sender) && !empty($message)){
            $sender = mysqli_real_escape_string($sender);
            $message = mysqli_real_escape_string($message);

            $query = "INSERT INTO chat VALUES({$sender},{$message})";

            if(mysqli_query($query)){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

?>

enter image description here

8
  • Did you look at the manual? You have the same issue with the escaping. Commented Dec 2, 2017 at 15:26
  • no i didn't. where is it manual? Commented Dec 2, 2017 at 16:35
  • i read it but there is no answer for my problem. Commented Dec 2, 2017 at 16:44
  • Can you update the question showing your usage as the manual displays? Commented Dec 2, 2017 at 16:45
  • i don't understand Commented Dec 2, 2017 at 16:49

1 Answer 1

1

You need to pull those variables into your function. This can be done using the global keyword.

function send_msg($sender,$message){
    global $connection;

    if(!empty($sender) && !empty($message)){
        $sender = mysqli_real_escape_string($connection, $sender);
        $message = mysqli_real_escape_string($connection, $message);

        $query = "INSERT INTO chat VALUES({$sender},{$message})";

        if(mysqli_query($connection, $query)){
            return true;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

NOTE: there are potential issues when using global and it is generally discouraged. See chris85's comment below.

Sign up to request clarification or add additional context in comments.

2 Comments

To me the question is unclear if it is scope related or just a lack of reading the manual. Using the global may resolve the issue, but potential issues it could bring should be noted, stackoverflow.com/questions/12445972/stop-using-global-in-php.
this is not wok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.