Anyone know PHP very well?

Status
This thread has been locked.

Badger

Software Developer
Supreme
Feedback score
22
Posts
2,366
Reactions
1,561
Resources
2
Would anyone like to tell me how this would work please? I created this like months ago and I'm not even sure if it's in any way right. Please let me know where I fucked up or if it all looks good and/or if you tested it for me. Thank you. - TheWolfBadger

PHP:
<?php
/**
* Created by PhpStorm.
* User: TheWolfBadger
* Date: 2/17/15
* Time: 2:35 PM
*/
function create_new_row($serverName, $ip, $bannerLocation, $creator) {
    $dir = parse_ini_file("/files/config.ini", true);
    $config = parse_ini_file($dir['Location'], true);
    $logindata = array();
    $logindata['User'] = $config['Database']['User'];
    $logindata['Server'] = $config['Database']['Server'];
    $logindata['Database'] = $config['Database']['Database'];
    $logindata['Port'] = $config['Database']['Port'];
    if($config['Database']['Password']!=null && $config['Database']['Password']!=""){
        $logindata['Password'] = $config['Database']['Password'];
    }else{
        $logindata['Password'] = null;
    }
    $con = new mysqli($logindata['Server'], $logindata['User'], $logindata['Password'], $logindata['Database'], $logindata['Port']);
    if($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    }
    $query = $con->query("INSERT INTO servers (server, ip, bannerLocation) VALUES ('$serverName', $ip, '$bannerLocation')");
    if($query == true) {
        //Run Success block
    } else {
        //Run Error block
    }
}
function remove_row($serverName, $creator) {
    $dir = parse_ini_file("/files/config.ini", true);
    $config = parse_ini_file($dir['Location'], true);
    $logindata = array();
    $logindata['User'] = $config['Database']['User'];
    $logindata['Server'] = $config['Database']['Server'];
    $logindata['Database'] = $config['Database']['Database'];
    $logindata['Port'] = $config['Database']['Port'];
    if($config['Database']['Password']!=null && $config['Database']['Password']!=""){
        $logindata['Password'] = $config['Database']['Password'];
    }else{
        $logindata['Password'] = null;
    }
    $con = new mysqli($logindata['Server'], $logindata['User'], $logindata['Password'], $logindata['Database'], $logindata['Port']);
    if($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    }
    $query = $con->query("DELETE FROM servers WHERE serverName='$serverName'");
    if($query == true) {
        //Run Success block
    } else {
        //Run Error block
    }
}
function getPage($num) {
    $dir = parse_ini_file("/files/config.ini", true);
    $config = parse_ini_file($dir['Location'], true);
    $logindata = array();
    $logindata['User'] = $config['Database']['User'];
    $logindata['Server'] = $config['Database']['Server'];
    $logindata['Database'] = $config['Database']['Database'];
    $logindata['Port'] = $config['Database']['Port'];
    if($config['Database']['Password']!=null && $config['Database']['Password']!=""){
        $logindata['Password'] = $config['Database']['Password'];
    }else{
        $logindata['Password'] = null;
    }
    $con = new mysqli($logindata['Server'], $logindata['User'], $logindata['Password'], $logindata['Database'], $logindata['Port']);
    if($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    }
    $result = $con->query("SELECT * FROM servers");
    $rows = mysql_fetch_assoc($result);
    return $rows;
}
The config.ini:
Code:
[Database]
host = ""
username = ""
password = ""
dbname = ""
port = ""
socket = ""
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

Malek668

Feedback score
1
Posts
369
Reactions
181
Resources
0
Would anyone like to tell me how this would work please? I created this like months ago and I'm not even sure if it's in any way right. Please let me know where I fucked up or if it all looks good and/or if you tested it for me. Thank you. - TheWolfBadger

