Jump to content

Recommended Posts

<?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();
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/47896-why-doesnt-this-work/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/47896-why-doesnt-this-work/#findComment-234106
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/47896-why-doesnt-this-work/#findComment-234130
Share on other sites

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.  ;D

 

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.

Link to comment
https://forums.phpfreaks.com/topic/47896-why-doesnt-this-work/#findComment-234328
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.