syad Posted April 20, 2007 Share Posted April 20, 2007 <?php /* File name is rss.php */ echo "<html>\n<head>\n</head>\n<body>\n"; require_once('rss_classes.php'); //require_once('rss_settings.php'); $my_user = new user(1); echo "<p>" . $my_user->user_name . "</p>"; echo "</body>\n"; echo "</html>"; ?> <?php /* rss_classes.php and is found in same directory I don't understand why this doesn't work and there is blood spurting from my head from banging it against the keyboard */ class dbConnection{ var $message; dbConnecton(){ $db = "my_table"; $host = "my_host"; $user = "user_name"; $pass = "password"; mysql_connect($host, $user, $pass) or die("Unable to connect to host $host"); mysql_select_db($db) or die( "Unable to select database $db"); } dbResult($query){ $result = mysql_query ($query) or die (mysql_error()); return $result; } dbClose(){ mysql_close(); } } class user{ function user($id){ $db = new dbConnection(); $result = mysql_fetch_array ($db->dbResult("Select * from users where user_id = '$id' ")); $this->user_name = $result[1]; $this->user_pass = $result[2]; $this->user_link = $result[3]; $this->user_email = $result[4]; $db->dbclose(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/47896-why-doesnt-this-work/ Share on other sites More sharing options...
MadTechie Posted April 20, 2007 Share Posted April 20, 2007 Please put the code in the code tags explain the problem including errors and for gods sake read the rules.. Quote Link to comment https://forums.phpfreaks.com/topic/47896-why-doesnt-this-work/#findComment-234061 Share on other sites More sharing options...
redbullmarky Posted April 20, 2007 Share Posted April 20, 2007 first off, your 'methods' in your dbConnection class dont have the prepended 'function': <?php class dbConnection{ var $message; function dbConnecton(){ $db = "my_table"; $host = "my_host"; $user = "user_name"; $pass = "password"; mysql_connect($host, $user, $pass) or die("Unable to connect to host $host"); mysql_select_db($db) or die( "Unable to select database $db"); } function dbResult($query){ $result = mysql_query ($query) or die (mysql_error()); return $result; } function dbClose(){ mysql_close(); } } ?> but as MadTechie states - explain the problem - don't just post code and assume we'll dig through the whole thing looking for bugs and errors for you. Give us all the information YOU have... "It doesn't work" is not information Quote Link to comment https://forums.phpfreaks.com/topic/47896-why-doesnt-this-work/#findComment-234106 Share on other sites More sharing options...
syad Posted April 20, 2007 Author Share Posted April 20, 2007 Ok. Sorry. I am a NOOBIE. I thought the code was short enough for someone to take a glance at it and see the problem right off. I am new to php programming but want to create some objects to pass back to templates. I want to create a separate class for each mysql table that I have so that every time I need to get some information I just create an instance of the class and have the info in an object. The problem is that I am not that familiar with php classes and there aren't many examples of what I want to do that I could find (maybe I am not looking in all the right places.) I thought if I could get input from those who are more experienced than myself would help me immensely. My intent with the dbConnection class was to be able to create a db connection simply by calling the constructor. With the user class, I wanted to be able to create a object with all the user information just by calling the constructor of that class. I also am going to hopefully create a blog class that I can pass a user id and get all the blogs for that user loaded into an object. My problem is that I can't see where I am doing wrong in trying to create an instance of the dbConnection within the user class construct. please help. I want to simply call the user class in the rss.php file and immediately have all the info. Quote Link to comment https://forums.phpfreaks.com/topic/47896-why-doesnt-this-work/#findComment-234130 Share on other sites More sharing options...
syad Posted April 20, 2007 Author Share Posted April 20, 2007 ps. I will try to keep my posts short and to the point from now on. Quote Link to comment https://forums.phpfreaks.com/topic/47896-why-doesnt-this-work/#findComment-234151 Share on other sites More sharing options...
redbullmarky Posted April 20, 2007 Share Posted April 20, 2007 ok - so did you or did you not correct the points i pointed out to you? and are you still getting errors? and you've still not said what errors are being displayed... Quote Link to comment https://forums.phpfreaks.com/topic/47896-why-doesnt-this-work/#findComment-234255 Share on other sites More sharing options...
syad Posted April 20, 2007 Author Share Posted April 20, 2007 Yes I did correct the errors. Thanks. It worked. I appreciate it. I worked on it for a while and didn't notice something as simple as that. I am using yahoo and there were no errors displayed. I was trying to get them to show up but they never did. That's why I couldn't find the error. Quote Link to comment https://forums.phpfreaks.com/topic/47896-why-doesnt-this-work/#findComment-234328 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.