PHP:
<?php
/**
* Created by PhpStorm.
* User: TheWolfBadger
* Date: 2/17/15
* Time: 2:35 PM
*/
function create_new_row($serverName, $ip, $bannerLocation, $creator) {
    $dir = parse_ini_file("/files/config.ini", true);
    $config = parse_ini_file($dir['Location'], true);
    $logindata = array();
    $logindata['User'] = $config['Database']['User'];
    $logindata['Server'] = $config['Database']['Server'];
    $logindata['Database'] = $config['Database']['Database'];
    $logindata['Port'] = $config['Database']['Port'];
    if($config['Database']['Password']!=null && $config['Database']['Password']!=""){
        $logindata['Password'] = $config['Database']['Password'];
    }else{
        $logindata['Password'] = null;
    }
    $con = new mysqli($logindata['Server'], $logindata['User'], $logindata['Password'], $logindata['Database'], $logindata['Port']);
    if($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    }
    $query = $con->query("INSERT INTO servers (server, ip, bannerLocation) VALUES ('$serverName', $ip, '$bannerLocation')");
    if($query == true) {
        //Run Success block
    } else {
        //Run Error block
    }
}
function remove_row($serverName, $creator) {
    $dir = parse_ini_file("/files/config.ini", true);
    $config = parse_ini_file($dir['Location'], true);
    $logindata = array();
    $logindata['User'] = $config['Database']['User'];
    $logindata['Server'] = $config['Database']['Server'];
    $logindata['Database'] = $config['Database']['Database'];
    $logindata['Port'] = $config['Database']['Port'];
    if($config['Database']['Password']!=null && $config['Database']['Password']!=""){
        $logindata['Password'] = $config['Database']['Password'];
    }else{
        $logindata['Password'] = null;
    }
    $con = new mysqli($logindata['Server'], $logindata['User'], $logindata['Password'], $logindata['Database'], $logindata['Port']);
    if($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    }
    $query = $con->query("DELETE FROM servers WHERE serverName='$serverName'");
    if($query == true) {
        //Run Success block
    } else {
        //Run Error block
    }
}
function getPage($num) {
    $dir = parse_ini_file("/files/config.ini", true);
    $config = parse_ini_file($dir['Location'], true);
    $logindata = array();
    $logindata['User'] = $config['Database']['User'];
    $logindata['Server'] = $config['Database']['Server'];
    $logindata['Database'] = $config['Database']['Database'];
    $logindata['Port'] = $config['Database']['Port'];
    if($config['Database']['Password']!=null && $config['Database']['Password']!=""){
        $logindata['Password'] = $config['Database']['Password'];
    }else{
        $logindata['Password'] = null;
    }
    $con = new mysqli($logindata['Server'], $logindata['User'], $logindata['Password'], $logindata['Database'], $logindata['Port']);
    if($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    }
    $result = $con->query("SELECT * FROM servers");
    $rows = mysql_fetch_assoc($result);
    return $rows;
}
The config.ini:
Code:
[Database]
host = ""
username = ""
password = ""
dbname = ""
port = ""
socket = ""
cant read it -_- its in black :eek:
 

omarhachach

Your Friendly Neighbourhood Snorlax
Supreme
Feedback score
14
Posts
627
Reactions
359
Resources
0
Would anyone like to tell me how this would work please? I created this like months ago and I'm not even sure if it's in any way right. Please let me know where I fucked up or if it all looks good and/or if you tested it for me. Thank you. - TheWolfBadger

PHP:
<?php
/**
* Created by PhpStorm.
* User: TheWolfBadger
* Date: 2/17/15
* Time: 2:35 PM
*/
function create_new_row($serverName, $ip, $bannerLocation, $creator) {
    $dir = parse_ini_file("/files/config.ini", true);
    $config = parse_ini_file($dir['Location'], true);
    $logindata = array();
    $logindata['User'] = $config['Database']['User'];
    $logindata['Server'] = $config['Database']['Server'];
    $logindata['Database'] = $config['Database']['Database'];
    $logindata['Port'] = $config['Database']['Port'];
    if($config['Database']['Password']!=null && $config['Database']['Password']!=""){
        $logindata['Password'] = $config['Database']['Password'];
    }else{
        $logindata['Password'] = null;
    }
    $con = new mysqli($logindata['Server'], $logindata['User'], $logindata['Password'], $logindata['Database'], $logindata['Port']);
    if($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    }
    $query = $con->query("INSERT INTO servers (server, ip, bannerLocation) VALUES ('$serverName', $ip, '$bannerLocation')");
    if($query == true) {
        //Run Success block
    } else {
        //Run Error block
    }
}
function remove_row($serverName, $creator) {
    $dir = parse_ini_file("/files/config.ini", true);
    $config = parse_ini_file($dir['Location'], true);
    $logindata = array();
    $logindata['User'] = $config['Database']['User'];
    $logindata['Server'] = $config['Database']['Server'];
    $logindata['Database'] = $config['Database']['Database'];
    $logindata['Port'] = $config['Database']['Port'];
    if($config['Database']['Password']!=null && $config['Database']['Password']!=""){
        $logindata['Password'] = $config['Database']['Password'];
    }else{
        $logindata['Password'] = null;
    }
    $con = new mysqli($logindata['Server'], $logindata['User'], $logindata['Password'], $logindata['Database'], $logindata['Port']);
    if($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    }
    $query = $con->query("DELETE FROM servers WHERE serverName='$serverName'");
    if($query == true) {
        //Run Success block
    } else {
        //Run Error block
    }
}
function getPage($num) {
    $dir = parse_ini_file("/files/config.ini", true);
    $config = parse_ini_file($dir['Location'], true);
    $logindata = array();
    $logindata['User'] = $config['Database']['User'];
    $logindata['Server'] = $config['Database']['Server'];
    $logindata['Database'] = $config['Database']['Database'];
    $logindata['Port'] = $config['Database']['Port'];
    if($config['Database']['Password']!=null && $config['Database']['Password']!=""){
        $logindata['Password'] = $config['Database']['Password'];
    }else{
        $logindata['Password'] = null;
    }
    $con = new mysqli($logindata['Server'], $logindata['User'], $logindata['Password'], $logindata['Database'], $logindata['Port']);
    if($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    }
    $result = $con->query("SELECT * FROM servers");
    $rows = mysql_fetch_assoc($result);
    return $rows;
}
The config.ini:
Code:
[Database]
host = ""
username = ""
password = ""
dbname = ""
port = ""
socket = ""
Why are you doing
PHP:
$dir = parse_ini_file("/files/config.ini", true);
$config = parse_ini_file($dir['Location'], true);
If, you want $config["database"]["user"] or something like that.
Just do:
PHP:
$config = parse_ini_file("/files/config.ini", true);
It is all you need.

