SitePoint Sponsor

User Tag List

Results 1 to 20 of 20

Thread: functions

  1. #1
    SitePoint Enthusiast Superteddy's Avatar
    Join Date
    May 2005
    Location
    Trondheim
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    functions

    I have thins function im trying to make but im not sure im doing it right.

    PHP Code:
      <?php

    function raidName()
    {
        global 
    $db;
        
        
    $select "SELECT raidName FROM raid WHERE raidName = $raidName";
        
    $result $db->sql_query("$select");
        
    $result $db->sql_fetchrow();
        
        return 
    $result[0];
    }

    ?>
    and then im trying to get it to work by doing

    PHP Code:
                    <h1><?php $raidName;?></h1>
    i have been trying to work this out for over 2hours now but since im 100% new to php im lost here. If anyone could explain what im doing wrong it would be nice.


    Im not sure, but im guessing that //global $db; // is calling the database. i already get that from the header witch gets it from the sessions file. Remove ing the global $db; dosnt do anything at all so i dont think it has to stay there but i keept it there.

    /cheers

  2. #2
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
     echo raidName(); 

  3. #3
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if u dont know whts global $db is doing in ur code then u r really lost... http://www.samspublishing.com/articl...&seqNum=7&rl=1

    $db needs to defined some where to get access in function else u need to pass the values to the function to do the connection....
    i think $db needs to be an object

    wht kinda err u r getting

  4. #4
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    whts the o/p for following
    PHP Code:
    <?php

    function raidName()
    {
        global 
    $db;
        
        
    $select "SELECT raidName FROM raid WHERE raidName = $raidName";
        
    $result $db->sql_query("$select");
        
    $result $db->sql_fetchrow();
        
        echo 
    $result[0];
        
    }

    raidName();

    ?>

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $raidName is a variable. A variable is like a container, which holds a value.
    raidName() is a function. When a function is called, it returns a value, which can be assigned to a variable. When you call functions, you can also pass variables to the function, but this is optional.
    echo is a statement, which works a bit like a function. When you call echo, it will output the values of a variable. For example, look at the following:
    PHP Code:
    $some_variable "Hello, World";
    echo 
    $some_variable
    $some_variable is a variable, which is first assigned the value of "Hello, World". In the following line, the variable is passed to echo, which outputs the value of the variable.
    Functions are called by writing their name. Before they can be called, they must be declared. Have a look at this:
    PHP Code:
    function someFunction() {
        return 
    "Hello again";

    This declares a function, someFunction. Declaring the function doesn't do anything in itself. You have to call the function, to actually do anything:
    PHP Code:
    $some_variable someFunction();
    echo 
    $some_variable
    In this example, you call the function previously declared. The function returns the value "Hello again", which is then assigned to the variable $some_variable. You can then call echo as before, passing the variable to it.

    Hope that clears up a few things.

  6. #6
    SitePoint Enthusiast Superteddy's Avatar
    Join Date
    May 2005
    Location
    Trondheim
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the help guys but im still stuck.

    PHP Code:
    <?

      
    include("include/header.php");
     
    function 
    raidName() 
    {
        global 
    $MySQLDB;
        
        
    $select "SELECT 'raidName' FROM 'raid'";
        
    $result $db->sql_query("$select");
        
    $request_data $db->sql_fetchrowset();
        
    $num_requests $db->sql_numrows();
        
        return 
    $result[0];
    }

    ?>
     <div id="primary" class="single-post">
            <div class="inside">
                
                <div class="primary">
                    <h1><?php echo raidName(); ?></h1>
    Im getting a error in line 17 ( $result = $db->sql_query("$select"); )

    Fatal error: Call to a member function sql_query() on a non-object in \view.php on line 17

    Im guessing that it is because of the $db and that it should be an object but im not sure what i would put there.

    the function is from the old version of the tool im trying to make a new from. so im not the owner and im just learning php/mysql ( nice way to learn btw )

  7. #7
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're right - the problem is that $db is undefined. Judging from the syntax, it's supposed to be an object. If you stick the line global $db back in, I would guess that it'll work. What this line does is to make the global variable $db available to the function. Without that, the variable doesn't exist inside the function.

    To illustrate how global variables works, you can try the following two examples:
    PHP Code:
    <?php
    $foo 
    "Hello, World";
    function 
    bar() {
        echo 
    $foo;
    }
    bar();
    ?>
    PHP Code:
    <?php
    $foo 
    "Hello, World";
    function 
    bar() {
        global 
    $foo;
        echo 
    $foo;
    }
    bar();
    ?>

  8. #8
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    where is $db is defined ? its an object....
    where is sql_query function is defined , its not a complete code to solve ur err.
    is all thses things are in header.php then please post ur code of header.php here.

  9. #9
    SitePoint Enthusiast Superteddy's Avatar
    Join Date
    May 2005
    Location
    Trondheim
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    view.php

    PHP Code:
    <?

      
    include("include/header.php");

    function 
    raidName()
    {
        global 
    $MySQLDB;
        
        
    $select "SELECT 'raidName' FROM 'raid'";
        
    $result $db->sql_query("$select");
        
    $request_data $db->sql_fetchrowset();
        
    $num_requests $db->sql_numrows();
        
        return 
    $result[0];
    }

    ?>
    <div id="primary" class="single-post">
            <div class="inside">
                
                <div class="primary">
                    <h1><?php echo raidName(); ?></h1>
    database.php

    PHP Code:
          
    class MySQLDB
    {
       var 
    $connection;         //The MySQL database connection
       
    var $num_active_users;   //Number of active users viewing site
       
    var $num_active_guests;  //Number of active guests viewing site
       
    var $num_members;        //Number of signed-up users
       /* Note: call getNumMembers() to access $num_members! */

       /* Class constructor */
       
    function MySQLDB(){
          
    /* Make connection to database */
          
    $this->connection mysql_connect(DB_SERVERDB_USERDB_PASS) or die(mysql_error());
          
    mysql_select_db(DB_NAME$this->connection) or die(mysql_error()); 
    called like this : header -> sessions -> database

  10. #10
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sounds good but still few things are missing
    $db
    and method sql_query()

  11. #11
    SitePoint Enthusiast Superteddy's Avatar
    Join Date
    May 2005
    Location
    Trondheim
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    there is no $db, its called $MySQLDB

    and method sql_query() - seems like this is what i dont understand.

    no matter what i try i always get an error to this line : $result = $db->sql_query("$select");

  12. #12
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    look into ur code
    PHP Code:
    $request_data $db->sql_fetchrowset(); 
    here $db it is an object which is u r using in ur function if it is $mysqldb then replace $db vth that

    Method sql_query() -- its a calss method which u r calling so it needs to be defined somewhere in ur calss.

  13. #13
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no $MySQLDB. There's a MySQLDB, which is a class. A class is not a variable.

    Your database.php file is missing something. I guess that there is a line somewhere, perhaps near the end, which spells:
    PHP Code:
    $db = new MySQLDB(); 

  14. #14
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $db = new classname() ;

    class 
    classname 
    {
    var .....

       function 
    sql_query ()
       {
            
    some code here
       
    }

    the code ur posting if u ve only that part then u r not going 2 pass this err, never

  15. #15
    SitePoint Enthusiast Superteddy's Avatar
    Join Date
    May 2005
    Location
    Trondheim
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    There is no $MySQLDB. There's a MySQLDB, which is a class. A class is not a variable.

    Your database.php file is missing something. I guess that there is a line somewhere, perhaps near the end, which spells:
    PHP Code:
    $db = new MySQLDB(); 

    yes there way *blush*,

    PHP Code:
    $database = new MySQLDB(); 
    getting
    Fatal error: Call to undefined method MySQLDB::sql_query() in view.php on line 10 still
    PHP Code:
    $result $database->sql_query("$select"); 

  16. #16
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use $database instead of $db and locate the function sql_query

  17. #17
    SitePoint Enthusiast Superteddy's Avatar
    Join Date
    May 2005
    Location
    Trondheim
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?

      
    include("include/header.php");

    function 
    raidName()
    {
        global 
    $database;
        
        
    $select "SELECT 'raidName' FROM 'raid'";
        
    $result mysql_query($select$this->connection);
        
    $request_data $MySQLDB>sql_fetchrowset();
        
    $num_requests = $->sql_numrows();
        
        return 
    $result[0];
    }

    ?>
    <div id="primary" class="single-post">
            <div class="inside">
                
                <div class="primary">
                    <h1><?php echo raidName(); ?></h1>
    now im getting
    Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting T_VARIABLE or '$' in view.php on line 12

    line 12:
    PHP Code:
    $num_requests = $->sql_numrows(); 
    Last edited by Superteddy; Jan 11, 2007 at 17:16. Reason: fixed the line 196 error

  18. #18
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no u r doing some mistakes
    http://us2.php.net/zend-engine-2.php
    PHP Code:
    global $database;
        
           
    $request_data $MySQLDB>sql_fetchrowset(); 
    whatever u r define obejct use that name
    $MySQLDB whts this ????????

  19. #19
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why dont u post ur whole code or just have a look at this http://us2.php.net/zend-engine-2.php u 'll be able to find and track the problem

  20. #20
    SitePoint Wizard simplecode's Avatar
    Join Date
    Jun 2006
    Posts
    1,129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •