Jump to content

ludo1960

Members
  • Posts

    123
  • Joined

  • Last visited

Posts posted by ludo1960

  1. 3 hours ago, ginerjm said:

    unfamiliar with Mongodb so this may be a silly question but what is this line saying:

    
     $db = new MongoDB\Driver\Manager("mongodb://localhost:27017"); 

    Is "MongoDB\Driver\Manager"  a function name that is part of the mongodb interface?   To me it looks more like a path (without quotes) so I am puzzled.

    Sorry, the link is here https://www.php.net/manual/en/set.mongodb.php there is our chum MongDB\Driver\Manager .

  2. Hello guys,

    I've installed the mongoDB PHP driver using "sudo pecl install mongodb"

    Testing shows driver is installed correctly and it appears nicely in phpinfo():

    ludo@aegir:~$ php7.2 -i | grep -i mongodb
    mongodb
    MongoDB support => enabled
    MongoDB extension version => 1.5.3
    MongoDB extension stability => stable
    mongodb.debug => no value => no value 
    

    For connecting I've got:

       $db = new MongoDB\Driver\Manager("mongodb://localhost:27017"); 

    Now I want to define a collection and insert a record, i've been using code from here  https://www.php.net/manual/en/set.mongodb.php:

    $collection = $db->mydatabase->mytable;
    
    $result = $collection->insertOne( [ 'name' => 'test', 'item' => 'testitem' ] );

    But this fails  Notice: Undefined property: MongoDB\Driver\Manager::$mydatabase in /var/www....

    Has anyone got a workingg example of defining and inserting into mongoDB. Seems the driver was updated fairly recently and so stuff on the internet is not always relevant. 

  3. Good news guys, found the error:

    I was having a tough time trying to install xdebug, I used the output from php -v and put it in here: https://xdebug.org/wizard.php  The output from my php.ini was PHP 7.2.18-..... Then the wizard gave me the steps to follow. The script needs phpize from the php-dev bundle seemingly. As I had just rebuilt my dev box, I was watching the xdebug install doing its thing and I noticed that php7.0 and php7.3 kept whizzing by as the script progressed. Strange I thought, i've got php7.2 why is xdebug downloading all this stuff?? Needless to say when i tried to get xdebug working by adding 

    zend_extension=/usr/lib/php/20170718/xdebug.so

    to the php.ini, and you guessed it, it didn't work! Plan B was called for, re-installed my dev box from scratch. Then installed php7.2-dev so that I had the right version of phpize, and it all went to plan. No more double entries in the database, and all errors are displayed...Result!!! Moral of the tale, dont trust the xdebug wizard!

  4. Ok, after trying it all again ie truncate, load page again, still double entries. Here's the maria.err log

    2019-05-14 20:37:23 139840968318336 [Note] InnoDB: innodb_empty_free_list_algorithm has been changed to legacy because of small buffe$
    
    2019-05-14 20:37:23 139840968318336 [Note] InnoDB: Using mutexes to ref count buffer pool pages
    2019-05-14 20:37:23 139840968318336 [Note] InnoDB: The InnoDB memory heap is disabled
    2019-05-14 20:37:23 139840968318336 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
    2019-05-14 20:37:23 139840968318336 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
    2019-05-14 20:37:23 139840968318336 [Note] InnoDB: Compressed tables use zlib 1.2.8
    2019-05-14 20:37:23 139840968318336 [Note] InnoDB: Using Linux native AIO
    2019-05-14 20:37:23 139840968318336 [Note] InnoDB: Using SSE crc32 instructions
    2019-05-14 20:37:23 139840968318336 [Note] InnoDB: Initializing buffer pool, size = 128.0M
    2019-05-14 20:37:23 139840968318336 [Note] InnoDB: Completed initialization of buffer pool
    2019-05-14 20:37:23 139840968318336 [Note] InnoDB: Highest supported file format is Barracuda.
    2019-05-14 20:37:24 139840968318336 [Note] InnoDB: 128 rollback segment(s) are active.
    2019-05-14 20:37:24 139840968318336 [Note] InnoDB: Waiting for purge to start
    2019-05-14 20:37:24 139840968318336 [Note] InnoDB:  Percona XtraDB (http://www.percona.com) 5.6.42-84.2 started; log sequence number $
    2019-05-14 20:37:24 139840301561600 [Note] InnoDB: Dumping buffer pool(s) not yet started
    2019-05-14 20:37:24 139840968318336 [Note] Plugin 'FEEDBACK' is disabled.
    2019-05-14 20:37:24 139840968318336 [Note] Server socket created on IP: '127.0.0.1'.
    2019-05-14 20:37:24 139840968318336 [Note] Reading of all Master_info entries succeded
    2019-05-14 20:37:24 139840968318336 [Note] Added new Master_info '' to hash table
    2019-05-14 20:37:24 139840968318336 [Note] /usr/sbin/mysqld: ready for connections.
    Version: '10.1.38-MariaDB-0+deb9u1'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  Debian 9.8

     

  5. ok,

    I added  !include /etc/mysql/mariadb.cnf to my /etc/mysql/my.cnf

    and added 

    log_error=/var/log/mysql/mariadb.err 

    to /etc/mysql/mariadb.cnf

    restarted mysql, truncated and tried again, still double entry and no mariadb.err where it should be??

  6. Added it to /etc/php/7.2/cli/php.ini  :

    error_reporting = E_ALL
    
    error_reporting(E_ALL);
    ini_set('display_errors', '1');

    Restarted Nginx, sudo service php7.2-fpm reload, truncated the database table, tried page again, still double entry and no errors.

  7. sure, here you go:

    <?php   
    
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    
    include_once 'connection.php' ;
    
    $url = "http://admin.contenta.com/api";
    
    //$url = 'https://live-contentacms.pantheonsite.io/api';
    
    $jsondata = file_get_contents($url);
    
    $response = json_decode($jsondata, true);
    
    $myarray = $response['links'] ;
    
    foreach ($myarray as $k => $v) {
           $newarr[strval($k)] = $v['href']; 
    }
    
        $res = $db->prepare("INSERT INTO apidata (item, value) VALUES (?, ?)") ;
        
        foreach ($newarr as $key => $value) {
            $res->execute( [ $key, $value ] );
        }
    
        echo "<pre>";  print_r( $newarr ); echo "</pre>";

     

  8. Stranger and stranger, if I run the code with the original code above: i get:

    Array
    (
        [self] => h
        [blocks] => h
        [comments] => h
        [reviews] => h
        [commentTypes] => h
        [consumer--consumer] => h
        [files] => h
        [graphql_query_map--graphql_query_map] => h
        [imageStyles] => h
        [mediaBundles] => h
        [images] => h
        [articles] => h
        [pages] => h
        [recipes] => h
        [node--tutorial] => h
        [contentTypes] => h
        [menus] => h
        [vocabularies] => h
        [categories] => h
        [tags] => h
        [roles] => h
        [users] => h
        [menuLinks] => h
    )

    And the database is like so:

    image.thumb.png.3e46f1eb36244d1cb544012459214168.png

    Then changing to your new version of the code gives me:

    Array
    (
        [self] => https://live-contentacms.pantheonsite.io/api
        [blocks] => https://live-contentacms.pantheonsite.io/api/blocks
        [comments] => https://live-contentacms.pantheonsite.io/api/comments
        [reviews] => https://live-contentacms.pantheonsite.io/api/reviews
        [commentTypes] => https://live-contentacms.pantheonsite.io/api/commentTypes
        [consumer--consumer] => https://live-contentacms.pantheonsite.io/api/consumer/consumer
        [files] => https://live-contentacms.pantheonsite.io/api/files
        [graphql_query_map--graphql_query_map] => https://live-contentacms.pantheonsite.io/api/graphql_query_map/graphql_query_map
        [imageStyles] => https://live-contentacms.pantheonsite.io/api/imageStyles
        [mediaBundles] => https://live-contentacms.pantheonsite.io/api/mediaBundles
        [images] => https://live-contentacms.pantheonsite.io/api/images
        [articles] => https://live-contentacms.pantheonsite.io/api/articles
        [pages] => https://live-contentacms.pantheonsite.io/api/pages
        [recipes] => https://live-contentacms.pantheonsite.io/api/recipes
        [node--tutorial] => https://live-contentacms.pantheonsite.io/api/node/tutorial
        [contentTypes] => https://live-contentacms.pantheonsite.io/api/contentTypes
        [menus] => https://live-contentacms.pantheonsite.io/api/menus
        [vocabularies] => https://live-contentacms.pantheonsite.io/api/vocabularies
        [categories] => https://live-contentacms.pantheonsite.io/api/categories
        [tags] => https://live-contentacms.pantheonsite.io/api/tags
        [roles] => https://live-contentacms.pantheonsite.io/api/roles
        [users] => https://live-contentacms.pantheonsite.io/api/users
        [menuLinks] => https://live-contentacms.pantheonsite.io/api/menuLinks
    )

    And my database looks like:

    image.thumb.png.d0a31bc6d4e4818a0e9aea7c17e1b5e8.png

    My database must be goosed! How lucky is that?

  9. Try this yourself and you will see double entries:

    <?php   
    
    include_once 'connection.php' ;
    
    //$url = "http://admin.contenta.com/api";
    
    $url = 'https://live-contentacms.pantheonsite.io/api';
    
    $jsondata = file_get_contents($url);
    
    $response = json_decode($jsondata, true);
    
    $myarray = $response['links'] ;
    
    foreach ($myarray as $k => $v) {
            $newarr[strval($k)] = $v['href']; 
    }
    
        $res = $db->prepare("INSERT INTO apidata (item, value) VALUES (?, ?)") ;
        
        foreach ($newarr as $key => $value) {
            $res->execute( [ $key, $value ] );
        }
    
        echo "<pre>";  print_r( $newarr ); echo "</pre>";

     

  10. Already got that in my connection.php, and no errors are shown?

    function pdoConnect() 
    {
        $dsn = "mysql:dbname=".DBNAME."; host=".HOST."; charset=utf8";
    
        $db = new pdo($dsn, USERNAME, PASSWORD, 
            [
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_EMULATE_PREPARES => false,
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            ]);
        return $db;
    }
    
    $db = pdoConnect();

     

  11. I changed the item column to be UNIQUE, and now 23 records are inserted. Don't thinks it is a proper fix, would love to know why the code causes a double insert. I don't see how the code is being run twice, surely a web page just refreshes itself once?

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.