saurabhsood91
saurabhsood91

Reputation: 479

Using Moodle Data Manipulation API

I am trying out sample queries of the Data Manipulation API for moodle. I have tried out the following query so far

<?php
require './config.php';
global $DB;
$user= $DB->get_record_sql('SELECT * FROM {mdl_user} WHERE id=?', array(1));
echo mysql_num_rows($user);

?>

I am getting 'Error Reading from Database'. I am using moodle on a local installation. What am I doing wrong here?

Upvotes: 1

Views: 2175

Answers (1)

franzlorenzon
franzlorenzon

Reputation: 5943

  1. I don't think you need to add a prefix to the table, as it is added automatically;
  2. get_records_sql() return the records as an array of objects.

In this case you're getting just one record, so it is a single object.

With the appropriate corrections, this code worked for me:

<?php
require './config.php';
global $DB;
$user= $DB->get_record_sql('SELECT * FROM {user} WHERE id=?', array(1));
var_dump($user);
?>

Upvotes: 2

Related Questions