mysqli_connect致命错误:require() [英] mysqli_connect Fatal Error: require()

查看:96
本文介绍了mysqli_connect致命错误:require()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发了一个非常简单的UPDATE查询页面,供用户更改其帐户密码,但是在建立MySQLi连接时(似乎是这样)遇到了一些麻烦.我是这门程序设计的新手,这是我第一次尝试执行动态查询,因此希望可以让您一个人轻松地发现它,并乐意提供一些急需的圣贤建议. /p>

这是相关页面: http://www.parochialathleticleague.org/accounts.html

在执行表单的PHP脚本时,我最初只收到空白的空白屏幕.我仔细检查了代码,并竭尽所能来诊断问题.最终在require命令中添加了"OR die"功能之后,现在我收到以下消息:

警告:require(1)[function.require]:无法打开流:在第10行的/home/pal/public_html/accounts.php中没有这样的文件或目录

致命错误:require()[function.require]:无法打开所需的'1' (include_path ='.:/usr/local/php52/pear')在 /home/pal/public_html/accounts.php,第10行

我很沮丧.这是脚本代码:

<?php

// Show errors:
ini_set('display_errors', 1);

// Adjust error reporting:
error_reporting(E_ALL);

// Connect to the database:
require ('../mysqli_connect.php') OR die('Error : ' . mysql_error());

// Validate the school:
if (empty($_POST['school'])) {
    echo "You forgot to enter your school.<br>";
    $validate = 'false';
} else {
    $school = mysqli_real_escape_string($db, trim($_POST['school']));
    $validate = 'true';
}

// Validate the existing password:
if (empty($_POST['pass'])) {
    echo "You forgot to enter your existing password.<br>";
    $validate = 'false';
} else {
    $pass = mysqli_real_escape_string($db, trim($_POST['pass']));
    $validate = 'true';
}

// Validate the new password:
if (empty($_POST['new_pass'])) {
    echo "You forgot to enter your new password.<br>";
    $validate = 'false';
} elseif (empty($_POST['confirm_pass'])) {
    echo "You forgot to confirm your new password.<br>";
    $validate = 'false';
} elseif ($_POST['new_pass'] != $_POST['confirm_pass']) {
    echo "Sorry, your new password was typed incorrectly.<br>";
    $validate = 'false';
} else {
    $new_pass = mysqli_real_escape_string($db, trim($_POST['new_pass']));
    $validate = 'true';
}

// If all conditions are met, process the form:
if ($validate != 'false') {

    // Validate the school/password combination from the database:
    $q = "SELECT school_id FROM user_schools WHERE (school_name='$school' AND pass=SHA1('$pass') )";
    $r = @mysqli_query($db, $q);
    $num = @mysqli_num_rows($r);
    if ($num == 1) {

        // Get the school_id:
        $row = mysqli_fetch_array($r, MYSQLI_NUM);

        // Perform an UPDATE query to modify the password:
        $q = "UPDATE user_schools SET pass=SHA1('$new_pass') WHERE school_id=$row[0]";
        $r = @mysqli_query($db, $q);

        if (mysqli_affected_rows($db) == 1) {
            header("Location: confirm_accounts.html");
        } else {
            echo "Your password could not be changed due to a system error. Apologies for the inconvenience. If this problem continues, please contact us directly.";
        }

    }

}

mysqli_close($db);
exit();

?>

最后,这是连接脚本中所需的代码(当然,省略了帐户值):

<?php

// Set the database access information as constants:
DEFINE ('DB_USER', '***');
DEFINE ('DB_PASSWORD', '***');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', '***');

// Make the connection:
$db = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' .mysqli_connect_error() );

// Set the encoding:
mysqli_set_charset($db, 'utf8');

最近几个小时,我一直在尝试自行解决此问题. Google无法解决.在这里浏览档案无法解决问题.

这是我确实知道的:

  • 它所需的脚本mysqli_connect.php可以独立运行.我已经对其进行了广泛的测试,以确保所有登录信息都是正确的.
  • 该脚本肯定也位于我所要求的父目录中.我尝试将其移动到公共目录并从那里调用它.相同的错误消息.尝试键入整个文件路径.相同的消息.
  • 此外,大多数脚本本身都能正常工作.当我省略所有MySQL行并仅保留基本验证代码时,它运行没有问题,并按要求将我发送到确认页面.建立连接似乎确实是一个问题.

