1

first time requesting your help here.

I know my title is wrong, i will change it if once we figure what is the issue, at the moment im kinda lost.

Alright there we go first i wanna select all users from my database that are in range of the current user.

If i execute the query it returns 4 rows from PHPMyAdmin, oh i should mention im using MariaDB

Here is the query:

SET @orig_lat=45.820904;
SET @orig_lon=-73.760549;
SET @dist=1000 * 5;//5 KM
SET @accountId=9;

SELECT SQL_CALC_FOUND_ROWS *, (((acos(sin((@orig_lat*pi()/180)) * sin((account.latitude*pi()/180))+cos((@orig_lat*pi()/180))*cos((account.latitude*pi()/180))*cos(((@orig_lon-account.longitude)*pi()/180))))*180/pi())*60*1.1515*1609.344) AS distance FROM accounts AS account HAVING distance <= @dist AND account.id <> @accountId  ORDER BY distance ASC LIMIT 50 OFFSET 0;0

Now from C# here how it looks:

public static Dictionary<List<AccountData>, int> SearchAccounts(string accountId, double latitude, double longitude, int rangeInKM, int limit, int page = 1){
List<AccountData> returnList = new List<AccountData>();
int total_count = 0;
query = "SELECT SQL_CALC_FOUND_ROWS *, (((acos(sin((@orig_lat*pi()/180)) * sin((account.latitude*pi()/180))+cos((@orig_lat*pi()/180))*cos((account.latitude*pi()/180))*cos(((@orig_lon-account.longitude)*pi()/180))))*180/pi())*60*1.1515*1609.344) AS distance FROM accounts AS account HAVING distance <= @dist AND account.id <> @accountId ORDER BY distance ASC LIMIT @limit OFFSET @page;";
var rows = Database.ExecuteQuery(query,
                new QueryParameter("accountId", accountId),
                new QueryParameter("orig_lat", latitude),
                new QueryParameter("orig_lon", latitude),
                new QueryParameter("dist", 1000 * rangeInKM),
                new QueryParameter("limit", limit),
                new QueryParameter("page", page - 1));

The ExecuteQuery function code:

public override DatabaseRow[] ExecuteQuery (string query, params QueryParameter[] parameters)
    {
        try
        {
            using (MySqlConnection connection = new MySqlConnection (connectionString))
            {
                using (MySqlCommand command = new MySqlCommand (query, connection))
                {
                    foreach (QueryParameter parameter in parameters)
                        command.Parameters.AddWithValue(parameter.name, parameter.obj);

                    connection.Open();
                    using (MySqlDataReader reader = command.ExecuteReader())
                    {
                        int fieldCount = reader.FieldCount;
                        List<DatabaseRow> rows = new List<DatabaseRow> ();

                        while (reader.Read())
                        {
                            //For each row create a DatabaseRow
                            DatabaseRow row = new DatabaseRow();

                            //And add each field to it
                            for (int i = 0; i < fieldCount; i++)
                            {
                                row.Add(
                                    reader.GetName(i),
                                    reader.GetValue(i)
                                );
                            }

                            //Add it to the rows
                            rows.Add(row);
                        }

                        return rows.ToArray();
                    }
                }
            }
        }
        catch(MySqlException e)
        {
            throw new DatabaseException(e.Message, e);
        }
    }

Please note the loop while(reader.read()) ... is never 'triggered' as if they were no rows matching the query.

Now im kinda lost of what i should do to fix that issue, all help is welcome!

Thanks alot for your help :)

Sylvain Martens
  • 1,306
  • 1
  • 9
  • 14

1 Answers1

0

Alright i figured what was my issue, i was parsing the latitude as the longitude in the query parameters.

Im leaving it in case someone needs the code. Always double check your parameters ;)

Needed to change

var rows = Database.ExecuteQuery(query,
            new QueryParameter("accountId", accountId),
            new QueryParameter("orig_lat", latitude),
            new QueryParameter("orig_lon", latitude),
            new QueryParameter("dist", 1000 * rangeInKM),
            new QueryParameter("limit", limit),
            new QueryParameter("page", page - 1));

To

var rows = Database.ExecuteQuery(query,
            new QueryParameter("accountId", accountId),
            new QueryParameter("orig_lat", latitude),
            new QueryParameter("orig_lon", longitude),
            new QueryParameter("dist", 1000 * rangeInKM),
            new QueryParameter("limit", limit),
            new QueryParameter("page", page - 1));
Sylvain Martens
  • 1,306
  • 1
  • 9
  • 14