Can't insert info in a server, but i can as localhost

后端 未结 1 769
时光取名叫无心
时光取名叫无心 2021-01-27 12:47

Im creating an app, but i can\'t insert in a server using Mysql, php and android studio, i can insert as localhost, but when try to insert in the server it just doesn\'t do it.

1条回答
  •  长发绾君心
    2021-01-27 12:50

    EDIT 1:

    getMessage();
            }
        } else {
            $response['error'] = true;
            $response['message'] = 'Please choose a file.';
        }
    
        // Display response.
        echo json_encode($response);
    } else {
        $response['error'] = true;
        $response['message'] = 'No post method used!';
    
        // Display response.
        echo json_encode($response);
    }
    
    /**
     * We are generating the file name, so this method will 
     * return a file name for the image to be upload.
     * 
     * @return int
     * @throws Exception
     */
    function getFileName() {
        try {
            // Connect to database.
            $con = mysqli_connect(HOST, USER, PASS, DB);
            if (!$con) {
                throw new Exception('Could not connect to database!');
            }
    
            // Select max id.
            $sql = "SELECT max(id) as id FROM reportes2";
            $mysqliResult = mysqli_query($con, $sql);
            if (!$mysqliResult) {
                throw new Exception('The select statement failed!');
            }
    
            // Fetch max id.
            $result = mysqli_fetch_array($mysqliResult);
    
            // Close database connection.
            $closed = mysqli_close($con);
            if (!$closed) {
                throw new Exception('The database connection can not be closed!');
            }
    
            // Validate results.
            if ($result['id'] == null) {
                return 1;
            } else {
                return ++$result['id'];
            }
        } catch (Exception $e) {
            echo $e->getMessage();
            exit();
        }
    }
    



    EDIT 2 - How to prepare and run queries using mysqli library:


    Option 1: Using mysqli_stmt_get_result() + mysqli_fetch_array():

    ', '', '', '');
        if (!$conn) {
            throw new Exception('Connect error: ' . mysqli_connect_errno() . ' - ' . mysqli_connect_error());
        }
    
        //---------------------------------------------------------
        // Sql statement.
        //---------------------------------------------------------
        $query = "SELECT * FROM users WHERE username = ? AND password = ?";
    
        //---------------------------------------------------------
        // Prepare sql statement.
        //---------------------------------------------------------
        $stmt = mysqli_prepare($conn, $query);
        if (!$stmt) {
            throw new Exception('The sql statement can not be prepared!');
        }
    
        //---------------------------------------------------------
        // Bind variables to the prepared statement as parameters.
        //---------------------------------------------------------
        $bound = mysqli_stmt_bind_param($stmt, 'ss', $username, $password);
        if (!$bound) {
            throw new Exception('The variables could not be bound to the prepared statement!');
        }
    
        //---------------------------------------------------------
        // Execute the prepared statement.
        //---------------------------------------------------------
        $executed = mysqli_stmt_execute($stmt);
        if (!$executed) {
            throw new Exception('The prepared statement could not be executed!');
        }
    
        //---------------------------------------------------------
        // Get the result set from the prepared statement.
        //---------------------------------------------------------
        $result = mysqli_stmt_get_result($stmt);
        if (!$result) {
            throw new Exception(mysqli_error($conn));
        }
    
        //---------------------------------------------------------
        // Get the number of rows in statements result set.
        //---------------------------------------------------------
        $rows = mysqli_num_rows($result);
    
        if ($rows > 0) {
            //---------------------------------------------------------
            // Read the result set.
            //---------------------------------------------------------
            $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
            if (!isset($row)) {
                echo 'No records returned!';
                exit();
            }
    
            echo 'Login successful: ' . $row['username'] . '/' . $row['password'];
        } else {
            echo 'Invalid username/password. Please check and retry login.';
        }
    
        //-----------------------------------------------------------
        // Frees stored result memory for the given statement handle.
        //-----------------------------------------------------------
        mysqli_stmt_free_result($stmt);
    
        //---------------------------------------------------------
        // Close db connection.
        //---------------------------------------------------------
        $closed = mysqli_close($conn);
        if (!$closed) {
            throw new Exception('The database connection can not be closed!');
        }
    } catch (Exception $exception) {
        echo '
    ' . print_r($exception, true) . '
    '; exit(); }


    Option 2: Using mysqli_stmt_store_result() + mysqli_stmt_bind_result() + mysqli_stmt_fetch():

    ', '', '', '');
        if (!$conn) {
            throw new Exception('Connect error: ' . mysqli_connect_errno() . ' - ' . mysqli_connect_error());
        }
    
        //---------------------------------------------------------
        // Sql statement.
        //---------------------------------------------------------
        $query = "SELECT * FROM users WHERE username = ? AND password = ?";
    
        //---------------------------------------------------------
        // Prepare sql statement.
        //---------------------------------------------------------
        $stmt = mysqli_prepare($conn, $query);
        if (!$stmt) {
            throw new Exception('The sql statement can not be prepared!');
        }
    
        //---------------------------------------------------------
        // Bind variables to the prepared statement as parameters.
        //---------------------------------------------------------
        $bound = mysqli_stmt_bind_param($stmt, 'ss', $username, $password);
        if (!$bound) {
            throw new Exception('The variables could not be bound to the prepared statement!');
        }
    
        //---------------------------------------------------------
        // Execute the prepared statement.
        //---------------------------------------------------------
        $executed = mysqli_stmt_execute($stmt);
        if (!$executed) {
            throw new Exception('The prepared statement could not be executed!');
        }
    
        //---------------------------------------------------------
        // Transfer the result set from the prepared statement.
        //---------------------------------------------------------
        $stored = mysqli_stmt_store_result($stmt);
        if (!$stored) {
            throw new Exception('The result set from the prepared statement could not be transfered!');
        }
    
        //---------------------------------------------------------
        // Get the number of rows in statements' result set.
        //---------------------------------------------------------
        $rows = mysqli_stmt_num_rows($stmt);
    
        if ($rows > 0) {
            //---------------------------------------------------------
            // Bind result set columns to corresponding variables.
            //---------------------------------------------------------
            $bound = mysqli_stmt_bind_result($stmt, $resId, $resUsername, $resPassword);
            if (!$bound) {
                throw new Exception('The result set columns could not be bound to the variables');
            }
    
            //--------------------------------------------------------------------
            // Fetch results from the prepared statement into the bound variables.
            //--------------------------------------------------------------------
            while (mysqli_stmt_fetch($stmt)) {
                echo 'Successfully returned data:

    '; echo 'ID: ' . $resId . '
    '; echo 'Username: ' . $resUsername . '
    '; echo 'Password: ' . $resPassword . '
    '; } } else { echo 'Invalid username/password. Please check and retry login!'; } //----------------------------------------------------------- // Free stored result memory for the given statement handle. //----------------------------------------------------------- mysqli_stmt_free_result($stmt); //--------------------------------------------------------- // Close db connection. //--------------------------------------------------------- $closed = mysqli_close($conn); if (!$closed) { throw new Exception('The database connection can not be closed!'); } } catch (Exception $exception) { echo '
    ' . print_r($exception, true) . '
    '; exit(); }

    Nota bene: Trying to use mysqli_stmt_store_result() together with mysqli_stmt_get_result() will lead to errors.

    0 讨论(0)
提交回复
热议问题