<script type="text/javascript">
function on_callPhp1()
{
var result = "<?php php_func();?>";
document.getElementById('phptext').innerHTML = result;
}
</script>
<form action="" method="POST">
<input type="button" value="Get Numbers" onclick="on_callPhp1()"/>
</form>
<div id="phptext"></div>
<?php
include_once ('/var/www/db/connection.php');
function php_func() {
for ($i = 0; $i <= 25000; $i++) {
$num = (rand(1,1000));
echo "Number #" .$i . "\t ------ " . $num;
echo "<br>";
$queryInsertNum = "INSERT INTO myDb.myTable(number) VALUES ('$num');";
$result = $mysqli -> query($queryInsertNum);
}
}
?>
每当我运行上面的代码时,我都看不到表单按钮。
但是,当我注释掉它们出现的最后一行$result = $mysqli -> query($queryInsertNum);
并且页面正常运行时。
可能出现什么问题?
我的connection.php如下
<?php
global $mysqli;
$mysqli = new mysqli("127.0.0.1", "user1", "somepassword", "myDB");
if ($mysqli -> connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli -> connect_errno . ") " . $mysqli -> connect_error;
}
?>
答案 0 :(得分:1)
我想这是因为$ mysqli是你函数中的一个未知对象,试着在你的函数中添加全局$ mysqli。
<?php
include_once ('/var/www/db/connection.php');
function php_func() {
global $mysqli;
for ($i = 0; $i <= 25000; $i++) {
$num = (rand(1,1000));
echo "Number #" .$i . "\t ------ " . $num;
echo "<br>";
$queryInsertNum = "INSERT INTO myDb.myTable(number) VALUES ('$num');";
$result = $mysqli -> query($queryInsertNum);
}
}
?>
答案 1 :(得分:1)
你有一个variable scope
问题,每个php函数都有它自己的作用域,除非你把这个变量传递给你的函数,否则你不能处理它内部函数外部的变量。
我们在这里讨论的$mysqli
变量。
要解决这个问题,你需要使用全局变量,这被认为是一种不好的做法,checkout this wiki for more details OR ,将变量作为参数传递如下:
// hey `php_func` ,please pass $mysqli value to be able to use it within you
// or whatever the variable name is
function php_func($mysqli) {
for ($i = 0; $i <= 25000; $i++) {
$num = (rand(1,1000));
echo "Number #" .$i . "\t ------ " . $num;
echo "<br>";
$queryInsertNum = "INSERT INTO myDb.myTable(number) VALUES ('$num');";
$result = $mysqli -> query($queryInsertNum);
}
}
但小心,每次调用函数时都需要将该变量传递给它,如下所示:
php_func($mysqli);
这将把我们带到另一个问题,即你是混合和误解the difference between client-side and server-side programming?,
你正在调用这样的php函数:
var result = "<?php php_func();?>";
这是错误的,如果你从firefox中点击CTRL+U
,你就会发现在调用你的HTML事件onclick
之前已经执行了该功能
建议在这种情况下 - ,你需要在客户端和服务器端之间打开一个联系渠道 - 使用ajax
所以,你的脚本可能是这样的:在这里我假设你的文件名是phpfunc.php
ajax部分:
<script type="text/javascript">
function on_callPhp1()
{
$.ajax({
url: 'phpfunc.php?get=data', // change this to whatever your file is
type: 'GET',
success: function (data) {
document.getElementById('phptext').innerHTML = data;
}
});
}
</script>
<form action="" method="POST">
<input type="button" value="Get Numbers" onclick="on_callPhp1()"/>
</form>
<div id="phptext"></div>
<?php
function php_func($mysqli) {
for ($i = 0; $i <= 25000; $i++) {
$num = (rand(1,1000));
echo "Number #" .$i . "\t ------ " . $num;
echo "<br>";
$queryInsertNum = "INSERT INTO myDb.myTable(number) VALUES ('$num');";
$result = $mysqli -> query($queryInsertNum);
}
}
if (isset($_GET['get']) && $_GET['get'] == 'data') {
include_once ('/var/www/db/connection.php');
php_func($mysqli);
}
?>