Gazan Posted February 3, 2009 Share Posted February 3, 2009 Hey there. I can't make my script work.. I'm using the db class for connecting, and the query class for doing the sql queries. but when i output the code below, it returns: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\test.php on line 20 However, if i do the sql queries without using classes it works perfect, of course. Heres my source (Notice: It has nothing to do with the missing password in the connect, 'cause i don't use pass for my offline server..): <?php class db { function db_connect() { mysql_connect("localhost", "root"); mysql_select_db("underskrift"); } } class query { function sql_query($sql) { global $db; $db->db_connect(); mysql_query($sql) or die(mysql_error()); } function sql_row($result) { mysql_fetch_array($result); } } $db = new db; $query = new query; $sql = 'SELECT * FROM underskrifter'; $result = $query->sql_query($sql); while($row = $query->sql_row($result)) { echo $row['underskrift_navn']; } ?> Quote Link to comment Share on other sites More sharing options...
gevans Posted February 3, 2009 Share Posted February 3, 2009 Even though you don't use a password you should still leave it in blank; mysql_connect("localhost", "root", ""); try this see what happens; <?php class db { function db_connect() { mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("underskrift") or die(mysql_erro()); } } class query { function sql_query($sql, $db) { $db->db_connect(); mysql_query($sql) or die(mysql_error()); } function sql_row($result) { mysql_fetch_array($result); } } $db = new db; $query = new query; $sql = 'SELECT * FROM underskrifter'; $result = $query->sql_query($sql, $db); while($row = $query->sql_row($result)) { echo $row['underskrift_navn']; } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted February 3, 2009 Share Posted February 3, 2009 You never call the db_connect() method of the $db object. Seriously however, why are these speperate objects? And the use of globals within objects kinda defeats the entire purpose. Quote Link to comment Share on other sites More sharing options...
Gazan Posted February 3, 2009 Author Share Posted February 3, 2009 Nope.. Still not working. The code you posted just returns the same error, just line 19 instead of 20. EDIT: I know, this was just made pretty quickly, and im new to OOP. I tried this out instead, but didn't work either. <?php class query { function sql_connect() { mysql_connect("localhost", "root", ""); mysql_select_db("underskrift"); } function sql_query($sql) { $this->sql_connect(); mysql_query($sql) or die(mysql_error()); } function sql_row($result) { mysql_fetch_array($result); } } $query = new query; $sql = 'SELECT * FROM underskrifter'; $result = $query->sql_query($sql); while($row = $query->sql_row($result)) { echo $row['underskrift_navn']; } ?> Quote Link to comment Share on other sites More sharing options...
Mikedean Posted February 3, 2009 Share Posted February 3, 2009 I agree with thorpe, however: class query { function sql_query($sql) { global $db; $db->db_connect(); $query = mysql_query($sql) or die(mysql_error()); return $query; } function sql_row($result) { return mysql_fetch_array($result); } } Works. You need to return the results from the functions. Quote Link to comment Share on other sites More sharing options...
gevans Posted February 3, 2009 Share Posted February 3, 2009 how about this..... <?php class db { function db_connect() { mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("underskrift") or die(mysql_erro()); } } class query { function sql_query($sql, $db) { $db->db_connect(); return mysql_query($sql) or die(mysql_error()); } function sql_row($result) { return mysql_fetch_array($result); } } $db = new db; $query = new query; $sql = 'SELECT * FROM underskrifter'; $result = $query->sql_query($sql, $db); while($row = $query->sql_row($result)) { echo $row['underskrift_navn']; } ?> @ thorpe.. he does connect inside the second class @ Gazan... Thorpe is right, that's a bad way of doing this. The whole thing should be one class, and using globals is not a good way to deal with state EDIT: what Mikedean said! Quote Link to comment Share on other sites More sharing options...
Gazan Posted February 3, 2009 Author Share Posted February 3, 2009 Hmm. All your examples just keeps giving me the same error.. EDIT: Thanks everyone! got it to work. Especially thanks Mikedean, second try on your example did it! And yeah, - i'll put the connection inside the query class. - Thanks for the tip Quote Link to comment 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.