Jump to content

Cine

Members
  • Posts

    16
  • Joined

  • Last visited

    Never

Posts posted by Cine

  1. Show us some code and I'll take a look for you.

     

    I currently have this: Date("Y-m-d", $this->dob);

     

    ...which outputs 1970-01-01 and is stored in the database.

     

    I need a way of formatting that custom date string ($this->dob) into a format that MySQL can understand which is(Y-m-d).

     

    But I don't know how :(

  2. Hi there,

     

    I have a string '12/04/1990', that's in the format dd/mm/yyyy.

     

    I'm attempting to convert that string to a Date, and then insert that date into a MySQL DATE field.

     

    The problem is, every time I try to do so, I keep getting values like this in the database: 1970-01-01.

     

     

    Any ideas?

     

     

    Much appreciated.

     

     

  3. The getItems() method would be more appropriate in the ItemsAbstraction class. You could have it return an array of objects using mysql_fetch_object() for example.

     

    Unless you need methods from the Item class there would be no need to create an Item object for every record in a list, especially if the stdClass returned by fetch_object() has the same properties as your Item class.

     

    When working with a single item in the application, it may be more suitable to instantiate your Item class for it.

     

    Ah I didn't think about the mysql_fetch_object() method.

     

    It would suit what I'm trying to achieve perfectly.

     

     

    Thanks.

     

  4. In my opinion I would determine the solution based upon the following question: Are there additional methods you want to execute against the results?

     

    If yes, then creating new objects from the results would make sense. If no, then I see no reason to create new objects out of the results.

     

     

    My thinking entirely.

     

    Thanks.

  5. this really boils down to personal preference, both methods wil work, for its simplicity I myself would go with number 2 simply because the task that you want to execute is not dificult and there is no need to over work things..a single method with passable arguments should be all that you need to query a database and ouput results.

     

    Hi, appreciate your reply.

     

    The reason why I suggested using #1, was that there'll be quite a few 'get...' methods and other database interoperability methods in the 'ItemAbstraction' class and the advantage of them all calling one method in the 'Item' class, is that they can share a single persistent database connection, instead of establishing a new one for each query; which is what #2 would require.

     

    Thanks.

  6. Hi,

     

    I need some advice on how I should go about fetching data from a database, taking advantage of OOP principles.

     

    I have a table named 'items' in a Database.

     

    I've created a an 'Item' class, that holds all my instance variables for a specific item. These instance variables, can be directly associated with the fields I have in the table.

     

    I also have an 'ItemAbstraction' class, which is responsible for 'getting' items given an Item ID or name for example.

     

    My question is, which of the two methods below would work best?

     

    1) Have a method in the 'ItemAbstraction' class that calls a 'getItems' method in the 'Item' class which queries the database and returns an associative array of items. Then, as I'm iterating through this array... create a 'Item' object for each returned item row, and set the instance variables in that object accordingly. I'll then store this object in my own subject array, which can be returned.

     

    2) To simply have a method that returns the associative array of items returned by a successful database query?

     

    3) Any other ways?

     

     

    Many thanks for the replies in advance.

     

     

  7. Im thinking it HAS to be something to do with how I'm using save_results() and use_result().

     

    But after scouring through the PHP docs, I've yet to come across anything that alludes to this.

     

     

    EDIT: Yeah, the store_result() function is returning false, which means an error occurred. Which explains why use_result() is returning an error.

     

    Now to find why it's failing?

  8. You said you didn't want to use a Database for this... is there any particular reason why? A Database would be much easier though, SQL queries make it simple to search through data sets will would be ideal for what you're looking to do.

     

    If you're set on not using a Database to hold the information on each Monster... you could use an XML file instead.

     

    Then use SimpleXML or another parser to extract the monster information.

     

     

  9. Hi there,

     

    I have a Database Abstraction class for MySQL.

     

    I'm trying to use the MySQLi::store_result() to save a result set returned by a successful SELECT query (MySQLi::query()).

     

    As soon as that is done (Or at any time), I call the member function getData() which I hoped would return an associative array of the stored SELECT data. However, I'm not sure if I'm doing this correctly at all.

     

    The code at present, is returning a Fatal error:

     

    Fatal error: Call to a member function fetch_assoc() on a non-objectFatal error: Call to a member function fetch_assoc() on a non-object

     

    The $data variable is being passed the result of calling use_result(), which is supposed to be an unbuffered result object.  Currently however, it's returning False as something erroneous occurred. But I have no idea what.

     

    I've posted an extract of the class below.

     

    Thanks in advance.

     

        public function select($table, $fields, $condition = false) {
    
            if($this->isConnected()) {
    
                if(isset($table, $fields)) {
    
                    $SQL = "SELECT ";
    
                    foreach($fields as $column) {
                        $SQL .= "`$column`";
                        $SQL .= ($column != end($fields)) ? ", " : " ";
                    }
    
                    $SQL .= "FROM $table ";             
                    $SQL .= ($condition) ? "WHERE $condition" : "";
    
                    $result = $this->query($SQL);
                    
    
                    if($result->num_rows > 0) {
                        $this->DB->store_result();
                        return $this->getData();
                    }
    
                    return 0;
                     
    
                } else {
                    echo self::ERR_SELECT_SYNTAX;
                    return false;
                }
            
            }
        }
    
    
        public function getData() {
            $data = $this->DB->use_result();
            return $data->fetch_assoc();
        }
    

  10. Hello,

     

    Here's my system. Once a user is successfully logged in, a new instance of a User class is created. The constructor of this class grabs the details of the logged-in user from a database and stores them inside properties in the User class.

     

    The problem is, obviously I'd want to access these properties from any page I require them to be able to display user data, so I looked into storing the User object in a Session.

     

    When I tried implementing this, I ran into a bunch of errors and I couldn't figure it out.

     

    Here's an example:

     

    After a user has logged in, I had the following code:

    $_SESSION['user'] = new User($this->username);

     

    I was under the impression that this assigns a user object to a session. But it's not working as I receive this error:

     

    Notice: Undefined index: user in ../v2/admin/index.php on line 18

     

     

    Then on the page I want to display the name of the current user logged-in, I had this code:

    $_SESSION['user']->get_Name();

     

    But then I get this error:

    Fatal error: Call to a member function get_IP() on a non-object in ../v2/admin/index.php on line 18

     

     

    Can tell me what I have to do, to make this work?

     

     

    Thanks.

     

  11. Hello,

     

    I'm getting some errors when binding parameters inside a loop, and it's doing my head in :)

    
    public function query($sql, $params) {
           
    	if(!$sobj = $this->conn->prepare($sql)) {
                   
    		die('Query Prepare Error (' . $this->conn->mysqli_errno . ') '
    			. $this->conn->mysqli_error);
                   
    	} else {
                   
    		if(!is_array($params)) {
                           
    			$params = array_slice(func_get_args(), 1);
    
    		}
    
    		foreach($params as $value) {
                           
    			$type = strtolower(gettype($value));
    			$sobj->bind_param($type[0], $this->conn->real_escape_string($value));
                           
    		}                      
                   
    		$sobj->execute();
    		$sobj->bind_result($result);
    		$sobj->fetch();
                   
    		$sobj->close();
    
    		return $result;
                   
    	}
    
    }
    
    
    

     

    The errors I'm getting is:

     

    Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statement in /nfs/c06/h02/mnt/97387/domains/nickythorne.com/html/v2/includes/classes/class_db2.php on line 67
    
    Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables doesn't match number of parameters in prepared statement in /nfs/c06/h02/mnt/97387/domains/nickythorne.com/html/v2/includes/classes/class_db2.php on line 67

     

    (Line 67 is $sobj->bind_param($type[0], $this->conn->real_escape_string($value)); above)

     

    I'm calling the function like this:

    $details = new DB();
    echo $details->query("SELECT `postcode` FROM `details` WHERE `name` = ?  and `address` = ?", array("Josh", "45 Ashdale Road"));

     

     

    Does anyone know how I can solve this?

     

     

    Thanks.

     

     

  12. Replace:

    <a href="#" style="" class="" title=""><img src="button_add_to_cart.gif" width="72px" height="17px" /></a>

     

    With:

    <a href="#" style="" class="" title="" OnClick="alert('Call this number to order');"><img src="button_add_to_cart.gif" width="72px" height="17px" /></a>

     

    Hope that helps.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.