不确定从这里要去哪里.任何帮助将不胜感激!预先非常感谢.

@YourCommonSense-根据您的建议,这是修改后的脚本.仍然出现黑屏.我是否听错了您的建议?

<?php

// Show errors:
ini_set('display_errors', 1);

// Adjust error reporting:
error_reporting(E_ALL);

// Connect to the database:
require ('../mysqli_connect.php');

// Validate the school:
if (empty($_POST['school'])) {
    echo "You forgot to enter your school.<br>";
    $validate = 'false';
} else {
    $school = mysqli_real_escape_string($db, trim($_POST['school']));
    $validate = 'true';
}

// Validate the existing password:
if (empty($_POST['pass'])) {
    echo "You forgot to enter your existing password.<br>";
    $validate = 'false';
} else {
    $pass = mysqli_real_escape_string($db, trim($_POST['pass']));
    $validate = 'true';
}

// Validate the new password:
if (empty($_POST['new_pass'])) {
    echo "You forgot to enter your new password.<br>";
    $validate = 'false';
} elseif (empty($_POST['confirm_pass'])) {
    echo "You forgot to confirm your new password.<br>";
    $validate = 'false';
} elseif ($_POST['new_pass'] != $_POST['confirm_pass']) {
    echo "Sorry, your new password was typed incorrectly.<br>";
    $validate = 'false';
} else {
    $new_pass = mysqli_real_escape_string($db, trim($_POST['new_pass']));
    $validate = 'true';
}

// If all conditions are met, process the form:
if ($validate != 'false') {

    // Validate the school/password combination from the database:
    $q = "SELECT school_id FROM user_schools WHERE (school_name='$school' AND pass=SHA1('$pass') )";
    $r = mysqli_query($db, $q);
    if (!$r) {
    throw new Exception($mysqli->error." [$query]");
    }
    $num = mysqli_num_rows($r);
    if ($num == 1) {

        // Get the school_id:
        $row = mysqli_fetch_array($r, MYSQLI_NUM);

        // Perform an UPDATE query to modify the password:
        $q = "UPDATE user_schools SET pass=SHA1('$new_pass') WHERE school_id=$row[0]";
        $r = mysqli_query($db, $q);
        if (!$r) {
        throw new Exception($mysqli->error." [$query]");
        }

        if (mysqli_affected_rows($db) == 1) {
            header("Location: confirm_accounts.html");
        } else {
            echo "Your password could not be changed due to a system error. Apologies for the inconvenience. If this problem continues, please contact us directly.";
        }

    }

}

mysqli_close($db);
exit();

?>

解决方案

您的代码和解决方案"有两个最大的问题:

  1. 您到处都有@运算符.为此,您对问题有-1票. @运算符邪恶本身. IT负责您看到的空白页.
  2. 但是,您选择的补救措施使情况变得更糟.解决任何错误报告问题的"OR die"并不是魔术.如果使用不当,将导致错误,就像您遇到的错误一样.为此,您在错误消息中有1.

首先,您的收录还可以,所以,就别管它了.

从mysqli中获取错误时,请遵循以下说明:

您需要更强大,更有用的错误报告解决方案,而不是随机添加或死".

如果在整个应用程序代码中都使用mysqli_query()而不将其封装到某些帮助程序类中,则trigger_error()是引发PHP错误的好方法,因为它还会告诉您文件和行号,其中错误发生

$res = mysqli_query($mysqli,$query) or trigger_error(mysqli_error($mysqli)."[$query]");

所有脚本中的
从那时起,系统将通知您未创建对象的原因. (如果您对这种or语法感到好奇,请我已经在此处进行了解释-它也解释了为什么您拥有错误消息中的(1))

但是,如果将查询封装到某个类中,则触发错误的文件和行将非常无用,因为它们将指向调用本身,而不是引起某些问题的应用程序代码.因此,当运行封装的mysqli命令时,必须使用另一种方式:

$result = $mysqli->query($sql);
if (!$result) {
    throw new Exception($mysqli->error." [$query]");
}

作为异常将为您提供堆栈跟踪,它将为您提供调用错误查询的位置.

请注意,您通常必须能够看到PHP错误.在实时站点上,您必须查看错误日志,因此必须进行设置

error_reporting(E_ALL);
ini_set('display_errors',0);
ini_set('log_errors',1);

在本地开发服务器上,可以在屏幕上显示错误:

