Categories
MySQL

MySQL Error Number 1045

Connection attempts to a MySQL database can return "MySQL Error Number 1045".  This is a frustrating error, but has a reasonably simple solution.

While this solution is presented in the context of databases hosted on dreamhost.com accessed by the MySQL Administrator tool, most of the discussion is likely to apply to anyone encountering the 1045 error.

At the root of the error is the fact that the database server validates not only the user account making the request but also the machine the request is being made from.  To resolve the error it is necessary to tell the server that the machine is authorized.

The first step is determine the fully qualified host name of the machine you are accessing from.  The easiest way to do that is to google "display my host name".  This will locate sites like www.displaymyhostname.com, which will show you the fully qalified host name for your machine.  Copy this, you'll need it in a moment.

In the case of Dreamhost.com databases, log into your dreamhost.com control panel, then select MySQL databases from the menu.

Scroll down to the database you were trying to access, then to the right of the database name click the username being used.

The next page lists the databases that user has access to and the rights held on those databases.

In the "Allowable Hosts" section, add, on a separate line, the fully qualified host name you identified above.

Save the changes, and you're done.  You'll now be able to access your database.

Categories
Database MySQL

MySQL GUI Tools – Still ready for Prime Time

MySQL ceased development of their GUI Tools, MySQL Administrator and MySQL Query Browser, with the release of their MySQL Workbench product.

Workbench is supposed to incorporate the functions of Administrator and Query Browser, but does a poor job of it.  While it has many new features which are no doubt very useful, it does a very poor job on some of the basic functions.  Inexplicably, it does not allow you to edit the results of a query without using a non-standard SQL syntax and won't let you edit the results at all unless all the fields have been returned in the query.  The only way you can limit the query is with WHERE clauses if you want to edit the results.

The MySQL representative on the support forum doesn't see that as a problem, but this sort of ad hoc editing is what made query editor so useful.  Fully 90% of my work in Query Editor is to edit small data subsets.  For instance, in a membership table you might wish to return a query like SELECT fname,lname,city FROM members WHERE lname = ‘Smith’; to isolate a record   As it stands, workbench won’t let you limit the query by restricting the fields.  You can use the WHERE clause, but you can’t limit the fields.  Not much help if you have dozens of fields in the table.

Until this is rectified, assuming it ever is, I recommend installing the Administrator and Query Browser from the old GUI tools package which is still available at the link below.  The best bet for the foreseeable future is to install the old tools with the most recent version of MySQL Workbench.  Get used to the new tool, and use the old tools for what they do best until Workbench catches up.

As much as I love MySQL and Open Source in general, I find the blasé attitude of the MySQL support staff inexplicable.  Why they would cripple their new tool by removing a useful function available in the previous tool is beyond me.

imageDownload and install the old MySQL GUI tools

Download and run the old GUI Tools here.  If you’re not familiar with .msi files, they are the Microsoft Installer format, just double-click them and they’ll start the install.

http://downloads.mysql.com/archives/MySQLGUITools/ mysql-gui-tools-5.0-r12-win32.msi

When installing, select Custom on the Setup Type dialog.  Don't install MySQL Migration Toolkit or Language Support unless you really need them.

Categories
MySQL PHP

Loading configuration data from a MySQL into PHP constants

The following code will take the contents of the first row of a configuration table and create a constant for each attribute stored in it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// ----------------------------- LOAD CONFIGURATION DATA FROM DATABASE -----------------------------
// Each data element in first row is placed in a constant using attribute name as constant name
// The array_slice function eliminates the first attribute in the row, 'id', which is the primary
//   key and not relevant for our purposes.
$result = mysql_query("SELECT * FROM configuration WHERE id = '1'") or die(__LINE__." SELECT Error: ".mysql_error());
if($result != false and mysql_num_rows($result)) {
    foreach(array_slice(mysql_fetch_assoc($result), 1) as $key => $val) {
        if(!defined($key)) {
            define(strtoupper($key), $val);
        } else {
            user_error("'$key' already defined.");
        }
    }
}
// ---------------------------- /load configuration data from database -----------------------------
Categories
MySQL

Cannot edit table data in MySQL Query Browser

If a table cannot be edited in MySQL, check to ensure that it has a primary key.

This can arise if a table is copied from another table – the copy will not have a primary key.

Adding a primary key will resolve the problem.

Categories
MySQL PHP

Building a dynamic SELECT list with PHP and MySQL

A common scenario in web applications is the need to create a <SELECT> list using a set of values extracted from a lookup table in a database. This article will explore a simple example of how to do that.

For this example, imagine that in our database we have a lookup table, lu_status, that holds all the possible values we will use for the status field in our form.  Imagine also the the attribute name in our form is 'status', and that our lu_status table has two attributes; status, which is the value and option_label which will be the visible label in the select control drop list. Because we might be using the <select> list in a form to edit existing records in a data table, we can also assume that the current value of the status attribute, if any, is in the variable $status.

In line 2, we create a variable, $select_status, and put the first line of the code for our select control in it.  The name 'status' in that line is the attribute name in the data table that the form will be feeding. The \n at the end of the line will cause a line break in the source code of the HTML page, making it easier to read and troubleshoot.

Line 3 is the default –SELECT ONE– value commonly used in forms when no value has previously been selected.

Lines 4-12 are where we access that database and get to work.

In line 4, we read all the possible values from the lookup table which we will use to populate our select list.

Line 5 opens a while loop which we will use to process each row that we read from the lookup table in the previous row, creating a <option> element for each.

Line 6 opens the <option> element.

Lines 7-11 are an if statement which will determine if the current value of the status attribute (if there is a current value) matches the current status attribute of the current row from the lookup table. If so, it adds the attribute selected='selected' to the option element, otherwise it creates the element without the selected attribute. In either case, the status attribute and option_label attribute from the current row are inserted into the <option> element appropriately. Each row in the table lu_status will be processed this way inside the while loop.

Now that we've build all our <option> elements, all that remains is to close the <select> control in line 13 and we're done.

Here's the PHP code;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// assemble the HTML code for the <select> list in a variable called $select_status
$select_status = "<select name='status'>\n";
$select_status .= "<option value=''>SELECT ONE</option>\n";
$dataset = mysql_query("SELECT * FROM lu_status");
while($row=mysql_fetch_array($dataset)) {
  $select_status .= "<option value='".$row['option_label']."'";
  if (strtolower($row['status']) == strtolower($status)) {
    $select_status .= " selected='selected'>".$row['option_label']."</option>\n";
  } else {
    $select_status .= ">".$row['option_label']."</option>\n";
  }
} // end while loop
$select_status .= "</select>\n";
// now insert the <select> list control into the page
echo "$select_status";

And here's the HTML code we will see generated;

1
2
3
4
5
<select name='status'>
  <option value=''>SELECT ONE</option>
  <option value='active' selected='selected'>Active</option>
  <option value='inactive'>Inactive</option>
</select>