【问题标题】:Undefined variable and mysqli errors未定义的变量和 mysqli 错误
【发布时间】:2016-11-05 23:01:58
【问题描述】:

我的代码中出现了这些错误,我不知道为什么

错误:

1. Undefined variable: con in /opt/local/apache2/htdocs/SE/index.php on line 28
 2. mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /opt/local/apache2/htdocs/SE/index.php on line 28
 3. Undefined variable: con in /opt/local/apache2/htdocs/SE/index.php on line 29
 4. mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /opt/local/apache2/htdocs/SE/index.php on line 29
 5. Undefined variable: con in /opt/local/apache2/htdocs/SE/index.php on line 31
 6. mysqli_query() expects parameter 1 to be mysqli, null given in /opt/local/apache2/htdocs/SE/index.php on line 31
 7. mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /opt/local/apache2/htdocs/SE/index.php on line 32
 8. mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /opt/local/apache2/htdocs/SE/index.php on line 33

这是我的文件

dbcon.php

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "login";

function getConnected($hostname, $username, $password, $database) {
        $con = mysqli_connect($hostname, $username, $password, $database);

        if($con->connect_error)
            die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return $con;}?>

index.php

<?php session_start(); ?>
<?php include('dbcon.php');?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="form-wrapper">
  
  <form action="#" method="post">
    <h3>Влез в системата</h3>
	
    <div class="form-item">
		<input type="text" name="user" required="required" placeholder="Фак. Номер" autofocus required/>
    </div>
    
    <div class="form-item">
		<input type="password" name="pass" required="required" placeholder="Парола" required/>
    </div>
    
    <div class="button-panel">
		<input type="submit" class="button" title="Log In" name="login" value="Влез"/>
    </div>
  </form>
  <?php
	if (isset($_POST['login']))
		{
			$username = mysqli_real_escape_string($con, $_POST['user']);
			$password = mysqli_real_escape_string($con, $_POST['pass']);
        
			$query 		= mysqli_query($con, "SELECT * FROM users WHERE  password='$password' and username='$username'");
			$row		= mysqli_fetch_array($query);
			$num_row 	= mysqli_num_rows($query);
			
			if ($num_row > 0) 
				{			
					$_SESSION['user_id']=$row['user_id'];
					header('location:home.php');
					
				}
			else
				{
					echo 'Invalid Username and Password Combination';
				}
		}
  ?>
  <div class="reminder">
    <p>Not a member? <a href="#">Sign up now</a></p>
    <p><a href="#">Forgot password?</a></p>
  </div>
</div>
</body>
</html>

我得到了 mysqli_query()、mysqli_fetch_array() 和 mysqli_num_rows() 的错误。他们都希望参数 1 为 mysqli_result,null 给出。

【问题讨论】:

  • 你永远不会调用你的 getConnected 函数。使用$con=getConnected();

标签: php html mysqli compiler-errors


【解决方案1】:

在您的dbcon.php 文件中,您有一个函数getConnected($hostname, $username, $password, $database),但您从未使用它。您应该在 index.php 的代码中执行以下操作:

$con = getConnected($hostname, $username, $password, $database);

它将如下所示:

if (isset($_POST['login'])) {
    $con = getConnected($hostname, $username, $password, $database);
    $username = mysqli_real_escape_string($con, $_POST['user']);
    $password = mysqli_real_escape_string($con, $_POST['pass']);
    // ........ rest of your code

或者你可以将这些行写出一个函数:

$con = mysqli_connect($hostname, $username, $password, $database);

        if($con->connect_error) // should be mysqli_connect_errno()
            die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return $con; // without return

所以 dbcon.php 看起来像这样:

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "login";

$con = mysqli_connect($hostname, $username, $password, $database);

if($con->connect_error) // should be mysqli_connect_errno()
    die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());

同样在你的函数(dbcon.php)中检查是否建立连接写错了,应该是这样的:

if (mysqli_connect_errno()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}

您正在使用程序风格,更多信息可以找到here

【讨论】:

  • 如果我用 $con = new getConnected();我收到该错误致命错误:在第 27 行的 /opt/local/apache2/htdocs/SE/index.php 中找不到类“getConnected”
  • 抱歉,拼错了 :D 请再次检查我的答案 :)
  • 我仍然收到错误;D 缺少参数和 dbcon.php 中未定义的变量(主机名、用户名、密码、数据库)
  • 现在请检查 ;)
  • 是的,我注意到该错误,但现在我有 mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: NO) in /opt/local/第 8 行的 apache2/htdocs/SE/dbcon.php 并尝试在第 10 行的 /opt/local/apache2/htdocs/SE/dbcon.php 中获取非对象的属性
猜你喜欢
  • 2018-01-29
  • 2014-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多