error_reporting(E_ALL);
ini_set('display_errors',1);

,当然,您永远不要在语句前使用错误抑制运算符(@).

Developing a very simple UPDATE query page for users to change their account password, but have run into a bit of a brick wall with establishing the MySQLi connection (or so it would seem). I'm new to this line of programming and this is my first attempt to perform a dynamic query, so hopefully it's something that one of you can spot easily enough and you'd so kind as to offer some much-needed sage advice.

Here's the page in question: http://www.parochialathleticleague.org/accounts.html

Upon executing the form's PHP script, I was at first receiving nothing but a blank white screen. I scoured through my code and did everything I could think of to diagnose the problem. After eventually adding an "OR die" function to the require command, I am now greeted with this message:

Warning: require(1) [function.require]: failed to open stream: No such file or directory > in /home/pal/public_html/accounts.php on line 10

Fatal error: require() [function.require]: Failed opening required '1' (include_path='.:/usr/local/php52/pear') in /home/pal/public_html/accounts.php on line 10

I'm pretty stumped. Here's the script code:

<?php

// Show errors:
ini_set('display_errors', 1);

// Adjust error reporting:
error_reporting(E_ALL);

// Connect to the database:
require ('../mysqli_connect.php') OR die('Error : ' . mysql_error());

// Validate the school:
if (empty($_POST['school'])) {
    echo "You forgot to enter your school.<br>";
    $validate = 'false';
} else {
    $school = mysqli_real_escape_string($db, trim($_POST['school']));
    $validate = 'true';
}

// Validate the existing password:
if (empty($_POST['pass'])) {
    echo "You forgot to enter your existing password.<br>";
    $validate = 'false';
} else {
    $pass = mysqli_real_escape_string($db, trim($_POST['pass']));
    $validate = 'true';
}

// Validate the new password:
if (empty($_POST['new_pass'])) {
    echo "You forgot to enter your new password.<br>";
    $validate = 'false';
} elseif (empty($_POST['confirm_pass'])) {
    echo "You forgot to confirm your new password.<br>";
    $validate = 'false';
} elseif ($_POST['new_pass'] != $_POST['confirm_pass']) {
    echo "Sorry, your new password was typed incorrectly.<br>";
    $validate = 'false';
} else {
    $new_pass = mysqli_real_escape_string($db, trim($_POST['new_pass']));
    $validate = 'true';
}

// If all conditions are met, process the form:
if ($validate != 'false') {

    // Validate the school/password combination from the database:
    $q = "SELECT school_id FROM user_schools WHERE (school_name='$school' AND pass=SHA1('$pass') )";
    $r = @mysqli_query($db, $q);
    $num = @mysqli_num_rows($r);
    if ($num == 1) {

        // Get the school_id:
        $row = mysqli_fetch_array($r, MYSQLI_NUM);

        // Perform an UPDATE query to modify the password:
        $q = "UPDATE user_schools SET pass=SHA1('$new_pass') WHERE school_id=$row[0]";
        $r = @mysqli_query($db, $q);

        if (mysqli_affected_rows($db) == 1) {
            header("Location: confirm_accounts.html");
        } else {
            echo "Your password could not be changed due to a system error. Apologies for the inconvenience. If this problem continues, please contact us directly.";
        }

    }

}

mysqli_close($db);
exit();

?>

Lastly, here's the code from the connection script that it's requiring (with omitted account values, of course):

<?php

// Set the database access information as constants:
DEFINE ('DB_USER', '***');
DEFINE ('DB_PASSWORD', '***');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', '***');

// Make the connection:
$db = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' .mysqli_connect_error() );

// Set the encoding:
mysqli_set_charset($db, 'utf8');

I've been trying for the last couple of hours to troubleshoot this problem on my own. Google couldn't solve it. Looking through archives here couldn't solve it.

Here's what I do know for sure:

  • The script that it's requiring, mysqli_connect.php, does work on its own. I've tested it extensively to make sure that all of the log-in info is correct.
  • The script is also definitely in the parent directory that I've requested it from. I've tried moving it to the public directory and calling it from there. Same error message. Tried typing the entire file path. Same message.
  • Also, most of the script works fine on its own. When I omitted all of the MySQL lines and just left the basic validation code, it ran without a problem and sent me to the confirmation page as requested. It definitely seems to be a problem with making the connection.

