dtpf76658 2014-07-01 18:58
浏览 26

PHP代码工作正常,直到它被放入函数[关闭]

I am mystified. The below code works just fine if it's enclosed in tags and the $data1, $data2 variables are gotten via $POST. However I'd like it enclosed in a function, and if I do:

function ($input1, $input2) {  (then the code here)  }

It fails. Is it something to do with the variables? Or the that you (just a guess) can't "require" something within a function?

Anyway...here's the code. I feel like this is something simple and syntactic, but I'm totally stumped. Here's the code:

//Set the connection or die returning an error.
require "connection.php";

echo 'connected';

    // Make the query:

    $query = "INSERT INTO message (col1, col2) VALUES ('$somedata1', '$somedata2')";
    $result = @mysqli_query ($myconnection, $query); // Run the query.

    if ($result) { // If it ran OK.
echo "Success!";

    } else { // If it did not run OK.

        echo "Error";

    } // End of if 

    mysqli_close($myconnection); // Close the database connection.

//Free the result set.
mysqli_free_result($result);

//Close the connection.
mysqli_close($myconnection);

echo "<span style='color:red;'>Done.</span>";

EDITING to add some more code:

First, the DB connection is as follows:

<?php

DEFINE ('DB_USER', 'abcdef');
DEFINE ('DB_PASSWORD', '12345');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'mydb');

// Make the connection:
echo "Defined.";
$myconnection = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) 
OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );
echo "Connected.";

// Set the encoding...

mysqli_set_charset($myconnection, 'utf8');

?>

And I was not calling this anywhere else in the page that had the function. My hope was to have everything needed to connect and store all tightly there inside the function. Maybe this helps or makes more sense?

I had not gotten to the point where I was calling the function. Just including it at the top with the other functions was causing a white, blank page to appear.

  • 写回答

1条回答 默认 最新

  • dongyang9813 2014-07-01 19:02
    关注

    You are using the variables $myconnection, $data1 and $data2 which are defined outside the function. Add this line to the start of the function and it will work. Note that you have to put every variable's name there that is used but not defined in the function.

    global $myconnection, $data1, $data2;
    

    Also, don't put the require in the function.

    EDIT: As many have suggested, I'll include other ways.

    Function argument:

    function func($myconnection, $data1, $data2) {
        // ... 
    } 
    
    // Calling the function 
    func($myconnection, $data1, $data2);
    

    $GLOBALS:

    Replace all occurrences of $myconnection, $data1 and $data2 with $GLOBALS['myconnection'], $GLOBALS['data1'] and $GLOBALS['data2'].

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度