--
Best Regards,
Omar H. of NoClue Development.
 

Badger

Software Developer
Supreme
Feedback score
22
Posts
2,366
Reactions
1,561
Resources
2
Why are you doing
PHP:
$dir = parse_ini_file("/files/config.ini", true);
$config = parse_ini_file($dir['Location'], true);
If, you want $config["database"]["user"] or something like that.
Just do:
PHP:
$config = parse_ini_file("/files/config.ini", true);
It is all you need.

--
Best Regards,
Omar H. of NoClue Development.
Updated my code, let me know what ya'll feedback is! Thank you, appreciate any criticism! :)
Code:
<?php

// create_new_row("test", "test", "test");
// remove_row("test");
// getPage(1);

# Create a new row in table $table
# @Usage create_new_row("servers", "serverName, ip, bannerLocation, creator",
# "MinePlex, Mineplex.com, http://website.com/dir/banners/MineplexBanner, Jared"
function create_new_row($table, $tableCategories, $newValues) {
    $query = mysqli_prepare(connection(),
        "INSERT INTO '$table' ('$tableCategories') VALUES ('$newValues');"
    );

    if ($query->execute() == true) {
        //Run Success block
    } else {
        //Run Error block
        print "False, Failed";
    }
}

# Create a database in MySQL
# @Usage create_database("myDatabase");
function create_database($database) {
    $query = mysqli_prepare(connection(),
        "CREATE DATABASE '$database';");
    if ($query->execute() == true) {
        //Run Success block
    } else {
        //Run Error block
        print "False, Failed";
    }
}

# Delete a database in MySQL
# @Usage delete_database("myDatabase");
function delete_database($database) {
    $query = mysqli_prepare(connection(),
        "DROP DATABASE '$database'");
    if ($query->execute() == true) {
        //Run Success block
    } else {
        //Run Error block
        print "False, Failed";
    }
}

# Create a table in MySQL
# @Usage create_table("appointments", "advisor VARCHAR(55), time TIME, appointmentMade TIMESTAMP,
# student VARCHAR(55), studentID VARCHAR(9)");
function create_table($tableName, $tableQuery) {
    $query = mysqli_prepare(connection(),
        "CREATE TABLE IF NOT EXISTS '$tableName' ('$tableQuery');");
    if ($query->execute() == true) {
        //Run Success block
    } else {
        //Run Error block
        print "False, Failed";
    }
}

# Delete a table in MySQL
# @Usage delete_table("myTable");
function delete_table($tableName) {
    $query = mysqli_prepare(connection(),
        "DROP TABLE '$tableName';");
    if ($query->execute() == true) {
        //Run Success block
    } else {
        //Run Error block
        print "False, Failed";
    }
}

# Remove a row from a table in MySQL
# @Usage remove_row("myTable", "serverName", "Mineplex");
function remove_row($table, $category, $equalcategory) {
    $query = mysqli_prepare(connection(),
        "DELETE FROM $table WHERE '$category' = '$equalcategory';"
    );

    if ($query->execute() == true) {
        //Run Success block
    } else {
        //Run Error block
    }
}

# Bring back an array of rows from table $table with $numPerPage per page
# @Usage getPage("myTable", 1, 16);
function getPage($table, $page, $numPerPage) {
    $max = $page * $numPerPage;
    $min = ($max - $numPerPage) - 2;
    $query = mysqli_prepare(connection(),
        "SELECT * FROM '$table' LIMIT '$max' OFFSET '$min';"
    );

    $rows = mysqli_fetch_assoc($query->execute());
    $array = [];
    while($min < $max) {
        if(sizeof($rows) >= $min) {
            array_push($array, $rows[$min]);
            $min++;
        }
    }
    return $array;
}

