![]() |
|
Small SQL data retrieval script. - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: PHP (https://sinister.li/Forum-PHP) +--- Thread: Small SQL data retrieval script. (/Thread-Small-SQL-data-retrieval-script) |
Small SQL data retrieval script. - i0xIllusi0n - 01-30-2013 I know this script may seem useless to some of you, but hey, it helps meout and I thought I'd post it. It's easier because you only need to use a function for getting column data from the database. Plus, if you ever need to edit something there's only 1 place to edit it. PHP Code: $result = getinfo("table", "columntocheck", "dataforcolumntocheck", "getrow");
For example, getting a users ID: PHP Code: $result = getinfo("users", "name", $name, "id");
In config.php: PHP Code: <?php
$dbconnect = new mysqli("host", "username", "password", "database");
include 'functions.php';
?>In functions.php; PHP Code: <?php
function getinfo($from, $where, $input, $getrow)
{
if ($result = $GLOBALS['dbconnect']->query("SELECT * FROM $from WHERE $where='$input'"))
{
while ($row = $result->fetch_assoc())
{
$endresult = $row["$getrow"];
}
if ($endresult != "")
{
return $endresult;
}
else
{
return "$where does not exist!";
}
$endresult->free();
$result->free();
}
}
?>RE: Small SQL data retrieval script. - Kinanizer - 01-30-2013 Thanks for the share, but how would I use it? RE: Small SQL data retrieval script. - i0xIllusi0n - 02-03-2013 (01-30-2013, 01:54 PM)Kinanizer Wrote: Thanks for the share, but how would I use it? Set it up correctly, with your SQL details in config.php. Then, on a page, include config.php which includes functions.php so you'll have the function. PHP Code: $id = getinfo("users", "name", $name, "id");
"users" = the table name "name" = select a column that matches the next variable with this. so since $name is "admin", this says match column where the name is admin $name = "admin" "id" = getting the row, "id" RE: Small SQL data retrieval script. - BreShiE - 02-04-2013 It'd help if you gave out a full explanation of what this actually does. But nice post. RE: Small SQL data retrieval script. - i0xIllusi0n - 02-04-2013 (02-04-2013, 12:29 AM)BreShiE Wrote: It'd help if you gave out a full explanation of what this actually does. But nice post. "Small SQL Data Retrieval Script" "It's easier because you only need to use a function for getting column data from the database." |