Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The order of columns is dictated by the order of the select query. </p> <pre><code>SELECT tRelations.ID,tRelations.DATA_RECORD_UUID,tRelations.RECEIVER_ID, tRelations.IS_TEST,tRelations.STATE,tRelations.UNIQUE_ID,tRelations.LAST_MODIFIED, tRelations.CREATED FROM tRelations </code></pre> <p>It says: ID, DATA_RECORD_UUID, RECEIVER_ID, IS_TEST, STATE, UNIQUE_ID and it will be also added in that way to the array. You can reorder it to </p> <pre><code>SELECT tRelations.STATE,tRelations.RECEIVER_ID,tRelations.LAST_MODIFIED, tRelations.CREATED,tRelations.UNIQUE_ID,tRelations.DATA_RECORD_UUID,tRelations.ID, tRelations.IS_TEST FROM tRelations </code></pre> <p>But it will be hard to get the other fields in that order. You you'll probably have to fill them yourself unless you can use aliases like <code>tRelation.SOME_FIELD as '2'</code>.</p> <pre><code>public function getRowArray($mysql_query_result){ $order=array("STATE", "RECEIVER_ID", "LAST_MODIFIED", "CREATED", "UNIQUE_ID", "3", ....); while($row=mysql_fetch_array($mysql_query_result)){ $newRow = array(); foreach($order as $value) { $newRow[$value] = $row[$value]; } $result[]=$newRow; } return $result; } </code></pre> <p>This will actually loop row by row and order it in your desired column order.</p> <p>On a side note: </p> <p>Your code is pretty dangerous and vulnerable for SQL Injections. You shall never directly put the content of a variable inside a SQL query! </p> <pre><code>$user_id = "Somestring with spaces"; $last_sync = "2013-08-07 15:51:25"; $ary=mysql_query("SELECT tRelations.ID,tRelations.DATA_RECORD_UUID,tRelations.RECEIVER_ID, tRelations.IS_TEST,tRelations.STATE,tRelations.UNIQUE_ID,tRelations.LAST_MODIFIED, tRelations.CREATED FROM tRelations JOIN tUserData ON (tUserData.UNIQUE_ID=tRelations.DATA_RECORD_UUID) JOIN tUsers ON (tUsers.ID=tUserData.USER_ID) WHERE tUsers.ID=$user_id AND tRelations.last_modified&gt;'$last_sync' ORDER BY tRelations.ID ASC;") or die(mysql_error()); </code></pre> <p>This would result in a query of </p> <pre><code>SELECT tRelations.ID,tRelations.DATA_RECORD_UUID,tRelations.RECEIVER_ID, tRelations.IS_TEST,tRelations.STATE,tRelations.UNIQUE_ID,tRelations.LAST_MODIFIED, tRelations.CREATED FROM tRelations JOIN tUserData ON (tUserData.UNIQUE_ID=tRelations.DATA_RECORD_UUID) JOIN tUsers ON (tUsers.ID=tUserData.USER_ID) WHERE tUsers.ID=Somestring with spaces AND tRelations.last_modified&gt;'2013-08-07 15:51:25' ORDER BY tRelations.ID ASC;" </code></pre> <p>You see the flaw? That's because "$user_id" wasn't escaped. You should <strong>always</strong> use <a href="http://php.net/manual/en/function.mysql-escape-string.php" rel="nofollow">mysqli_escape_string</a> to escape your string. For example: </p> <pre><code>$ary=mysql_query("SELECT tRelations.ID,tRelations.DATA_RECORD_UUID,tRelations.RECEIVER_ID, tRelations.IS_TEST,tRelations.STATE,tRelations.UNIQUE_ID,tRelations.LAST_MODIFIED, tRelations.CREATED FROM tRelations JOIN tUserData ON (tUserData.UNIQUE_ID=tRelations.DATA_RECORD_UUID) JOIN tUsers ON (tUsers.ID=tUserData.USER_ID) WHERE tUsers.ID=".mysqli_escape_string($user_id)." AND tRelations.last_modified&gt;'".mysqli_escape_string($last_sync)."' ORDER BY tRelations.ID ASC;") or die(mysql_error()); </code></pre> <p>Or even better: Use PDO (PHP Data Objects) when interacting with database. PDO is supported since PHP 5.1 and allows safe interaction with the database. It requires a little bit more code, but significantly improves the readability and security of your PHP application. </p>
    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.
    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