So i've recently been starting to program PHP in an object oriented way, but I'm running into some difficulties in from a design stand point and from an object oriented stand point:
Issue 1: In my class I cannot give visibility to any variable, it simple breaks everything. For example if I do:
- private $private = 'Private';
I get the error :
Quote:
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /hermes/bosweb/web085/b852/sl.proto7/public_html/rcanime/newsClass.php on line #
The error is the same if I use public or protected. I have tried declaring a a private variable in the main body of my class, in the constructor and in a method all to the same result. Is there something I'm overlooking here about PHP and visibility?
Issue 2: I have my news class which loads various items from a sql table entry such as story title, story, date, comments etc. It make sense to me to make 1 query to the database, get the sql result array and then use the field I need from it from subsequent methods: loadStory(), loadTitle() etc, but when I try to put the sql commands to run one query from the database in my constructor, I simply get no data from the sql result. Once again is there something I'm overlooking about the way PHP / MySQL works?
Below is the code I currently have which is failing:
-
-
<?php
-
-
class News
-
{
-
public $private = 'Private';
-
-
function News()
-
{
-
-
include('connect.php');
-
}
-
-
-
function loadImage()
-
{
-
$query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
-
$result = mysql_query($query) or die(mysql_error());
-
$image = mysql_result($result,0,"avatar");
-
echo $image;
-
-
-
}
-
-
function loadTitle()
-
{
-
$query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
-
$result = mysql_query($query) or die(mysql_error());
-
$title = mysql_result($result,0,"title");
-
echo $title;
-
}
-
-
function loadStory()
-
{
-
$query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
-
$result = mysql_query($query) or die(mysql_error());
-
$story = mysql_result($result,0,"post");
-
echo $story;
-
}
-
-
function loadComments()
-
{
-
-
}
-
-
function loadDate()
-
{
-
$query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'dot' USING latin1 ) ";
-
$result = mysql_query($query) or die(mysql_error());
-
$date = mysql_result($result,0,"date");
-
echo $date;
-
}
-
-
}
-
-
?>
-
-
This is what I was hoping to do, since it makes sense to me from a design stand point to just do the work once instead of over and over again.:
- <?php
-
-
class News
-
{
-
public $private = 'Private';
-
-
function News()
-
{
-
-
include('connect.php');
-
$query = "SELECT * FROM `news` WHERE `user` LIKE CONVERT( _utf8 'my_user' USING latin1 ) ";
-
$result = mysql_query($query) or die(mysql_error());
-
}
-
-
-
function loadImage()
-
{
-
-
$image = mysql_result($result,0,"avatar");
-
echo $image;
-
-
-
}
-
-
function loadTitle()
-
{
-
$title = mysql_result($result,0,"title");
-
echo $title;
-
}
-
-
function loadStory()
-
{
-
$story = mysql_result($result,0,"post");
-
echo $story;
-
}
-
-
function loadComments()
-
{
-
-
}
-
-
function loadDate()
-
{
-
$date = mysql_result($result,0,"date");
-
echo $date;
-
}
-
-
}
-
-
?>
Any assistance is much appreciated!