Strange behaviour

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Strange behaviour

Postby geojay » 10. February 2010 10:20

I've been suffering Apache restarting on my PC where I do my development. I don't see this on my proper web server. On my PC I'm running XAMPP 1.7.3 on Windows XP with PHP Version 5.3.1, Apache 2.2.14 and MySQL 5.1.41.

I've managed to reduce the problem down to the following which I'm at a loss to explain:


Code: Select all
$sql  = "SELECT text ";
$sql .= "FROM t_text ";
$sql .= "WHERE name = '$name' ";
$sql .= "AND `site_id` = {$opus['site_id']} ";
$text_result = db_query($sql, __LINE__, __FILE__);

$return1 = mysql_result($text_result, 0, NULL);
print('return1 = ' . $return1 . "<br/>\n");

function mysql_safe_result_local($result, $row = NULL, $field = NULL)
{
   $value = mysql_result($result, $row, $field);
   $value_safe = htmlspecialchars($value);
   return $value_safe;
}
$return2 = mysql_safe_result_local($text_result, 0);
print('return2 = ' . $return2 . "<br/>\n");

function mysql_safe_result_local2($result, $row = NULL)
{
   $value = mysql_result($result, $row);
   $value_safe = htmlspecialchars($value);
   return $value_safe;
}
$return3 = mysql_safe_result_local2($text_result, 0);
print('return3 = ' . $return3 . "<br/>\n");

$return4 = htmlspecialchars(mysql_result($text_result, 0, NULL));
print('return4 = ' . $return4 . "<br/>\n");


This gives me the following result (when 'test test' is the string stored in the database):
return1 = test test
return2 = !���!����
return3 = test test
return4 = test test


Where $return2 looks like nonsense. This only seems to happen when the function is used, not when the equivalent processing happens in line. Within the function, the $field variable needs to be NULL to see the problem. Outside of the function this has no effect in producing the problem.

Can anyone suggest what is going on or what I should do by way of further diagnosis?

Many thanks!
geojay
 
Posts: 12
Joined: 09. February 2010 13:44

Re: Strange behaviour

Postby geojay » 10. February 2010 19:56

Here's a stand alone version of it with some more variations. I'd be interested to know if other people see the same issues if they have the same version of XAMPP as me (XAMPP 1.7.3 on Windows XP with PHP Version 5.3.1, Apache 2.2.14 and MySQL 5.1.41.)

Code: Select all
<?php

   // Start database connection
   function db_start($db_host, $db_user, $db_password, $db_database)
   {
      // Connecting to database server
      $link = mysql_connect($db_host, $db_user, $db_password)
         or trigger_error("Could not connect to \"$db_host\" with user \"$db_user\": " . mysql_error(), E_USER_ERROR);
      // Selecting database
      mysql_select_db($db_database)
         or trigger_error("Could not select database :  $db_database", E_USER_ERROR);
      return $link;
   }

   function db_query($sql, $line, $inc_file_name)
   {
      global $opus;
      
      $mysql_version = "mysql_get_server_info()";
      if (!preg_match('/^3/', $mysql_version))
      {
         mysql_query("SET NAMES 'utf8'"); /* note the apostrophes: 'utf8' */
         mysql_query("SET CHARACTER SET utf8"); /* no apostrophes */
      }

      $return = mysql_query($sql)
         or trigger_error(mysql_error() . " at line $line  in file '$inc_file_name' . <br>\nSQL = '$sql'. ", E_USER_ERROR);
      return $return;
   }


   $opus['cfg']['db']['host'] = 'localhost';
   $opus['cfg']['db']['user'] = 'root';
   $opus['cfg']['db']['password'] = '';
   $opus['cfg']['db']['database'] = 'photosdb';

    // Connecting, selecting database
    db_start($opus['cfg']['db']['host'], $opus['cfg']['db']['user'], $opus['cfg']['db']['password'], $opus['cfg']['db']['database']);

   $name = 'contact:page_text';
   $opus['site_id'] = 1;

   // Get site specific text
   $sql  = "SELECT text ";
   $sql .= "FROM t_text ";
   $sql .= "WHERE name = '$name' ";
   $sql .= "AND `site_id` = {$opus['site_id']} ";
   $text_result = db_query($sql, __LINE__, __FILE__);

   // 1. Inline with no htmlspecialchars()
   $return1 = mysql_result($text_result, 0, NULL);
   print('return1 = ' . $return1 . "<br/>\n");


   // 2. Inline with htmlspecialchars()
   $return2 = htmlspecialchars(mysql_result($text_result, 0, NULL));
   print('return2 = ' . $return2 . "<br/>\n");


   // 3. Use function and $field = NULL
   function mysql_safe_result_3($result, $row = NULL, $field = NULL)
   {
      $value = mysql_result($result, $row, $field);
      $value_safe = htmlspecialchars($value);
   print('value_safe = ' . $value_safe . "<br/>\n");

      return $value_safe;
   }
   $return3 = mysql_safe_result_3($text_result, 0);
   print('return3 = ' . $return3 . "<br/>\n");


   // 4. Use function, print after mysql_result() and $field = NULL
   function mysql_safe_result_4($result, $row = NULL, $field = NULL)
   {
      $value = mysql_result($result, $row, $field);
      print('value = ' . $value . "<br/>\n");
      $value_safe = htmlspecialchars($value);
      print('value_safe = ' . $value_safe . "<br/>\n");

      return $value_safe;
   }
   $return4 = mysql_safe_result_4($text_result, 0);
   print('return4 = ' . $return4 . "<br/>\n");


   // 5. Use function and third argument of mysql_result() set to NULL
   function mysql_safe_result_5($result, $row = NULL)
   {
      $value = mysql_result($result, $row, NULL);
      $value_safe = htmlspecialchars($value);
      print('value_safe = ' . $value_safe . "<br/>\n");

      return $value_safe;
   }
   $return5 = mysql_safe_result_5($text_result, 0);
   print('return5 = ' . $return5 . "<br/>\n");


   // 6. Use function and third argument of mysql_result() not explicitly set
   function mysql_safe_result_local_6($result, $row = NULL)
   {
      $value = mysql_result($result, $row);
      $value_safe = htmlspecialchars($value);
      return $value_safe;
   }
   $return6 = mysql_safe_result_local_6($text_result, 0);
   print('return6 = ' . $return6 . "<br/>\n");

?>


This gives the result:

return1 = test test
return2 = test test
value_safe = test test
return3 = !���9���ø
value = test test
value_safe = test test
return4 = test test
value_safe = test test
return5 = test test
return6 = test test


Note that the only difference between 3 and 4 is the print after the mysql_result() call. Surely a print shouldn't affect anything??

I'm completely confused!

Thanks,
Geoff
geojay
 
Posts: 12
Joined: 09. February 2010 13:44


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 131 guests