Not sure where to go from here. Any assistance would be GREATLY appreciated! Many thanks in advance.

EDIT: @YourCommonSense - Here's the modified script, as per your suggestions. Still getting the blank screen. Am I following your advice incorrectly?

<?php

// Show errors:
ini_set('display_errors', 1);

// Adjust error reporting:
error_reporting(E_ALL);

// Connect to the database:
require ('../mysqli_connect.php');

// Validate the school:
if (empty($_POST['school'])) {
    echo "You forgot to enter your school.<br>";
    $validate = 'false';
} else {
    $school = mysqli_real_escape_string($db, trim($_POST['school']));
    $validate = 'true';
}

// Validate the existing password:
if (empty($_POST['pass'])) {
    echo "You forgot to enter your existing password.<br>";
    $validate = 'false';
} else {
    $pass = mysqli_real_escape_string($db, trim($_POST['pass']));
    $validate = 'true';
}

// Validate the new password:
if (empty($_POST['new_pass'])) {
    echo "You forgot to enter your new password.<br>";
    $validate = 'false';
} elseif (empty($_POST['confirm_pass'])) {
    echo "You forgot to confirm your new password.<br>";
    $validate = 'false';
} elseif ($_POST['new_pass'] != $_POST['confirm_pass']) {
    echo "Sorry, your new password was typed incorrectly.<br>";
    $validate = 'false';
} else {
    $new_pass = mysqli_real_escape_string($db, trim($_POST['new_pass']));
    $validate = 'true';
}

// If all conditions are met, process the form:
if ($validate != 'false') {

    // Validate the school/password combination from the database:
    $q = "SELECT school_id FROM user_schools WHERE (school_name='$school' AND pass=SHA1('$pass') )";
    $r = mysqli_query($db, $q);
    if (!$r) {
    throw new Exception($mysqli->error." [$query]");
    }
    $num = mysqli_num_rows($r);
    if ($num == 1) {

        // Get the school_id:
        $row = mysqli_fetch_array($r, MYSQLI_NUM);

        // Perform an UPDATE query to modify the password:
        $q = "UPDATE user_schools SET pass=SHA1('$new_pass') WHERE school_id=$row[0]";
        $r = mysqli_query($db, $q);
        if (!$r) {
        throw new Exception($mysqli->error." [$query]");
        }

        if (mysqli_affected_rows($db) == 1) {
            header("Location: confirm_accounts.html");
        } else {
            echo "Your password could not be changed due to a system error. Apologies for the inconvenience. If this problem continues, please contact us directly.";
        }

    }

}

mysqli_close($db);
exit();

?>

解决方案

Two BIGGEST problems with your code and your "solution":

  1. You have @ operator all over the place. For which you have -1 vote to your question. @ operator is the evil itself. IT is responsible for the blank page you see.
  2. However, the remedy you choose made the things worse. This "OR die" thing is not a magic chant to solve any error reporting problem. And used improperly will cause errors like one you have. For which you have 1 in the error message.

First of all, your include is all right, so, leave it alone.

While to get an error from mysqli, follow these instructions:

Instead of adding "or die" randomly, you need more robust and helpful error reporting solution.

If you are using mysqli_query() all over the application code without encapsulating it into some helper class, trigger_error() is a good way to raise a PHP error, as it will tell you also the file and the line number where error occurred

$res = mysqli_query($mysqli,$query) or trigger_error(mysqli_error($mysqli)."[$query]");

in all your scripts
and since then you will be notified of the reason, why the object weren't created. (If you're curious of this or syntax, I've explained it here - it also explains why you have (1) in the error message)

However, if you're encapsulating your query into some class, file and line from trigger error will be quite useless as they will point to the call itself, not the application code that caused certain problem. So, when running mysqli commands encapsulated, another way have to be used:

$result = $mysqli->query($sql);
if (!$result) {
    throw new Exception($mysqli->error." [$query]");
}

as Exception will provide you with a stack trace, which will lead you the the place from which an erroneous query were called.

Note that you have to be able to see PHP errors in general. On a live site you have to peek into error logs, so, settings have to be

error_reporting(E_ALL);
ini_set('display_errors',0);
ini_set('log_errors',1);

while on a local development server it's all right to make errors on screen:

error_reporting(E_ALL);
ini_set('display_errors',1);

and of course you should never ever use error suppression operator (@) in front of your statements.

这篇关于mysqli_connect致命错误:require()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