|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-11-19 19:22 UTC] jrust@php.net
The following mysql query should return the data padded with spaces on the right:
<?php
$query = '
SELECT
RPAD(t1.f_field1,15,\' \'),
RPAD(t2.f_field2, 15, \' \')
FROM
test.table1 as t1
LEFT JOIN test.table2 AS t2 ON t1.record=t2.joinID
ORDER BY t1.record DESC';
$link = mysql_connect('localhost', 'root', 'password');
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
var_dump($row);
}
?>
However, the all spaces to the right of the string are chopped off, as if PHP (or maybe the MySQL api?) is doing an rtrim on the data.
Some details:
* The exact same query run from the MySQL console returns the correctly padded data.
* If the ORDER BY is commented out the data comes back padded correctly.
* If the query does not contain a JOIN the data comes back padded correctly (even if there is an ORDER BY)
* It doesn't matter what the data is for table1 and table2 so long as the two tables can be joined and have some data in the fields
* I am using MySQL version 3.23.52
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 19:00:01 2025 UTC |
Works fine here: [derick@kossu derick]$ mysqldump -u root -p bug20510 Enter password: -- MySQL dump 8.21 -- -- Host: localhost Database: bug20510 --------------------------------------------------------- -- Server version 3.23.49a -- -- Table structure for table 'table1' -- CREATE TABLE table1 ( record int(10) unsigned NOT NULL auto_increment, field1 varchar(30) NOT NULL default '', PRIMARY KEY (record) ) TYPE=MyISAM; -- -- Dumping data for table 'table1' -- INSERT INTO table1 VALUES (1,'test'); -- -- Table structure for table 'table2' -- CREATE TABLE table2 ( record int(10) unsigned NOT NULL auto_increment, joinID int(10) unsigned NOT NULL default '0', field2 varchar(30) NOT NULL default '', PRIMARY KEY (record) ) TYPE=MyISAM; -- -- Dumping data for table 'table2' -- INSERT INTO table2 VALUES (1,1,'join one'); INSERT INTO table2 VALUES (3,1,'join two'); [derick@kossu derick]$ cat bug20510.php <?php mysql_connect('localhost', 'root', 'mypassword'); mysql_select_db('bug20510'); $res = mysql_query ("SELECT RPAD(t1.field1,15, ' '), RPAD(t2.field2, 15, ' ') FROM table1 as t1 LEFT JOIN table2 AS t2 ON t1.record=t2.joinID ORDER BY t1.record DESC"); while ($row = mysql_fetch_row($res)) { var_dump ($row); } ?> [derick@kossu derick]$ php -v PHP 4.3.0-dev (cli) (built: Nov 19 2002 21:39:05) Copyright (c) 1997-2002 The PHP Group Zend Engine v1.3.0, Copyright (c) 1998-2002 Zend Technologies [derick@kossu derick]$ php bug20510.php array(2) { [0]=> string(15) "test " [1]=> string(15) "join one " } array(2) { [0]=> string(15) "test " [1]=> string(15) "join two " } Compiled against the bundled library, using mysql 3.23.49a as server. Which versions do you use? DerickHmm...wierd. Here's my info. Same problem happens with php 4.2.1 with MySQL compiled as dynamic module. <jrust:rhun dir="jrust"/>$ /usr/local/bin/php -v PHP 4.3.0-pre2 (cli), Copyright (c) 1997-2002 The PHP Group Zend Engine v1.3.0, Copyright (c) 1998-2002 Zend Technologies <jrust:rhun dir="jrust"/>$ /usr/local/bin/php -m [PHP Modules] xml tokenizer standard session posix pcre overload mysql mbstring ctype [Zend Modules] <jrust:rhun dir="jrust"/>$ rpm -qa|grep mysql mysqlclient9-3.23.22-6 mysql-3.23.41-1 mysql-server-3.23.41-1 mysql-devel-3.23.41-1 <jrust:rhun dir="jrust"/>$ /usr/local/bin/php foo.php array(2) { [0]=> string(4) "test" [1]=> string(8) "join two" } array(2) { [0]=> string(4) "test" [1]=> string(8) "join one" } Data without ORDER BY: array(2) { [0]=> string(15) "test " [1]=> string(15) "join one " } array(2) { [0]=> string(15) "test " [1]=> string(15) "join two " }