Fantast Posted July 5, 2006 Share Posted July 5, 2006 Hey.Since short im using pear to connect to the MySql databse, simply because i heard it is safer to connect this way. So, being totally new to PEAR, i had two questions:1) is connecting to a MySQL database through pear indeed the safest way to do so?2) i found that "->" is used constantly and throughout the script, as in[code]$this->db->query($sql);[/code]by example. Could anyone please try to explain me how the "->" signs work?many thanks in advance,Fantast~ Quote Link to comment https://forums.phpfreaks.com/topic/13781-the-meaning-of/ Share on other sites More sharing options...
ober Posted July 5, 2006 Share Posted July 5, 2006 1) "safe" is a tentative word at best. Hackers are always more apt to attack products with a bigger user base. Pear might be one of these, but I haven't heard of many people having problems with the Pear package. I personally use my own code to connect to MySQL, but I'm picky about my code and I don't like to use other people's stuff.2) http://www.phpfreaks.com/forums/index.php/topic,95867.0.html Quote Link to comment https://forums.phpfreaks.com/topic/13781-the-meaning-of/#findComment-53546 Share on other sites More sharing options...
Buyocat Posted July 5, 2006 Share Posted July 5, 2006 I think you should use PEAR, just because it is safer and also easier; Ober aside, I don't believe we should all spend vast amounts of energy solving the same problems over and over, now then to answer your question...$db->query ();that would mean that $db is an object and query is a method (function) for that object. The statement would be saying, execute query which belongs to the object db. Objects can also have attributes, say "name". Sometimes you can access the attributes using the -> symbol too. So,echo $db->name;That might print the name of the object. It would be worth reading up on objects if you plan on doing significant programming later on. By the way the variable $this is a special case used in classes. Basically it is referring to itself. So I am not sure where the $this came from, but if you chose to use it I would try another variable name (unless you are creating a class that has the $db as one of its attributes... but then why are you asking about ->?) Quote Link to comment https://forums.phpfreaks.com/topic/13781-the-meaning-of/#findComment-53548 Share on other sites More sharing options...
ober Posted July 5, 2006 Share Posted July 5, 2006 Solving the same problem over and over is one thing. Doing it once myself so I understand exactly what is going and then re-using my own code is another. Being able to modify and change and understand those changes to my own code is [i]priceless[/i]. Quote Link to comment https://forums.phpfreaks.com/topic/13781-the-meaning-of/#findComment-53551 Share on other sites More sharing options...
Fantast Posted July 5, 2006 Author Share Posted July 5, 2006 I see. I will need to do some reading about onjects and attributes indeed. However, I find it more effective to learn by doing than by just reading. So after i modified a piece of code which uses pear for logging in ([url=http://mosaic.stud.tue.nl/]http://mosaic.stud.tue.nl/[/url]), I'd like now to understand exactly how it works. I have done some oop in the past in a few simple java programs, but have never spent enough time in it to learn the structre well enough. But as far as i remember, in Java the "." was used instead of the ->, right?anyway, i was trying to use the pear connection once again for entering the data in the db. I'm not sure yet how $this works in that class, but i was just about to try figuring that out~these are parts of the code which i use for login:[code]class User{ var $db = null; // PEAR::DB pointer var $failed = false; // failed login attempt var $date; // current date GMT var $personalID = 0; // the current user's id function User(&$db) { $this->db = $db; $this->date = $GLOBALS['date']; if ($_SESSION['logged']) { $this->_checkSession(); } elseif ( isset($_COOKIE['mtwebLogin']) ) { $this->_checkRemembered($_COOKIE['mtwebLogin']); } } function _logout() { $_SESSION['logged'] = false; [...] } function _checkLogin($email, $password, $remember) { $email = $this->db->quote($email); $password = $this->db->quote(md5($password)); $sql = "SELECT * FROM login_info WHERE " . "email = $email AND " . "password = $password "; $result = $this->db->getRow($sql); if ( is_object($result) ) { $this->_setSession($result, $remember); return true; } else { $this->failed = true; $this->_logout(); return false; } } function _setSession(&$values, $remember, $init = true) { $this->personalID = $values->personalID; $_SESSION['personalID'] = $this->personalID; [...] if ($remember) { $this->updateCookie($values->cookie, true); } if ($init) { $session = $this->db->quote(session_id()); $ip = $this->db->quote($_SERVER['REMOTE_ADDR']); $sql = "UPDATE login_info SET session = $session, ip = $ip WHERE " . "personalID = $this->personalID"; $this->db->query($sql); } } function updateCookie($cookie, $save) { $_SESSION['cookie'] = $cookie; if ($save) { $cookie = serialize(array($_SESSION['email'], $cookie) ); setcookie('mtwebLogin', $cookie, time() + 31104000, '/directory/'); } } function _checkRemembered($cookie) { list($email, $cookie) = @unserialize($cookie); if (!$email or !$cookie) return; $email = $this->db->quote($email); $cookie = $this->db->quote($cookie); $sql = "SELECT * FROM login_info WHERE " . "(email = $email) AND (cookie = $cookie)"; $result = $this->db->getRow($sql); if (is_object($result) ) { $this->_setSession($result, true); } } function _checkSession() { $email = $this->db->quote($_SESSION['email']); $cookie = $this->db->quote($_SESSION['cookie']); $session = $this->db->quote(session_id()); $ip = $this->db->quote($_SERVER['REMOTE_ADDR']); $sql = "SELECT * FROM login_info WHERE " . "(email = $email) AND (cookie = $cookie) AND " . "(session = $session) AND (ip = $ip)"; $result = $this->db->getRow($sql); if (is_object($result) ) { $this->_setSession($result, false, false); } else { $this->_logout(); } }}$date = gmdate("'Y-m-d'");$db = db_connect();$user = new User($db);$user->_checkLogin($_POST['email'], $_POST['password'], $_POST['remember']);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13781-the-meaning-of/#findComment-53589 Share on other sites More sharing options...
Buyocat Posted July 5, 2006 Share Posted July 5, 2006 So, what exactly is your question? The $this variable just refers to the object itself. So if you're working with a method like _save () it would be something like this...[code]public function _save ($data){// data is a parameter that is passed to the method$_query = // make some SQL query maybe using data$this->db->query($_query); // ok so I'm using this, it is basically saying "Use the object's "db" // attribute to do the following... in this case queryreturn $something; // that exits the method and returns the value something for use}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13781-the-meaning-of/#findComment-53600 Share on other sites More sharing options...
.josh Posted July 6, 2006 Share Posted July 6, 2006 [quote author=ober link=topic=99570.msg392158#msg392158 date=1152130029]Solving the same problem over and over is one thing. Doing it once myself so I understand exactly what is going and then re-using my own code is another. Being able to modify and change and understand those changes to my own code is priceless.[/quote]:thumbs up: Quote Link to comment https://forums.phpfreaks.com/topic/13781-the-meaning-of/#findComment-53699 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.