The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how to change mysql username via custom php???

Discussion in 'cPanel Developers' started by Dr Ahmed, May 7, 2012.

  1. Dr Ahmed

    Dr Ahmed Member

    Joined:
    Mar 22, 2011
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hello mates,

    I have developped some custom plugin in cPanel, not WHM

    I have created php files and I have some problems the first and the most important one is when I used

    Code:
    
    <cpanel Mysql="adduser( "vb", "$DbPass" )" >
    <cpanel Mysql="adddb(vb)">
    <cpanel Mysql="initcache()" >
    <cpanel Mysql="adduserdb( "vb", "vb", "all" )" >
    
    
    it create them successfully execpt usename password

    I want to use a variable password not static

    in the above statement e.g: if $DbPass = "Scorpion"

    it creates usename_vb with $DbPass not with Scorpion, as I want

    so can any one help me on fixing that problem

    N.B: I used "$DbPass" and $DbPass without double quotes and the same problem

    Thank You in advance
     
  2. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Are these just plain PHP files, live PHP files, cpphp files, phpcp files or some other file extension?
     
  3. Dr Ahmed

    Dr Ahmed Member

    Joined:
    Mar 22, 2011
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    This is Plain php file
     
  4. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Assuming you do not want to use the Live API, which is documented at LiveAPI &mdash; PHP for PHP, you will need to make a decision about which gets executed first, cPanel tags or PHP code.

    If it's PHP code that gets executed first, then you use .phpcp

    If it's cPanel code that gets executed first, then you use .cpphp

    However, the Live API is a much more elegant solution worth investigating since it avoids having to make such choices which lead to inflexibility.
     
  5. Dr Ahmed

    Dr Ahmed Member

    Joined:
    Mar 22, 2011
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Thank You for your great information

    But Unfortuanally, I can't understand well liveAPI so I created the files with my programming knowledge of plain PHP

    My Code represents:
    Code:
    <?php
    function generatepass () {
    Code
    }
    
    $DbPass = generatepass () ;
    <cpanel Mysql="adduser( "vb", "$DbPass" )" >
    <cpanel Mysql="adddb(vb)">
    <cpanel Mysql="initcache()" >
    <cpanel Mysql="adduserdb( "vb", "vb", "all" )" >
    
    Installation Code
    
    ?>
    

    the file is called install.php in /usr/local/cpanel/base/frontend/x3/MyAPP

    so can u explain more your answer in previuos post??
     
    #5 Dr Ahmed, May 8, 2012
    Last edited: May 8, 2012
  6. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    When you write cPanel code and PHP code, one needs to be executed first and the other executed second. The file extensions control that order.

    If you are still confused, please elaborate on the source of that confusion so I can provide clarification.
     
  7. charsleysa

    charsleysa Active Member

    Joined:
    Jul 18, 2011
    Messages:
    41
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Palmerston North, New Zealand
    cPanel Access Level:
    Root Administrator
    Dr Ahmed

    Please note that you cannot execute cPanel code within PHP which is what you are trying to do.
    My recommendation is that you save the PHP file with the .phpcp extension. IE myscript.phpcp

    Then in your code please try something like this:
    Code:
    <?php
    
    function generatepass ()
    {
        // Some code here...
    }
    
    $DbPass = generatepass();
    ?>
    
    <cpanel Mysql="adduser( "vb", "<?php echo $DbPass; ?>" )" >
    <cpanel Mysql="adddb(vb)">
    <cpanel Mysql="initcache()" >
    <cpanel Mysql="adduserdb( "vb", "vb", "all" )" >
    
    <?php
    
    // Continue with your PHP code here...
    
    ?>
    
     
  8. Dr Ahmed

    Dr Ahmed Member

    Joined:
    Mar 22, 2011
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Yes Yes Yes charsleysa
    Your trick is successful and I now created database and its user and assign him successfully

    I have PHP code for importing database but in myscript.phpcp it gives me an error access denied

    PHP Warning: mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Access denied for user 'username_foo'@'localhost' (using password: YES) in import.php

    although I am very sure that username and its password and the database are correct

    and when I ran this import.php separately , it ran successfully

    so can u have any solution for that??
     
  9. Dr Ahmed

    Dr Ahmed Member

    Joined:
    Mar 22, 2011
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    I know now why I can't access Database

    as you inform me thay myscript.phpcp execute firstly all php codes then cpanel codes so integration and importing Database is first before Database and User creation

    so can I find a solution for that?
     
  10. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    The solution to not having to choose between which one executes first is to use the LiveAPI, which lets you call cPanel functions, return the results back into PHP and then call more cPanel functions from PHP variables etc. This lets you continually interact with cPanel&WHM rather than having to do just one thing, then pass the result to a different PHP script.
     
  11. Dr Ahmed

    Dr Ahmed Member

    Joined:
    Mar 22, 2011
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    as in my previous post , I didn't understand well how to use LiveAPI, so I depend on myself in creating my script in plain php
     
  12. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Nowadays, it's just a PHP class so using it is just like using most other contemporary PHP code enclosed in a class. If you are not familiar with classes, the PHP documentation on the subject is generally decent and worth looking over.
     
  13. Dr Ahmed

    Dr Ahmed Member

    Joined:
    Mar 22, 2011
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Thank You Mr.David I appreciate your help

    But before your last reply , I found this code but I can't use it properly for importing Database

    Code:
    <cpanel Fileman="restoredb()">
    I tried use restoredb() as restoredb(username_db.sql.gz) or restoredb("username_db.sql.gz") or restoredb("username_db.sql")

    But unfortunally I failed for that

    Do u have a trick for fix this situation?
     
  14. charsleysa

    charsleysa Active Member

    Joined:
    Jul 18, 2011
    Messages:
    41
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Palmerston North, New Zealand
    cPanel Access Level:
    Root Administrator
    your filename must be prefixed with 'file-' in order for it to work.
    e.g. restoredb("file-username_db.sql.qz")

    I recommend using LiveAPI as it is much easier for what you want to do.
    The following is the same as
    Code:
    <cpanel Mysql="adduser( "vb", "<?php echo $DbPass; ?>" )" >
    <cpanel Mysql="adddb(vb)">
    <cpanel Mysql="initcache()" >
    <cpanel Mysql="adduserdb( "vb", "vb", "all" )" >
    
    but using LiveAPI in PHP instead:
    Code:
    require_once "/usr/local/cpanel/php/cpanel.php";
    $cpanel = new CPANEL();
    $cpanel->api1( 'Mysql', 'adduser', array( 'username' => 'vb', 'password' => "$DbPass") );
    $cpanel->api1( 'Mysql', 'adddb', array( 'dbname' => 'vb') );
    $cpanel->api1( 'Mysql', 'initcache', array() );
    $cpanel->api1( 'Mysql', 'adduserdb', array( 'dbname' => 'vb', 'dbuser' => 'vb', 'perms_list' => 'all') );
    
     
  15. Dr Ahmed

    Dr Ahmed Member

    Joined:
    Mar 22, 2011
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    I am now starting liveapi with your example charsleysa , so Thank you so much for your demonstration but still I can't import Database automatically to the created Database either by LiveApi of from last statement I put

    so can u help me on that?

    I added you on my skype
     
  16. AngelsGrave

    AngelsGrave Member

    Joined:
    May 11, 2012
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hello,

    can anyone please help me with this Project

    Dr. Ahmed ... any clue will be great :)

    Thanks
     
  17. Dr Ahmed

    Dr Ahmed Member

    Joined:
    Mar 22, 2011
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    any new help for importing mysql Database?
     
Loading...

Share This Page