Note that there are some explanatory texts on larger screens.

plurals
  1. POMysql field value is truncated when ordered in PHP
    primarykey
    data
    text
    <p><strong>update</strong> seems to be an issue w/ mysql because it truncates it from the command line too</p> <p>anyway a db admin at my work figured it out.. see my solution</p> <p>In PHP (PDO, php_mysql, and php_mysqli all tested) I have a query using group_concat. When it is ordered the data in that field is truncated.</p> <p>The queries are identical except for that in the second one I have commented out the order by statement. I added the where clause so it would only fetch a few records. Interestingly the data will not be truncated if you select EXACTLY one record with the order by statement as in ID=130... but if you select it as a range it truncates it ID &lt; 131 and ID > 129</p> <p>The example is using PDO although it doesn't seem to matter what driver i use</p> <p><strong>Note: I tried CAST as char(1000) and that doesn't work</strong></p> <p>Here is my example code and the output</p> <pre><code>&lt;?php define('AREA',''); include ("phoenix/includes/config.php"); $config =&amp; PhoenixConfig::get_instance(); $conn=$config-&gt;dbh; //a PDO connection $truncated="SELECT `ac_Export`.`ID` AS 'db-ac_Export-ID', `ac_Export`.`RunDate` AS 'db-ac_Export-RunDate', `ac_Export`.`Result` AS 'db-ac_Export-Result', `ac_Export`.`LogFile` AS 'db-ac_Export-LogFile', `ac_Export`.`Type` AS 'db-ac_Export-Type', `ac_Export`.`cscart_users_ID` AS 'db-ac_Export-cscart_users_ID', `ac_Export`.`cscart_user_name` AS 'db-ac_Export-cscart_user_name', GROUP_CONCAT( ac_Batch.BatchCode ORDER BY ac_Batch.BatchCode SEPARATOR ', ' ) AS 'db-ac_Export-AllIncludedBatches' FROM ac_Export LEFT JOIN ac_ExportBatch ON ac_Export.ID = ac_ExportBatch.ac_Export_ID LEFT JOIN ac_Batch ON ac_ExportBatch.ac_Batch_ID = ac_Batch.ID where ac_Export.ID &lt; 131 and ac_Export.ID &gt; 129 GROUP BY ac_Export.ID ORDER BY `db-ac_Export-RunDate` DESC LIMIT 0,100 "; echo "&lt;h3&gt;Truncated&lt;/h3&gt;"; $sql = $truncated; foreach ($conn-&gt;query($sql) as $row) { if ($row['db-ac_Export-ID']==130) { echo $row["db-ac_Export-AllIncludedBatches"]; echo "&lt;br&gt;"; } } $sql="SELECT `ac_Export`.`ID` AS 'db-ac_Export-ID', `ac_Export`.`RunDate` AS 'db-ac_Export-RunDate', `ac_Export`.`Result` AS 'db-ac_Export-Result', `ac_Export`.`LogFile` AS 'db-ac_Export-LogFile', `ac_Export`.`Type` AS 'db-ac_Export-Type', `ac_Export`.`cscart_users_ID` AS 'db-ac_Export-cscart_users_ID', `ac_Export`.`cscart_user_name` AS 'db-ac_Export-cscart_user_name', CAST(GROUP_CONCAT( ac_Batch.BatchCode ORDER BY ac_Batch.BatchCode SEPARATOR ', ' ) AS CHAR(1000)) AS 'db-ac_Export-AllIncludedBatches' FROM ac_Export LEFT JOIN ac_ExportBatch ON ac_Export.ID = ac_ExportBatch.ac_Export_ID LEFT JOIN ac_Batch ON ac_ExportBatch.ac_Batch_ID = ac_Batch.ID WHERE ac_Export.ID &lt; 131 and ac_Export.ID &gt; 129 GROUP BY ac_Export.ID /*ORDER BY `db-ac_Export-RunDate` DESC */ LIMIT 0, 100 "; echo "&lt;h3&gt;Not Truncated&lt;/h3&gt;"; foreach ($conn-&gt;query($sql) as $row) { echo $row["db-ac_Export-AllIncludedBatches"]; } ?&gt; </code></pre> <p>Output example:</p> <blockquote> <p>Truncated </p> <p>G2013050301, G2013050702, G2013052901, G2013053103, G2013060500, P2013050103, P2013050201, P2013050301, P2013050702, P2013050802, P2013050901, P2013051101, P2013051200, P2013051301, P2013051401, P2013051501, P2013051601, P2013051701, P2013052102, P2013052201, P2013052301, P2013052600, P2013052700, P2013052801, P2013052901, P2013053001, P20 </p> <p>Not Truncated </p> <p>G2013050301, G2013050702, G2013052901, G2013053103, G2013060500, P2013050103, P2013050201, P2013050301, P2013050702, P2013050802, P2013050901, P2013051101, P2013051200, P2013051301, P2013051401, P2013051501, P2013051601, P2013051701, P2013052102, P2013052201, P2013052301, P2013052600, P2013052700, P2013052801, P2013052901, P2013053001, P2013053102, P2013060500</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload