Solved

MySQL Query Errors

Posted on 2014-09-11
4
136 Views
Last Modified: 2014-09-29
I'm testing a simple SQL query from my Products table, in the Navigator_Data database, whihc is the second database to my Drupal site.

I put this in a Block (PHP code text format), and get this error.

<?php
  $navigator_array = array(
      'database' => 'navigator_data',
      'username' => 'sandshakimi', 
      'password' => 'XXX', 
      'host' => 'localhost', 
      'driver' => 'mysql', 
  );
  Database::addConnectionInfo('PRODUCT', 'default', $navigator_data);
  db_set_active('PRODUCT');
  $result = mysql_query('SELECT * FROM `PRODUCT` ');
  db_set_active(); // without the paramater means set back to the default for the site

?>

Open in new window


 SCscdata-2.JPG
0
Comment
Question by:sandshakimi
  • 2
4 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Those functions must come from Drupal because they are not PHP functions.  Except... mysql_query() is probably Not the method being used in Drupal.  They are probably using the 'mysqli' driver which is different than the 'mysql' driver.  Look at the Drupal code to find their method of doing a query.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I have not verified this, but at first glance it looks like it's on target.
http://alvinalexander.com/mysql/drupal-create-mysql-database-user-account-example
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I think db_query() may be the Drupal wrapper for PHP mysql_query().  The problem appears to be caused by missing or incorrect authentication credentials, or missing permissions in the MySQL database.
0
 
LVL 13

Accepted Solution

by:
Arrow_1 earned 500 total points
Comment Utility
You should be defining your database connection info in your settings.php file. It should look something like this:
$databases['nav_data']['default'] = array(
    'database' => 'navigator_data',
    'username' => 'sandshakimi',
    'password' => 'XXX',
    'host' => 'localhost',
    'driver' => 'mysql',
);

Open in new window


Then in your code, access it this way:
$nav_data = Database::getConnection('default', 'nav_data');
$result = $nav_data->query('SELECT * FROM `PRODUCT` ');

Open in new window


or this way:
db_set_active('nav_data');
$result = db_query('SELECT * FROM `PRODUCT` ');
db_set_active();

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now