ballhogjoni Posted July 15, 2008 Share Posted July 15, 2008 is there a way to turn a mysql result array into an object? IE. $result = mysql_query(some query); $row = mysql_fetch_array($result); $obj = object($row); // just guessing on this Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/ Share on other sites More sharing options...
trq Posted July 15, 2008 Share Posted July 15, 2008 Use mysql_fetch_object() instead. Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-590063 Share on other sites More sharing options...
ballhogjoni Posted July 15, 2008 Author Share Posted July 15, 2008 Thanks, thorpe. I now get this warning when I execute the script: Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource mysql_select_db($this->db_name, $conn) or die(mysql_error()); $results = mysql_query($sql, $conn) or die(mysql_error()); $rows = mysql_fetch_object($results); Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-590064 Share on other sites More sharing options...
.josh Posted July 15, 2008 Share Posted July 15, 2008 where is $conn being assigned? where is $sql being assigned? Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-590072 Share on other sites More sharing options...
ballhogjoni Posted July 15, 2008 Author Share Posted July 15, 2008 $sql comes from this function: function _checkLogin($username, $password, $remember) { $db = new db(); $sql = "SELECT * FROM Member WHERE username = '$username' AND password = '$password'"; $result = $db->dbQuery($sql); if (is_object($result)) { $this->_setSession($result, $remember); echo '1'; return true; } else { $this->failed = true; $this->_logout(); echo '2'; return false; } } $conn comes from the db_connect() below: <?php class db { var $db_host = 'localhost'; var $db_user = 'root'; var $db_pass = ''; var $db_name = 'realfina_allcbreviews'; /* connects to the db */ function db_connect() { $conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass) or die(mysql_error()); return $conn; } /* This gets the info */ function dbQuery($sql) { $conn = $this->db_connect(); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db($this->db_name, $conn) or die(mysql_error()); $results = mysql_query($sql, $conn) or die(mysql_error()); $rows = mysql_fetch_object($results); return $rows; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-590077 Share on other sites More sharing options...
.josh Posted July 15, 2008 Share Posted July 15, 2008 $sql is a local variable inside that function. It doesn't exist outside that function unless you specify it as a global variable or else return it from whence the function was called (which I'm not seeing in your previous code calling that function in the first place). edit: or rather what I mean to say is, I don't really see a connection between $sql in that function and where you are trying to use it. I mean, I see you trying to pass it as an argument to the function but I don't see it being returned or set as a global or anything in the function where it's set in the first place, so I don't really see how you are getting it from point A to point C from the presumably point B of calling the function. Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-590078 Share on other sites More sharing options...
ballhogjoni Posted July 15, 2008 Author Share Posted July 15, 2008 That being said would you declare $sql as a global variable like this: /* This gets the info */ function dbQuery($sql) { $conn = $this->db_connect(); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db($this->db_name, $conn) or die(mysql_error()); $results = mysql_query($sql, $conn) or die(mysql_error()); $rows = mysql_fetch_object($results); return global $rows; } or like this: var global $sql; function db_connect() { $conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass) or die(mysql_error()); return $conn; } /* This gets the info */ function dbQuery($sql) { $conn = $this->db_connect(); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db($this->db_name, $conn) or die(mysql_error()); $results = mysql_query($sql, $conn) or die(mysql_error()); $rows = mysql_fetch_object($results); return $rows; } Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-590723 Share on other sites More sharing options...
trq Posted July 15, 2008 Share Posted July 15, 2008 No. Besides, according to your code $sql is pased to your functions as an argument, there is no point in attempting to declare it as a global. Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-590764 Share on other sites More sharing options...
discomatt Posted July 15, 2008 Share Posted July 15, 2008 ignore this Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-590783 Share on other sites More sharing options...
ballhogjoni Posted July 15, 2008 Author Share Posted July 15, 2008 No. Besides, according to your code $sql is pased to your functions as an argument, there is no point in attempting to declare it as a global. Thorpe if this is the case what did I not understand from Crayon Violent's response. I thought he said that I should declare $sql as global. Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-590853 Share on other sites More sharing options...
.josh Posted July 15, 2008 Share Posted July 15, 2008 Well I said that was an option for getting your var from point A to point C I didn't say it was the best option to go with. It was an example to illustrate that you are assigning $sql at point A and trying to use it at point C. Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-590878 Share on other sites More sharing options...
trq Posted July 15, 2008 Share Posted July 15, 2008 These snippets of code seam to be completely unrelated to each other, and your original question and follow-up. If your having understanding variable scope, read here, otherwise, in relation to.... Thanks, thorpe. I now get this warning when I execute the script: Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource mysql_select_db($this->db_name, $conn) or die(mysql_error()); $results = mysql_query($sql, $conn) or die(mysql_error()); $rows = mysql_fetch_object($results); Is that the relevent code relating to said error? And if so (assuming that is part of the dbQuery() method) can wee see where you call dbQuery()? Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-591014 Share on other sites More sharing options...
ballhogjoni Posted July 16, 2008 Author Share Posted July 16, 2008 Hey guys thanks. Thorpe yes that the code that is related to the error, I figured out my problem, I was declaring the $db object as a null variable earlier in my script and that fixed that issue. The other problem is that I can't get the dbQuery() method to return the results as an object. Basically it seems like its not returning any info. I am calling the dbQuery() method from the User class and the dbQuery() is located in the db class. I am of course including the file that holds the db class in the file that holds the User class. Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-591205 Share on other sites More sharing options...
trq Posted July 16, 2008 Share Posted July 16, 2008 Basically it seems like its not returning any info Well, you never check. Also, even if it did return a result it would only ever return the first one. If you are expecting more than one record you would need to call mysql_fetch_object() again. I'm not sure where you want to go from here so I'll just leave you to ponder for a bit. Are you expecting more than one result? If so, do you want dbQuery() to return an array of row objects? Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-591214 Share on other sites More sharing options...
ballhogjoni Posted July 16, 2008 Author Share Posted July 16, 2008 Actually I check using print_r() I just didn't add it in the code snippet. When I do, nothing is printed to the screen. Also I am expecting just one result. do you want dbQuery() to return an array of row objects? The answer is no, but it would be good to know how to do for the future. Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-591248 Share on other sites More sharing options...
trq Posted July 16, 2008 Share Posted July 16, 2008 If your only ever expecting one result, then you could simply use.... function dbQuery($sql) { $conn = $this->db_connect(); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db($this->db_name, $conn) or die(mysql_error()); if ($result = mysql_query($sql, $conn)) { if (mysql_num_rows($result)) { return mysql_fetch_object($result); } } return false; } For multiple results, you would be best to store the result within a property within the same class (lets call it result), you would then create another method to access this property. function dbQuery($sql) { $conn = $this->db_connect(); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db($this->db_name, $conn) or die(mysql_error()); if ($result = mysql_query($sql, $conn)) { if (mysql_num_rows($result)) { $this->result = $result; } } return false; } function dbResult() { return $this->result; } Then, to use this class would be something like.... $db = new db; if ($db->dbQuery("SELECT foo FROM bar")) { while ($row = $db->dbResult()) { echo $row->foo; } } Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-591265 Share on other sites More sharing options...
ballhogjoni Posted July 16, 2008 Author Share Posted July 16, 2008 Thanks!! Your the best Quote Link to comment https://forums.phpfreaks.com/topic/114755-solved-is-there-a-way-to-turn-a-mysql/#findComment-591520 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.