简体   繁体   English

脚本未正确发送登录信息

[英]Script isn't sending login information correctly

I'v just began coding my own script about an hour ago and I'v come across an error that I'm unsure how to fix, It appears that everything is parsing correctly except the username, for some reason when I execute my PHP table create script it's not parsing the username from config.php instead it's executing the script with no username. 我大约一个小时前才开始编写自己的脚本,遇到一个不确定的错误,似乎是用户名,由于执行我的PHP表时,由于某些原因,所有内容都可以正确解析创建脚本,它不是从config.php解析用户名,而是执行没有用户名的脚本。

I'v put the code though phpcodechecker.com/ and it's given me nothing, not even any warnings, it has me really quite confused, the code looks fine to me but I am quite novice at this so I'v probably missed something HUGE, any help would be greatly appreciated. 我把代码放到phpcodechecker.com/上了,它什么也没给我,甚至没有警告,它确实让我很困惑,代码对我来说看起来还不错,但是我对此还是新手,所以我可能错过了一些东西, 任何帮助将不胜感激。

Why isn't this code parsing the username through to createtable.php when it attempts to connect?? 为什么这段代码在尝试连接时不解析用户名到createtable.php?

Code snippets in question; 相关代码段; The way stack formats code blocks confuses the hell out of me so I'll just use pastebin 堆栈格式代码块的方式使我感到困惑,所以我只使用pastebin

config.php config.php文件

createtable.php createtable.php

This is because you are mixing mysqli and mysql api in config.php which end up with no connection at all 这是因为您在config.php中混合了mysqlimysql api,最终根本没有任何连接

mysqli_connect($db_hostname, $db_username, $db_password)

mysql_select_db($db_database) 

To extabilish a correct connectio to database with mysqli (wich is obviously better to use since mysql_* functions are deprecated) do something like this: 要使用mysqli将正确的连接扩展到数据库(由于不mysql_*使用mysql_*函数,显然最好使用mysql_* ),请执行以下操作:

$mysqli = new mysqli('localhost', 'fake_user', 'my_password', 'my_db');

if ($mysqli->connect_errno) {
    die('Connect Error: ' . $mysqli->connect_errno);
}

You can check documentation here 您可以在这里查看文档

I would like to add that you can use connection from config.php in any other scripts simply requiring it. 我想补充一点,您可以在需要它的任何其他脚本中使用来自config.php连接。

require('config.php');

so your creatables.php will look like this 因此您的creatables.php将如下所示

require("config.php");

$sql="CREATE TABLE quoter (
    id int NOT NULL AUTO_INCREMENT,
    quote text NOT NULL,
    quoteby CHAR(50) NOT NULL,
    PIMARY KEY (id)
    )";

if (mysqli_query($mysqli,$sql))
{
    echo "Table 'quoter' was created sucessfully";
}
else
{
    echo "Error creating table 'quoter': ".mysqli_error($mysqli);
}

and your config.php 和你的config.php

$mysqli = new mysqli('localhost', 'fake_user', 'my_password', 'my_db');

if ($mysqli->connect_errno) {
    die('Connect Error: ' . $mysqli->connect_errno);
}

I would like you to note that $mysqli is the parameter wich handle connection so you need to pass this one while executing operations in your database, eg 我希望您注意到$mysqli是句柄连接的参数,因此您在执行数据库中的操作时需要传递此参数,例如

mysqli_query($mysqli,$sql);
           //^here it is

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM