简体   繁体   中英

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.

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.

Why isn't this code parsing the username through to createtable.php when it attempts to connect??

Code snippets in question; The way stack formats code blocks confuses the hell out of me so I'll just use pastebin

config.php

createtable.php

This is because you are mixing mysqli and mysql api in config.php which end up with no connection at all

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 = 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.

require('config.php');

so your creatables.php will look like this

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

$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_query($mysqli,$sql);
           //^here it is

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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