-1

How would I create (for a mySQL database) a PHP function that has two parameters: the first being a column name in the table and the second being a username? The function should return the table's value in that column for the given username.

Essentially, I need something like the below code (which is for a SQLite Database and Java), but in PHP.

public String getInformation(String information, String username) {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cur = db.rawQuery("SELECT " + information + " FROM registeruser WHERE username=" + "'" + username + "'",null);
        String result = "";
        if (cur.moveToFirst()) {
            int resultColumn = cur.getColumnIndex(information);
            result = cur.getString(resultColumn);
        }
        return result;
    }
  • 1
    this isn't a PHP code, where's the PHP where you tried – Kevin May 28 '20 at 03:45
  • Yes, I said in the question that it's not PHP. That's an example of what I need, but in PHP. I tried to make method similar to the following, but I don't know how to take parameter and use that. – Anonymous Cardinal May 28 '20 at 03:50
  • private function getUsersPasswordByUsername($username){ $stmt = $this->con->prepare("SELECT password FROM registeruser WHERE username = ?"); $stmt->bind_param("s", $username); $stmt->execute(); $stmt->bind_result($password); $stmt->fetch(); return $password; } – Anonymous Cardinal May 28 '20 at 03:50
  • Never supply relevant details as comments, please remove your comment and edit your question. – mickmackusa May 28 '20 at 04:02

2 Answers2

0

I will convert your code to PHP:

Steps:

Connect to your database.

Prepare your statement by using prepare method.

Bind parameters to your prepared statement $stmt.

Execute your statement, get the resultset, extract the value and then return it.

<?php
function getInformation($field, $value)
{
    $conn = new mysqli("localhost", "user", "password", "database");
    $query ="select $field from registeruser where username=?";

    $stmt = $conn->prepare($query);
    $stmt->bind_param("s",$value);
    $stmt->execute();
    $res = $stmt->get_result();
    $row = $res->fetch_assoc();

     return $row[$field];
}

?>
Hardood
  • 503
  • 1
  • 5
  • 15
  • Code-only answers are low-value on Stack Overflow. All answers should contain a plain English explanation of what the answer does and how it works with the intent to educate and empower the OP and thousands of future researchers. I make a habit of never, ever upvoting code-only answers -- even if they are correct. – mickmackusa May 28 '20 at 03:59
-1

Connect to the database:

$link = mysqli_connect("yourip", "dbusername", "dbpassword", "dbname");
//check connection
if($link === false){
    die("ERROR: Could not connect. ".mysqli_connect_error());
}

Create the request:

$sql = "SELECT $columnsyouwant FROM $tablename WHERE username=$username";
$result = $link->query($sql);

Go through each result and do whatever you need:

if($result->num_rows>0){
    while($row = $result->fetch_assoc()){
        x=$row["columnname"] //or do whatever you need with this value
    }
}
Lewis H
  • 13
  • 3