Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MySQL Query Errors

Posted on 2014-09-11
4
Medium Priority
?
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40317787
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 111

Expert Comment

by:Ray Paseur
ID: 40317958
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 111

Expert Comment

by:Ray Paseur
ID: 40320052
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:
Aaron Feledy earned 2000 total points
ID: 40320090
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 to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 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