Dreamweaver troubles: Difference between revisions

From ShawnReevesWiki
Jump to navigationJump to search
Line 44: Line 44:
:And that can be replaced with this, using regex back-references:
:And that can be replaced with this, using regex back-references:
  if(!$1 = mysqli_query($3, $2)) die(mysqli_error($3));
  if(!$1 = mysqli_query($3, $2)) die(mysqli_error($3));
;Fifth, note that getting a row and counting are pretty similar, except the row count may depend on whether the entire result is pre-loaded or buffered or not:
;Fifth, note that getting a row and counting rows are pretty similar, except the row count may depend on whether the entire result is pre-loaded or buffered or not:
  $row_rsRecordsetName = mysqli_fetch_assoc($rsRecordsetName);
  $row_rsRecordsetName = mysqli_fetch_assoc($rsRecordsetName);
  $totalRows_rsRecordsetName = mysqli_num_rows($rsRecordsetName);
  $totalRows_rsRecordsetName = mysqli_num_rows($rsRecordsetName);
;Sixth, iterating through rows is also very similar:
;Sixth, data seeks and iterating through rows is also very similar:
mysqli_data
  <?php } while ($rsRecordsetName = mysqli_fetch_assoc($rsRecordsetName)); ?>
  <?php } while ($rsRecordsetName = mysqli_fetch_assoc($rsRecordsetName)); ?>
;Seventh, as usual, you need to free the result at the end, but you also should close the connection:
;Seventh, as usual, you need to free the result at the end, but you also should close the connection:

Revision as of 11:09, 30 April 2013

Here I'll list some problems I've had with Dreamweaver and my solutions, when found. Keep in mind I use Mac versions.

No table found

I was working on a database-driven PHP/MySQL page. At first I could work normally, but after a certain hour I couldn't access any tables from any database in Dreamweaver CS5.

I tried several troubleshooting methods, including restarting DW, restarting the computer, deleting the preferences files, deleting the Application Support folder, deleting go-between scripts on the server, and updating the scripts to match the new addresses Network Solutions assigned my databases without telling me. That last one I thought would be it. Nope, although I think it must have been *part* of the problem. While attempting to change site settings, I got a message that the test server was not responding, so I turned on Web Sharing on my Mac, which is the assigned Test Server for my site, and that fixed the problem, probably along with the MySQL server IP address update. DW, although it shouldn't have to, uses the Test Server for all sorts of behind-the-scenes actions, so you might find this hint useful:Keep the Test Server well oiled.

This is especially relevant now that more and more Mac users are using OS versions beyond Lion. Mountain Lion does not include the Web Sharing choice in the Sharing pane of System Preferences, so it is harder to turn Apache/httpd on and off. See this document at Apple.com if your Mac's web server is off and you don't know how to turn it on: https://discussions.apple.com/docs/DOC-3083

Stale PHP/MySQL routines

The developers of PHP have decided to deprecate their original mysql library. As of April 2013, Adobe hasn't changed the way Dreamweaver handles PHP/MySQL connections and queries much since version CS3. CS6 does not use the improved PHP library mysqli for connections, and the routines for recordsets and dynamic texts and actions are the same as ever. See the page on PHP.net that explains the deprecation: http://docs.php.net/manual/en/intro.mysql.php

However, all the behaviors can be edited by users, since Dreamweaver uses html, javascript, and their markup language edml to tell DW how to interpret dynamic page routines. In the Dreamweaver application package folder you can find those routines and might replace PHP's mysql functions with mysqli functions, for example changing mysql_num_rows to mysqli_num_rows. You'd have to change all the functions at once, since a single connection can be only mysql or mysqli, and would require all functions to match the connection. You might think changing might be as simple as replacing all instances of mysql_ with mysqli_. However, the way mysqli returns rows may be different and some mysqli functions take different arguments, so your scripts will need more careful editing.

Oracle's mysql>mysqli converter

For a utility that promises to change mysql procedures to mysqli procedures in the most straightforward fashion, see Oracle's converter: https://wikis.oracle.com/display/mysql/Converting+to+MySQLi

It might be interesting to convert not only one's scripts but Dreamweaver's Server Behavior files themselves.

I tried Oracle's converter on some Dreamweaver code and had two problems. One is that snippets must be solely the php part of scripts, not entire html pages. Another is that Dreamweaver uses a single, separate connection file with all pages that share the same connection parameters, so the connection is split from the rest of the code, and the converter doesn't handle the initial link properly anyway.

My way of converting existing web pages

I change all the functions to their mysqli equivalents, converting arguments to their proper order and place, and I add the required procedures such as mysqli_close(). Here's how I convert a typical page that lists rows of data from a database:

First change the connect-function in the connection file to this
$ConnectionName = mysqli_connect($hostname_ConnectionName,
$username_ConnectionName, $password_ConnectionName,$database_ConnectionName);
if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}
Second, since the mysqli version of connection selects the database, you can remove the db-selecting command that appears before every query, looking like this
mysql_select_db($database_ConnectionName, $ConnectionName);
Those can be found with the following regular expression, and replaced with nothing:
mysql_select_db(\s*$\w*\s*,\s*$\w*\s*)\s*;
Third, if there's data entry, change the GetSQLValueString function to use mysqli. Specifically, noting that mysqli_real_escape_string takes the connection, not the value, as the first argument, and the value as the second argument
 $theValue = function_exists("mysqli_real_escape_string")
? mysqli_real_escape_string($ConnectionName,$theValue) : mysql_escape_string($theValue);
Fourth, change the query call and error check to this
if(!$rsRecordsetName = mysqli_query($ConnectionName,$query_rsRecordsetName)) {die(mysql_error());}
It's pretty easy to change the from the old format by using a regular expression search and replace. Originally Dreamweaver set a query to look like this:
$resultname = mysql_query( $querystring, $connectionname) or die(mysql_error());
That can be matched by this:
(\$\w*)\s*=\s*mysql_query\((\$\w*),\s*(\$\w*)\)\s*or\s*die\(mysql_error\(\)\);
And that can be replaced with this, using regex back-references:
if(!$1 = mysqli_query($3, $2)) die(mysqli_error($3));
Fifth, note that getting a row and counting rows are pretty similar, except the row count may depend on whether the entire result is pre-loaded or buffered or not
$row_rsRecordsetName = mysqli_fetch_assoc($rsRecordsetName);
$totalRows_rsRecordsetName = mysqli_num_rows($rsRecordsetName);
Sixth, data seeks and iterating through rows is also very similar
mysqli_data
<?php } while ($rsRecordsetName = mysqli_fetch_assoc($rsRecordsetName)); ?>
Seventh, as usual, you need to free the result at the end, but you also should close the connection
mysqli_free_result($rsRecordsetName);
mysqli_close($ConnectionName);

For future web pages

When creating new web pages, I should not use the old Dreamweaver way of constructing queries, but use the more secure methods for which mysqli was improved, like using parameters and binding for passing parameters from forms into preset mysql queries.

Some interesting, simple tutorials about using mysqli, to show multiple rows of data, insert rows from form variables, and other basic routines, are available here: http://www.phpmysqlitutorials.com/category/mysqli/