Solved

MySQL Query Errors

Posted on 2014-09-11
4
164 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 83

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 110

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 110

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:
Arrow_1 earned 500 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

636 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