中的PHP变量未定义
我正在尝试让一个连接到本地数据库的基本注册页面正常工作中的PHP变量未定义,php,mysql,mysqli,undefined,Php,Mysql,Mysqli,Undefined,我正在尝试让一个连接到本地数据库的基本注册页面正常工作 Notice: Undefined variable: dbhost in C:\wamp\www\functions.php on line 19 Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp\www\functions.php on line 19 Notice: Undefined variable: con in C
Notice: Undefined variable: dbhost in C:\wamp\www\functions.php on line 19
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp\www\functions.php on line 19
Notice: Undefined variable: con in C:\wamp\www\functions.php on line 19
Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\wamp\www\functions.php on line 19
这些是我犯的错误。它们发生在我在functions.php脚本中引用函数时
<?php //functions.php
$dbhost = 'localhost'; //change to webserver ip
$dbname = 'maindb';
$dbuser = 'administrator';
$dbpass = 'password';
$appname = "webapp";
$con = mysqli_connect($dbhost,$dbuser,$dbpass) or die (mysqli_error($con));
mysqli_select_db($con, $dbname) or die (mysqli_error($con));
function createTable($name, $query)
{
queryMysql("CREATE TABLE IF NOT EXISTS $name($query)");
echo "Table '$name' created or already exists<br>";
}
function queryMysql($query)
{
$result = mysqli_query($dbhost, $query) or die(mysqli_error($con));
return $result;
}
这些变量是在全局范围内声明的,因此您的函数不可用。要使它们成为这样,您需要将它们作为该函数的参数传递:
function queryMysql($query, $con, $dbhost)
(您也可以使用global
关键字,但这是一种不好的做法,因此我不会在这里展示)
另外,mysqli\u query()
的第一个参数应该是存储在$con
中的MySQL连接资源,而不是$dbhost
function queryMysql($query, $con)
{
$result = mysqli_query($con, $query) or die(mysqli_error($con));
return $result;
}
mysqli\u query
需要连接而不是主机,您必须将其传入:
function queryMysql($con, $query)
{
$result = mysqli_query($con, $query) or die(mysqli_error($con));
return $result;
}
那么当你叫它:
$result = queryMysql($con, $query)
在函数中使用错误的变量$dbhost