Thomas Verhoeven
Thomas Verhoeven

Reputation: 286

Change code to link mySQL into a function

I'm new to php. I have this piece of code:

<?php 
if (!isset($_GET['id'])) {
    die("missing query parameter");
}
$id = intval($_GET['id']);
if ($id === '') {
    die("Invalid query parameter");
}

$db = mysql_connect("localhost", "root", "usbw");
$sdb = mysql_select_db("test", $db);
$sql = "SELECT * FROM config WHERE id=$id";
$mq = mysql_query($sql) or die("not working query");
$row = mysql_fetch_array($mq);
?>

And from this code, I want to make a function, but how? What I'm trying to do is linking my MySQL database to my PHP code, and I also try to use the GET[id] to auto change my page if a change my id.

This piece of code does work, but I want to change it into a function. But I don't know how to start.

Upvotes: 0

Views: 60

Answers (2)

Kostas Mitsarakis
Kostas Mitsarakis

Reputation: 4747

You can make a file named db.php including some common db functions so you will be able to use them easier:

<?php
function db_connection() {
    //SERVER, USER, MY_PASSWORD, MY_DATABASE are constants
    $connection = mysqli_connect(SERVER, USER, MY_PASSWORD, MY_DATABASE);
    mysqli_set_charset($connection, 'utf8');
    if (!$connection) {
        die("Database connection failed: " . mysqli_error());
    }
    return $connection;
}

function db_selection() {
    $db_select = mysqli_select_db(MY_DATABASE, db_connection());
    if (!$db_select) {
        die("Database selection failed: " . mysqli_error());
    }
    return $db_select;
}

function confirm_query($connection, $result_set) {
    if (!$result_set) {
        die("Database error: " . mysqli_error($connection));
    }
}

function q($connection, $query) {
    $result = mysqli_query($connection, $query);
    confirm_query($connection, $result);
    return $result;
}
?>

Then, you can have your code in some other files:

<?php
require_once('db.php'); //This file is required

$id = $_GET['id']; //Shorthand the $_GET['id'] variable
if (!isset($id)) {
    die("missing query parameter");
}
if ( filter_var($id, FILTER_VALIDATE_INT) === false) ) {
    die("Invalid query parameter");
}
$sql = "SELECT * FROM config WHERE id = '$id'";
$result = q($connection, $sql);
while ($row = mysqli_fetch_array($result)) {
    //Do something
}
?>

Try not to use mysql_* functions because they are deprecated. Use mysqli_* or even better try to learn about prepared statements or PDO.

Upvotes: 0

Alex Andrei
Alex Andrei

Reputation: 7283

Here's an example of a function around your query, mind you it's just an example, as there are many improvements to make.
Such as not using the mysql_* api and moving towards PDO or mysqli_*

But it should be enough to get you started

<?php

// your if logic stays unchanged

$db=mysql_connect("localhost","root","usbw");
$sdb=mysql_select_db("test",$db);


function getConfig($id,$db){

    $sql="SELECT * FROM config WHERE id=$id";
    $mq=mysql_query($sql);

    if ($mq) {
        return mysql_fetch_array($mq);
    }
    else {
        return false;
    }

}

$results = getConfig($id,$db);

if ($results==false) {
    print "the query failed";
}
else var_dump($results);

Upvotes: 1

Related Questions