# Run a query to MySQL
# @Usage runQuery("")
function runQuery($query) {
    $query = mysqli_prepare(connection(),
        "'$query'"
    );
    return $query->execute();
}

# Establish MySQL connection()
function connection() {
    $config = parse_ini_file("config.ini", true);

    $connection = mysqli_connect(
        $config['Database']['Host'],
        $config['Database']['User'],
        $config['Database']['Password'],
        $config['Database']['Database'],
        $config['Database']['Port']
    );

    if (mysqli_connect_errno()) {
        die("Connection failed: " . mysqli_connect_error());
    }

    return $connection;
}
 

omarhachach

Your Friendly Neighbourhood Snorlax
Supreme
Feedback score
14
Posts
627
Reactions
359
Resources
0
Updated my code, let me know what ya'll feedback is! Thank you, appreciate any criticism! :)
Code:
<?php

// create_new_row("test", "test", "test");
// remove_row("test");
// getPage(1);

# Create a new row in table $table
# @Usage create_new_row("servers", "serverName, ip, bannerLocation, creator",
# "MinePlex, Mineplex.com, http://website.com/dir/banners/MineplexBanner, Jared"
function create_new_row($table, $tableCategories, $newValues) {
    $query = mysqli_prepare(connection(),
        "INSERT INTO '$table' ('$tableCategories') VALUES ('$newValues');"
    );

    if ($query->execute() == true) {
        //Run Success block
    } else {
        //Run Error block
        print "False, Failed";
    }
}

# Create a database in MySQL
# @Usage create_database("myDatabase");
function create_database($database) {
    $query = mysqli_prepare(connection(),
        "CREATE DATABASE '$database';");
    if ($query->execute() == true) {
        //Run Success block
    } else {
        //Run Error block
        print "False, Failed";
    }
}

# Delete a database in MySQL
# @Usage delete_database("myDatabase");
function delete_database($database) {
    $query = mysqli_prepare(connection(),
        "DROP DATABASE '$database'");
    if ($query->execute() == true) {
        //Run Success block
    } else {
        //Run Error block
        print "False, Failed";
    }
}

# Create a table in MySQL
# @Usage create_table("appointments", "advisor VARCHAR(55), time TIME, appointmentMade TIMESTAMP,
# student VARCHAR(55), studentID VARCHAR(9)");
function create_table($tableName, $tableQuery) {
    $query = mysqli_prepare(connection(),
        "CREATE TABLE IF NOT EXISTS '$tableName' ('$tableQuery');");
    if ($query->execute() == true) {
        //Run Success block
    } else {
        //Run Error block
        print "False, Failed";
    }
}

# Delete a table in MySQL
# @Usage delete_table("myTable");
function delete_table($tableName) {
    $query = mysqli_prepare(connection(),
        "DROP TABLE '$tableName';");
    if ($query->execute() == true) {
        //Run Success block
    } else {
        //Run Error block
        print "False, Failed";
    }
}

# Remove a row from a table in MySQL
# @Usage remove_row("myTable", "serverName", "Mineplex");
function remove_row($table, $category, $equalcategory) {
    $query = mysqli_prepare(connection(),
        "DELETE FROM $table WHERE '$category' = '$equalcategory';"
    );

    if ($query->execute() == true) {
        //Run Success block
    } else {
        //Run Error block
    }
}

# Bring back an array of rows from table $table with $numPerPage per page
# @Usage getPage("myTable", 1, 16);
function getPage($table, $page, $numPerPage) {
    $max = $page * $numPerPage;
    $min = ($max - $numPerPage) - 2;
    $query = mysqli_prepare(connection(),
        "SELECT * FROM '$table' LIMIT '$max' OFFSET '$min';"
    );

    $rows = mysqli_fetch_assoc($query->execute());
    $array = [];
    while($min < $max) {
        if(sizeof($rows) >= $min) {
            array_push($array, $rows[$min]);
            $min++;
        }
    }
    return $array;
}

# Run a query to MySQL
# @Usage runQuery("")
function runQuery($query) {
    $query = mysqli_prepare(connection(),
        "'$query'"
    );
    return $query->execute();
}

# Establish MySQL connection()
function connection() {
    $config = parse_ini_file("config.ini", true);

    $connection = mysqli_connect(
        $config['Database']['Host'],
        $config['Database']['User'],
        $config['Database']['Password'],
        $config['Database']['Database'],
        $config['Database']['Port']
    );

    if (mysqli_connect_errno()) {
        die("Connection failed: " . mysqli_connect_error());
    }

    return $connection;
}
I would say that it is pretty solid code!
Pretty awesome!

--
Best Regards,
Omar H. of NoClue Development.
 
Status
This thread has been locked.
Top