langenf Posted September 9, 2008 Share Posted September 9, 2008 Hi, I try to make a class that could be used and reused to perform database actions. I dont know why this error occurs Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in W:\www\liz\DataConnection.php on line 35 im trying to modify and to understand this code i took from another topic in this forum.. class DataConnection{ var $hostname; var $username; var $password; var $database; var $datalink; function makeConnection($var1, $var2, $var3, $var4) { $this->hostname = $var1; $this->username = $var2; $this->password = $var3; $this->database = $var4; $this->datalink = mysql_pconnect($this->hostname,$this->username,$this->password); if(!$this->datalink){ return 0; } elseif(!mysql_select_db($this->database,$this->datalink)){ return 0; } return $this->datalink; } } function getDatalink() { $conn = new dataConnection(); if($_SERVER['WINDIR']){ return $conn->makeConnection("localhost", "testuser", "testpass", "testdb"); } if(!$_SERVER['WINDIR']){ return $conn->makeConnection("localhost", "testuser", "testpass", "testdb"); } } getDatalink() ; $query_myResult = "SELECT COUNT(*) as count FROM testtable where id=1"; $myResults = mysql_query($query_myResult, $datalink) or die(mysql_error()); $myResult = mysql_fetch_object($myResults); $totalRows_myResult = $myResult->count; please help Quote Link to comment https://forums.phpfreaks.com/topic/123427-trying-to-build-a-simple-class-to-do-the-databse-work/ Share on other sites More sharing options...
KevinM1 Posted September 9, 2008 Share Posted September 9, 2008 I believe your problem is... getDatalink() ; // <-- right here Your getDatalink function returns a database connection. So, you need to assign a variable to it to capture that information, like so: $myDBC = getDatalink(); //DBC = database connection Other than that, I strongly suggest updating to PHP 5 (latest version is 5.2.6, I believe). Its OOP syntax is much clearer, IMO, and, on the whole, its OOP capabilities are much more powerful than what PHP 4 offers. Quote Link to comment https://forums.phpfreaks.com/topic/123427-trying-to-build-a-simple-class-to-do-the-databse-work/#findComment-637485 Share on other sites More sharing options...
langenf Posted September 9, 2008 Author Share Posted September 9, 2008 thanks for the reply. I use php 5.2.3 as the code i posted before is a bit hard for me to understand, i found another one that is understandable and works. how can i add query function inside the class?. can you please give suggestions to improve this class? class MySQL { private $host; private $user; private $pass; private $dbName; private $dbc; private $dbSelected; public function __construct($host, $user, $pass, $dbName) { $this->host = $host; $this->user = $user; $this->pass = $pass; $this->dbName = $dbName; $this->dbConnect(); } private function dbConnect() { $this->dbc = @mysql_connect($this->host, $this->user, $this->pass); if(!$this->dbc) { echo "Cannot connect to the DB!<br />"; } else { $this->dbSelected = @mysql_select_db($this->dbName, $this->dbc); if(!$this->dbSelected) { echo "Cannot select the correct DB!<br />"; } } } public function isError() { if(!($this->dbc && $this->dbSelected)) { return true; } else { return false; } } public function query($query) { if(!$this->isError()) { $result = mysql_query($query, $this->dbc); return $result; } else { echo "Cannot return records from database due to connection failure.<br />"; } } } $myDB = new MySQL("localhost", "testuser", "testpass", "testdb"); $query = "SELECT * FROM testtable "; $result = $myDB->query($query); if($result) { while($row = mysql_fetch_assoc($result)) { echo "{$row['name']} {$row['petname']}<br />"; } } else { die("norecords from database due to connection failure.<br />"); } /*sql create database testdb; grant select,insert,update,delete on testdb.* to 'testdb'@'localhost' identified by 'testuser'; use testdb; CREATE TABLE testtable ( id INTEGER NOT NULL AUTO_INCREMENT, name VARCHAR(12) NULL , petname VARCHAR(12) NULL, PRIMARY KEY(id) ) ; insert into testtable(name,petname) values('john','petti'); insert into testtable(name,petname) values('ingram','julpets'); */ Quote Link to comment https://forums.phpfreaks.com/topic/123427-trying-to-build-a-simple-class-to-do-the-databse-work/#findComment-637498 Share on other sites More sharing options...
KevinM1 Posted September 9, 2008 Share Posted September 9, 2008 thanks for the reply. I use php 5.2.3 as the code i posted before is a bit hard for me to understand, i found another one that is understandable and works. how can i add query function inside the class?. can you please give suggestions to improve this class? Well, this class already has a query method: class MySQL { . . . public function query($query) { if(!$this->isError()) { $result = mysql_query($query, $this->dbc); return $result; } else { echo "Cannot return records from database due to connection failure.<br />"; } } } I think this class, in and of itself, is pretty adequate for getting the job done, so long as you're certain that you'll be dealing with a MySQL database. If you're unsure of what kind of DB you'll be working with, you should employ the Factory Method pattern to return the right kind of DB object based on the actual DB you're working with. In other words, you should have an abstract DB class that has a function (either its constructor or a static function) that analyzes the connection string passed into it and returns a concrete, subclass object of the particular DB type. So, if the function gets passed a MySQL connection string, it returns a MySQL DB object. If it gets passed a SQLite connection string, it returns a SQLite DB object. You should also be weary of how errors are handled here. Is it enough to merely spit something like "Could not connect, try again later" to the screen? At the very least, you should log any fatal errors like those automatically. Write to a file, send an e-mail to you with the error message...something. Just some ideas. Again, if you're certain you're going to be sticking with MySQL, then don't sweat the stuff I mentioned about the Factory Method. Quote Link to comment https://forums.phpfreaks.com/topic/123427-trying-to-build-a-simple-class-to-do-the-databse-work/#findComment-637513 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.