tags:

views:

45

answers:

4

Hi guys, I am trying to get a users ID from a database within a class, but I have very little to no experience with classes, how could I go about getting the uid from the DB and then return the uid?

so basically something like this,

class hello {
   public function getUid(){
      //connect to the db
      //get all of the users info
      $array = mysql_fetch_array($result);
      $uid = $array['uid'];

      return $uid;
   }
}

Like I said, I am still new to classes, so any advice or help would be greatly appreciated!

Thanx in advance!

+1  A: 
  • Create two classes. One for working with database, second one for managing User or Auth data.
  • For SQL class create methods connect(), query(), fetch() etc
  • For User class create methods get($id) etc
dwich
Thank you for helping out!
+1  A: 

Alright, one piece of advice:

Do everything for a reason. Don't use things you don't know. Go and learn them instead.

One may give you an answer for this specific question, but until you don't know what Object Oriented means and why there are classes at all, you shouldn't use them.

galambalazs
Thanx for the advice! At the moment I am learning as I go, which is why I needed some help.
+1  A: 

There is a problem with the way your code is written, not with the class. Take a closer look at this line:

$array = mysql_fetch_array($result);

This is the first time variable $result appears on the function. Therefore, it is not possible to communicate with the database.

A possible pseudo-code would be:

  • connect to the DB server
  • query the database
  • fetch the results
  • return the uid field.

Have a look at the relevant documentation first:

Anax
Thanx, will do!
+1  A: 

first build some mysql class library... suiting your requirment like this sample piece

<?php

include '../config/Dbconfig.php';

class Mysql extends Dbconfig    {

public $connectionString;
public $dataSet;
private $sqlQuery;

    protected $databaseName;
    protected $hostName;
    protected $userName;
    protected $passCode;

function Mysql()    {
    $this -> connectionString = NULL;
    $this -> sqlQuery = NULL;
    $this -> dataSet = NULL;

            $dbPara = new Dbconfig();
            $this -> databaseName = $dbPara -> dbName;
            $this -> hostName = $dbPara -> serverName;
            $this -> userName = $dbPara -> userName;
            $this -> passCode = $dbPara ->passCode;
            $dbPara = NULL;
}

function dbConnect()    {
    $this -> connectionString = mysql_connect($this -> serverName,$this -> userName,$this -> passCode);
    mysql_select_db($this -> databaseName,$this -> connectionString);
    return $this -> connectionString;
}

function dbDisconnect() {
    $this -> connectionString = NULL;
    $this -> sqlQuery = NULL;
    $this -> dataSet = NULL;
            $this -> databaseName = NULL;
            $this -> hostName = NULL;
            $this -> userName = NULL;
            $this -> passCode = NULL;
}

function selectAll($tableName)  {
    $this -> sqlQuery = 'SELECT * FROM '.$this -> databaseName.'.'.$tableName;
    $this -> dataSet = mysql_query($this -> sqlQuery,$this -> connectionString);
            return $this -> dataSet;
}

function selectWhere($tableName,$rowName,$operator,$value,$valueType)   {
    $this -> sqlQuery = 'SELECT * FROM '.$tableName.' WHERE '.$rowName.' '.$operator.' ';
    if($valueType == 'int') {
        $this -> sqlQuery .= $value;
    }
    else if($valueType == 'char')   {
        $this -> sqlQuery .= "'".$value."'";
    }
    $this -> dataSet = mysql_query($this -> sqlQuery,$this -> connectionString);
    $this -> sqlQuery = NULL;
    return $this -> dataSet;
    #return $this -> sqlQuery;
}

function insertInto($tableName,$values) {
    $i = NULL;

    $this -> sqlQuery = 'INSERT INTO '.$tableName.' VALUES (';
    $i = 0;
    while($values[$i]["val"] != NULL && $values[$i]["type"] != NULL)    {
        if($values[$i]["type"] == "char")   {
            $this -> sqlQuery .= "'";
            $this -> sqlQuery .= $values[$i]["val"];
            $this -> sqlQuery .= "'";
        }
        else if($values[$i]["type"] == 'int')   {
            $this -> sqlQuery .= $values[$i]["val"];
        }
        $i++;
        if($values[$i]["val"] != NULL)  {
            $this -> sqlQuery .= ',';
        }
    }
    $this -> sqlQuery .= ')';
            #echo $this -> sqlQuery;
    mysql_query($this -> sqlQuery,$this ->connectionString);
            return $this -> sqlQuery;
    #$this -> sqlQuery = NULL;
}

function selectFreeRun($query)  {
    $this -> dataSet = mysql_query($query,$this -> connectionString);
    return $this -> dataSet;
}

function freeRun($query)    {
    return mysql_query($query,$this -> connectionString);
}

} ?>

and the configuration file...

<?php
class Dbconfig {
    protected $serverName;
    protected $userName;
    protected $passCode;
    protected $dbName;

    function Dbconfig() {
        $this -> serverName = 'localhost';
        $this -> userName = 'root';
        $this -> passCode = 'pass';
        $this -> dbName = 'dbase';
    }
}

?>

Jaison Justus
Thank you so much, really appreciate you taking the time to help out!