We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17233
    • 15 Posts
    Hallo again

    I am trying to list out products, and want to make sure, that if database or table not exists, there will be no long error list printet out.

    This should be so easy, but I guess not in modx

    can any help me

    I connect to a seperate mysql database

    here is the code and a list of some codelines I tryed


    code in snippet

    // snippets parameters
    $dbname = isset ($dbname )?$dbname :$_GET[’dbname ’]; //database name
    $username = isset ($username )?$username :$_GET[’username ’];
    $password = isset ($password )?$password :$_GET[’password ’];
    $tblname = isset ($tblname )?$tblname :$_GET[’tblname ’];//tablename
    $tablestyleclass = isset ($tablestyleclass)?$tablestyleclass:$_GET[’tablestyleclass’];//table class
    $tbodystyleclass = isset ($tbodystyleclass)?$tbodystyleclass:$_GET[’tbodystyleclass’];//tbody class
    $headcolumn_1 = isset ($headcolumn_1)? $headcolumn_1:$_GET[’headcolumn_1’];//tablehead column 1
    $headcolumn_2 = isset ($headcolumn_2)? $headcolumn_2:$_GET[’headcolumn_2’];//tablehead column 2
    $headcolumn_3 = isset ($headcolumn_3)? $headcolumn_3:$_GET[’headcolumn_3’];//tablehead column 3

    $output = ’’;
    // check if database exists
    //connect to seperate mysql db

    if (!@mysql_select_db(’$dbname.$tblname’)){
    //if (!@$modx->db->connect(’localhost’, ’$dbname’, ’$username’, ’$password’, true)){

    echo " No list a the moment !";
    }
    else{

    //$ds = $modx->db->select(’*’,’$dbname.$tblname’);
    $ds = mysql_select_db(’$dbname.$tblname’);
    $resultArray = $modx->db->makeArray($ds);

    foreach($resultArray as $item) {
    $params[’value1’]= $item[’b’];
    $params[’value2’]= $item[’d’];

    $output .= $modx->parseChunk(’xls_list_of_products_tpl’, $params, ’[+’, ’+]’);
    //$output .= $params;
    }
    return $output;
    }




    List of some of the codes I tryed and didn’t work

    if (!(@mysql_select_db(`$dbname.$tblname`))){
    echo " No list a the moment !";
    }



    if (!($modx->db->select(`$dbname.$tblname`))){
    echo " No list a the moment !";
    }


    if(mysql_connect(’localhost’,’$dbname’,’$pass’)){
    echo " No list a the moment !";
    }



    if (!($modx->db->connect(`$dbname.$tblname`))){ ------------- `
    echo " No list a the moment !";
    }

    if (!($modx->db->connect(’$dbname.$tblname’))){ ---------------`to ’
    echo " No list a the moment !";
    }


    if (!($modx->db->connect($dbname.$tblname))){
    echo " No list a the moment !";
    }


    if (!(@$modx->db->connect(’127.0.0.1’, ’$dbname’, ’$user’, ’$pass’, true))){
    echo " No list a the moment !";
    }


    if (!@$modx->db->connect(’127.0.0.1’, ’$dbname’, ’$user’, ’$pass’, true)){
    echo " No list a the moment !";
    }




    what am I doing wrong?

    Thanks Lisa
      • 17233
      • 15 Posts
      I guess I have to find out myself grin

      I haven’t used modx that long, and I find it strange. Modx should be easy or what?

      I find php easy, and I guess modx need som of the tags or what

      modx with mix of other tags should it be named, if you ask me.

      this works php rolleyes

      $connect = mysql_connect ($host, $dbuser, $dbpass);
      $select_db = mysql_select_db($dbname,$connect);
      if(!@ select_db){
      echo " No list ";
      }

        • 9207 ☆ A M B ☆
        • 2,475 Posts
        This sounds more like a MySQL question... are you sure you have access to this other table?

        Can you try logging into the other database from the mysql command line? When you use your database user and database name as login criteria, can you see this other table? REMEMBER: TRY THIS USING THE USERNAME/PASSWORD IN YOUR MODX CONFIG:

        e.g.
        mysql -u yourusername -p


        (type your password), then see which databases you have access to:
        show databases;


        Pick one:
        use mydatabase;


        Then show tables:
        show tables;



        Can you see the table you’re trying to connect to? If you can’t see the db/table using your MODx login criteria, then that MySQL user doesn’t have the correct privileges.

        When you use the $modx object, it only has the privileges that are granted to the user that’s being used in the MODx config file. That’s just how MySQL works. When you use the mysql_connect() method, you can supply a username and password that are completely separate from the database name and user that are defined in the config file. Make sense?