Jump to content

mds1256

Members
  • Posts

    259
  • Joined

  • Last visited

Everything posted by mds1256

  1. The below scenario works fine but why shouldnt you do this? Why can I not just run the mysql_connect and mysql_select_db at the top of each page and then run my queries under all of this. What I have read is that the mysql_connection will die after the script finishes (at the end of the page) any way so why do people create mysql_connection objects as php is stateless so it doesnt save this any way. <?php mysql_connect('localhost', 'user', 'pass'); mysql_select_db('test'); ?> <html> <body> <?php $query = mysql_query("Select * from data"); while($row = mysql_fetch_array($query)) { echo $row['name']."<br />"; } ?> -------------- OTHER HTML HERE --------------- <?php $query = mysql_query("Select * from addresses"); while($row = mysql_fetch_array($query)) { echo $row['postcode']."<br />"; } ?> </body> </html>
  2. Yes. Make the connection first and pass it into your object upon instantiation. Your querying for two different users so not really. As I said, a "Person" represents a single "person". The latter is correct, I have forgot I am working with objects woops... Do you have example of passing a connection object into this object. Also can you use array attributes on methods eg.: $person = getName()['firstname']
  3. Hi, thanks for the reply. I am using singleton way so it doesnt create a new database connection / object each time i need to query the database? Is there a better way of achieving this. In the end I would like a database class that has a query method that I can use for each time I need to pull back results. so for example I need the query method to be dynamic and allow a return of an array, I then need to know the best way of allowing me to dynamically handling this array (maybe with another database method). I can do the following but if you look at the index page you will see that I need to store the returned array in a variable first then echo the variable with the array element. Looks a bit messy, is there a way to do this in a one liner? Database class: <?php class Database { private static $dbInstance; private $hostname; private $username; private $password; private $database; private function __construct() { $this->hostname = 'localhost'; $this->username = 'user'; $this->password = 'pass'; $this->database = 'test'; mysql_connect($this->hostname, $this->username, $this->password); mysql_select_db($this->database); } public static function getDBInstance() { if (!self::$dbInstance) { self::$dbInstance = new Database(); } return self::$dbInstance; } public function query($q) { return mysql_query($q); } } ?> person class: <?php require_once('Database.php'); class person { public function getName($id) { $con = Database::getDBInstance(); $query = $con->query("select name from data where id = ".$id); return mysql_fetch_assoc($query); } } ?> index page: <?php require_once('person.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <?php $person = new person(); $name = $person->getName(1); echo $name['name']; echo "<br />"; $name = $person->getName(2); echo $name['name']; ?> </body> </html>
  4. Hi I have created a Database class that uses the singleton method. I have a query method within this class so i would call this to return an array of results. What i need to know is what would be the best way to use this returned array Database Class: <?php class Database { private static $dbInstance; private $hostname; private $username; private $password; private $database; private function __construct() { $this->hostname = 'localhost'; $this->username = 'user'; $this->password = 'pass'; $this->database = 'test'; mysql_connect($this->hostname, $this->username, $this->password); mysql_select_db($this->database); } public static function getDBInstance() { if (!self::$dbInstance) { self::$dbInstance = new Database(); } return self::$dbInstance; } public function query($q) { return mysql_query($q); } } ?> Person Class: <?php require_once('Database.php'); class person { public function getName($id) { $con = Database::getDBInstance(); $query = $con->query("select name from data where id = ".$id); $result = mysql_fetch_assoc($query); echo $result['name']; } } ?> Index page: <?php require_once('person.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <?php $person = new person(); $person->getName(1); echo "<br />"; $person->getName(2); ?> </body> </html>
  5. What is the best way to backup a MySQL database which has 24 hours access to clients. With ms SQL I have setup maintenance plans that do a full backup every night and creates logs every hour. This would allow me to restore database Up to a certain point and could even look at the logs and use them to rebuilt to that point in time if I needed to. I need to do this on a MySQL server and I know you can use mysqldump but how can I get it to backup nightly and create logs ever hour? Any good software for setting these up, on a windows environment.
  6. Thanks for the above that is exactly what I thought but needed someone to confirm this. Thanks again
  7. Thanks, so is this secure in that no one can just tamper with anything to add a session variable of logged_in with a value of 1? Also how do you then retrieve data about a user from the database it will need some type of reference number, should that be stored in the session too?
  8. Hi What is the best way of handling a login system with sessions, I have read that you should never hold the password in a session, so what should you hold in the session in order to access a users data?
×
×
  • 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.