Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2003
    Posts
    142

    Unanswered: Conncet to MongoDB from PHP

    Hi,

    I currently using below to connect to MySQL from PHP.

    How can I use the same or anyother way to connect to MongoDB from PHP?

    <?php
    header("Content-Type: application/json");

    if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400'); // cache for 1 day
    }

    $customer_id = $_GET["customer_id"];

    $mysql_host = "mysql:host=mysql.my_domain.com;dbname=my_db";
    $mysql_user = "my_user";
    $mysql_password = "my_password";
    $mysql_options = array
    (
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
    );

    // $mysql_connection;
    $mysql_connection = new PDO($mysql_host, $mysql_user, $mysql_password, $mysql_options);
    $mysql_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $mysql_query = $mysql_connection->prepare('CALL sp_populate_customer_addresses(aram_customer)');
    $mysql_query->bindParam('aram_customer', $customer_id, PDO::PARAM_STR);
    $mysql_query->execute();

    if ($mysql_query->rowCount() <= 0) { echo "false"; }
    else
    {
    $jsonData = array();

    while($mysql_row = $mysql_query->fetch())
    {
    $jsonData[] = $mysql_row;
    }

    echo json_encode($jsonData);
    }
    ?>



    Thanks,
    Jassim

  2. #2
    Join Date
    Jan 2016
    Posts
    12
    To use MongoDB with PHP, you need to use MongoDB PHP driver.

    extension = php_mongo.dll
    Following is the code snippet to connect to the database −
    <?php
    // connect to mongodb
    $m = new MongoClient();

    echo "Connection to database successfully";
    // select a database
    $db = $m->mydb;

    echo "Database mydb selected";
    ?>

Posting Permissions

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