someone tell me where am going wrong
<?php
class myclass
{
var $config;

function getname()
{
$q=mysql_query("select * from users");
while($row=mysql_fetch_array($q))
{
$row[]=$minutes;
}
return $minutes;
}
..........................................
hers how i call
<?php
$name=new myclass;
$new->getname();

foreach($new as $news)
{echo $news;
}

Recommended Answers

All 3 Replies

Hi,
The problem is that you never define additional properties for your database connection..

Here is a simple example AND SHOULD NOT be used in production site. I am just trying to give you a simple example. You still need to do some dirty work and lots of reading about the visibility, methods, and I always recommend to practice with the singleton pattern, and PDO..

Example One - The beginner's class. This has no properties and constructor. As simple as it is, the functions are just wrapped inside the curly brackets of the class. If you are not sure on how the things work, I highly recommend to take baby steps just like the example codes below.

## I am always criticize by many because of my class names are always in lowerCase, but I am not going to do it this time.

class Myclass{
## notice first curly?
## we can just dump all the functions we want here
## please observe which are global and which are not

function lets_Connect($host,$db_name,$db_user,$db_password){
## this is where you want to make your script connect to the database

 // right here

## You can send the query based on the connection above. Don't forget to define table of interest, where the data are coming
$db_query = "define your database query Here";

## here you can extract the output as an array. Normally, people would use while loop

## echo output here or assign them to some variable and just use return $output

## we're done? close the curly bracket for the function
}
## close curly bracket for the class
}

## Lets instantiate ...no no.. lets call the class it would be a lot easier to remember.

$name= new Myclass;
$show_Something = $name->lets_Connect("localhost","my_DatabaseName","my_DatabaseUserName","my_DatabasePassword");
## echo the output. Depending on what you used inside the function lets_Connect
## if you used the echo inside the function, then the above should print out somehting on the screen.
## if you used the return $output or $something, then you must echo $show_something
echo $show_Something;

Later on, I will write an example class with all the trimmings in it. Assuming that I don't get lazy today..

ok, I was looking at my previous post on this forum and I just realized I posted something that can be adjusted or modified to your needs..

Example TWO - This example is one notch above the example One above. WARNING! this utilizes PDO.. I am pretty sure most servers support this.

First, create a file settings.php

<?php
$db_host = "localhost";
$db_database = "YourDB_Name";
$db_user = "YourDB_User";
$db_password = "YourDB_PassWord";

?>

then the result.php

<?php
class Video{
## warning this $title refers to the database table column "title"
public $title;
## same as above
public $remotethumb;
## same as the two variables above
public $tags;
## assign description
public $description;
## attempt to print or echo which I always prefered
## of course we can do anything with the variables as declared above.
public function echo_something(){
## make the title red
$red ="";
$red .='<b style="color:red;">'.$this->title.'</b><br/>';
$red .='<b style="color:navy;">'.$this->description.'</b><br/>';
## if we want to add more values from the query, we can always do so by extending $red variables.
## in fact, we can even use this class to ouput a thumbnail base on the url of the thumbnails from the media table..
$red .= '<img src="'.$this->remotethumb.'"/><br/>';
 return $red;
}

} 
## settings.php is a file above where the database user, password, and db name is located.
## they are defined as follows; $db_host = "localhost";$db_user = "username"; $db_password = "db password";$db_database = "dbName";
include_once 'settings.php';

try {
    $someDb = new PDO("mysql:host=$db_host;dbname=$db_database", $db_user, $db_password);
   ## double check and make sure we are connected. If we are, tell Morpheous and Neo about it, well yah.
   ## remove this in production site. we don't want the agents to know we know Morpheus and Neo :).
    echo 'Yes, we are connected Morpheous and Neo!<br />';

   ## define our connection query
   ## don't forget to change media to your database table.
   $query = "SELECT * FROM media WHERE category='music' ";

    ## prepare to fetch PDOStatement objects 
    $pdo_Object = $someDb->query($query);
## remove count below if not needed
    //$count = $pdo_Object->rowCount();
    //echo $count."<br/>";	
    ## Now, fetch that class video above. using the FETCH_CLASS pdo object.
    $obj = $pdo_Object->fetchALL(PDO::FETCH_CLASS, 'Video');

  ## Unlike my first not so good example this will only take place once, and not twice.
    foreach($obj as $video)
        {
     ## remember the echo_something in our class video above? This is where we can call it.
        echo $video->echo_something().'<br />';
        } 
    ## I think it will be worth it if we close whatever we opened to complete our operation. Codes below can do just that.
    $someDb = null;
}
## We can either eliminate these remaining two lines below if needed be, but it is highly necessary when testing your class or the script.
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

Consaider there are two clases,
one class to connect to database..that is very well working.

the second class i nid to put all mysql statements..communicating with database.

Pliz help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.