Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12

Thread: script help

  1. #1
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts

    script help

    PHP Code:
    <?php
    include('configed.php');

    class 
    user
    {
        function 
    warnUser($user,$wpts){
            
    $this->user $user;
            
    $this->wp $wpts;
            
            
    $link = @mysql_connect($host,$user,$pass) or die("Could not connect to database. Please try again later!");
            @
    mysql_select_db($db,$link) or die("Could not select database. Please try again or try again later.");
            
    $sql_insert "UPDATE `users` SET `wpts`='{$this->wp}' WHERE `user`='{$this->user}'";
            
    mysql_query($sql_insert,$link);
            
    $success "Successful";
            return 
    $success;
        }
    }

    $exe = new user();
    $exe->warnUser('SDP2006','2');
    ?>
    My error is
    Code:
    Could not select database. Please try again or try again later.
    I don't know why. btw here is configed.php
    PHP Code:
    <?php
    $host 
    "localhost"
    $dbase "testing";
    $user "root";
    $pass "rootpass";
    ?>
    My database information is correct, but It must be something else. Thanks for any help.
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    a) your class can not 'see' your connection variables as they are outside of its scope
    b) $db should be $dbase

    either import using global ...


    function warnUser($user,$wpts){
    global $host,$dbase,$user,$pass;

    or directly

    function warnUser($user,$wpts){
    include('configed.php');


    (both of which are bad ideas though they work)

    of globalise your variables in the first place ...

    <?php
    $GLOBALS['db']['host'] = "localhost";
    $GLOBALS['db']['dbase'] = "testing";
    $GLOBALS['db']['user'] = "root";
    $GLOBALS['db']['pass'] = "rootpass";
    ?>


    & then
    $link = @mysql_connect($GLOBALS['db']['host'] , ... etc

    ..........................................................................

    BUT , doing your db connection in each and every class method is not a good plan , perhaps ..

    PHP Code:
    <?php
    include('configed.php');

    class 
    user{
        var 
    $db ;//db connection

        
    function user$db ){
            
    //store the db connection//
            
    $this->db=$db;
        }

        function 
    warnUser($user,$wpts){
            
    $this->user $user;
            
    $this->wp $wpts;
            
             
    $sql_insert "UPDATE `users` SET `wpts`='{$this->wp}' WHERE `user`='{$this->user}'";
            
    mysql_query$sql_insert $this->db ) ;
            
    $success "Successful";
            return 
    $success;
        }
    }


     
    $link = @mysql_connect$host $user $pass ) or die("Could not connect to database. Please try again later!");
            @
    mysql_select_db$dbase $link ) or die("Could not select database. Please try again or try again later.");

    $exe = new user$link ) ;
    $exe->warnUser('SDP2006','2') ;

    //see the advantage ? you can reuse that link anywhere//
    $other = new other_class$link );
    ?>
    or a vriation on such , thats just one idea , there are many other ways , but the general idea is not to have database connections happening left right and center.

    ................................................................................

    A handy note .. if you are only accessing one database host (and mostly that will be the case) , you dont even need to pass all the database connections around , once php has made a MySQL connection , it will reuse that connection wherever it can , unless you close it its always available...

    PHP Code:
    <?
    mysql_connect
    ($host,$user,$pass);
    mysql_select_db($dbase);

    class 
    whatever{
        function 
    whatever(){
            
    //this query will work asis//
            
    echo 'resource id ...' mysql_query("SELECT * FROM table");
        }
    }

    whatever::whatever();
    ?>

    there are many good reasons for not having ANY sql calls within the classes themselves and abstracting the database work elsewhere , but thats another story.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, my script works now.
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #4
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    One more question. How do I change my MySQL Query to where it adds to the current total in the field. Like if there were 2 warning points and you wanted to add 2 more that would give a total of 4. How might I do that?

    Thanks
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    UPDATE `users` SET `wpts`= `wpts` + '{$this->wp}' WHERE `user`='{$this->user}'
    "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

    (Me Me Me Me Me Me Me Me Me)

  • #6
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, ReadMe.

    One last problem.
    PHP Code:
    <?php
    include('configed.php');

    class 
    user
    {
        var 
    $db ;

    function 
    user$db ){
            
    $this->db=$db;
        }

        function 
    warnUser($user,$wpts)
        {
        
    $this->user $user;
        
    $this->wp $wpts;
            
        
    $sql_insert "UPDATE `users` SET `wpts`=`wpts` + '{$this->wp}' WHERE `user`='{$this->user}'";
        
    mysql_query$sql_insert $this->db ) ;
        
    $success "{$this->user} recieved {$this->wp} warning points!";
        print 
    $success;
        }
        function 
    printPts($user)
        {
        
    $this->user $user;
        
    $sql_select "SELECT user,wpts FROM users WHERE `user`='{$this->user}'";
        
    mysql_query($sql_select$this->db);
        
    $sql mysql_fetch_array($sql_select);
        
    $this->user $sql[user];
        
    $this->wpts $sql[wpts];
        
    $final "{$this->user} has {$this->wpts} warning points!";
        print 
    $final;
        }
    }
    $link = @mysql_connect($host$user$pass) or die("Could not connect to database. Please try again later!");
          @
    mysql_select_db($dbase$link) or die("Could not select database. Please try again or try again later.");

    $exe = new user($link);
    $exe->warnUser('SDP2006','2');
    $exe->printPts('SDP2006');

    ?>
    Fuction warnUser() is okay, but I don't believe function printPts is. My errors are
    Code:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\server\htdocs\warn.php on line 27
    
    Notice: Use of undefined constant user - assumed 'user' in c:\server\htdocs\warn.php on line 28
    
    Notice: Use of undefined constant wpts - assumed 'wpts' in c:\server\htdocs\warn.php on line 29
    Anyone see what is wrong? Thanks alot.

    - Stevie
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #7
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, I got rid of all errors so
    PHP Code:
    <?php
    include('configed.php');

    class 
    user
    {
        var 
    $db ;

    function 
    user$db ){
            
    $this->db=$db;
        }

        function 
    warnUser($user,$wpts)
        {
        
    $this->user $user;
        
    $this->wp $wpts;
            
        
    $sql_insert "UPDATE `users` SET `wpts`=`wpts` + '{$this->wp}' WHERE `user`='{$this->user}'";
        
    mysql_query$sql_insert $this->db ) ;
        
    $success "{$this->user} recieved {$this->wp} warning points!<br />";
        print 
    $success;
        }
        function 
    printPts($user)
        {
        
    $sql_select "SELECT `user`,`wpts` FROM `users` WHERE `user`='$user'";
        
    $result  mysql_query($sql_select$this->db);
        
    $sqlmysql_fetch_array($result);
        
    $user $sql['user'];
        
    $wpts$sql['wpts'];
        
    $final "$user has $wpts warning points!";
        print 
    $final;
        }
    }
    $link = @mysql_connect($host$user$pass) or die("Could not connect to database. Please try again later!");
          @
    mysql_select_db($dbase$link) or die("Could not select database. Please try again or try again later.");

    $exe = new user($link);
    $exe->warnUser('SDP2006','2');
    $exe->printPts('SDP2006');

    ?>
    My problem now is in function printPts all that is printed is 'has warning points!' I dunno why. All help is welcome....

    -Thanks
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Firepages :
    A handy note .. if you are only accessing one database host (and mostly that will be the case) , you dont even need to pass all the database connections around , once php has made a MySQL connection , it will reuse that connection wherever it can , unless you close it its always available...
    I think that if you use mysql_connect(), that the connection is then automatically closed when the script is processed.

    You need to use mysql_pconnect() if you want persistent connections and optimize your serverperformance with connectionpooling.

    Note : using persistent connections will result in always having a number of 'sleeping connection' and requires more RAM.

    SDP2006 : you should be extremely carefull when you use variables to store your accountdetails in like
    <?php
    $host = "localhost";
    $dbase = "testing";
    $user = "root";
    $pass = "rootpass";
    ?>
    Always set these variables explicitely to '' as soon as you no longer need them or unset() then !!

  • #9
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, so I changed it to mysq_pconnect() and I'm still having the problem above ^.

    Thanks for all the help. Is is greatly appreciated.
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Have you made sure the SQL query works as expected? Try these steps:

    1.) add "if (!$result) print mysql_error();" to see any potential MySQL error messages.
    2.) print out the SQL query used
    3.) run the query (as it was printed out!) in phpMyAdmin and check that it returns the expected result sets.
    De gustibus non est disputandum.

  • #11
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    Originally posted by raf
    I think that if you use mysql_connect(), that the connection is then automatically closed when the script is processed.
    yes , so in the scope of a script (eg page) unless you explicity close and reopen your connections the connection is not 'lost' until the script finishes.

    eg one connection in 'config.inc' is valid thoughout the lifetime of the script that called it.

    when you use mysql_pconnect the connection stays (or attempts to) stay in memory across requests hence the overhead an dis mostly best avoided unless you have a `difficult` connection or say a seperate data-tier.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #12
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by mordred
    Have you made sure the SQL query works as expected? Try these steps:

    1.) add "if (!$result) print mysql_error();" to see any potential MySQL error messages.
    2.) print out the SQL query used
    3.) run the query (as it was printed out!) in phpMyAdmin and check that it returns the expected result sets.
    Yes, I ran the code in PHPMyAdmin and it returned results as I expected.

    I found the problem, it was my query. Not the query, but the WHERE user='{this->user}'. I had a user that didn't exist in the $user in the function.

    Thanks for the help guys and the notes on mysql_pconnect.

    Thanks
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!


  •  

    Posting Permissions

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