Jump to content

PHP alphabetical problem!!


asna90

Recommended Posts

can anyone help me, right now i' having a problem to do sql for retrieving data based on first character that user input. anyone have a solution?

 

mysql_select_db($database_COS, $COS);

$query_Recordset2 = sprintf("SELECT ast_formID, ast_model, ast_ownName, ast_ownDept, stf_id FROM asset WHERE ast_ownName LIKE '$colname_Recordset2%' ORDER BY ast_ownName ASC", GetSQLValueString($colname_Recordset2 . "%", "text"));

$query_limit_Recordset2 = sprintf("%s LIMIT %d, %d", $query_Recordset2, $startRow_Recordset2, $maxRows_Recordset2);

$Recordset2 = mysql_query($query_limit_Recordset2, $COS) or die(mysql_error());

$row_Recordset2 = mysql_fetch_assoc($Recordset2);

 

if (isset($_GET['totalRows_Recordset2'])) {

  $totalRows_Recordset2 = $_GET['totalRows_Recordset2'];

} else {

  $all_Recordset2 = mysql_query($query_Recordset2);

  $totalRows_Recordset2 = mysql_num_rows($all_Recordset2);

}

$totalPages_Recordset2 = ceil($totalRows_Recordset2/$maxRows_Recordset2)-1;

}

 

how exactly the sql for detecting first character in a string that user enter and match it with data in the database ?

 

please help me !  :-[

Link to comment
Share on other sites

Your queries are not what you think they are. When having problems with queries that are dynamically generated it helps if you echo them to the page and then the problems will be easier to spot. Also, it makes it MUCH easier to see these types of errors if you indent the lines of code based upon the logical structure.

 

Here is what you currently have:

    $query_Recordset2 = sprintf("SELECT ast_formID, ast_model, ast_ownName, ast_ownDept, stf_id
                                 FROM asset
                                 WHERE ast_ownName LIKE '$colname_Recordset2%'
                                 ORDER BY ast_ownName ASC",
                                 GetSQLValueString($colname_Recordset2 . "%", "text")
                                );

 

You are using sprintf() with a formatted string (the query) and one argument. But, the arguments are used to replace placeholders in the formatted string. Your formatted string has no placeholders. Instead you are setting the latter to match as "$colname_Recordset"!.

 

I don't know what value is the first letter of the user entered value of what the function GetSQLValueString() is supposed to do. The logic makes no sense to me, but I think your query builder should look like this

    mysql_select_db($database_COS, $COS);

    $query_rs2 = sprintf("SELECT ast_formID, ast_model, ast_ownName, ast_ownDept, stf_id
                          FROM asset
                          WHERE ast_ownName LIKE '%s'
                          ORDER BY ast_ownName ASC",
                          GetSQLValueString($FIRST_LETTER_OF_USER_ENTERED_VALUE . "%", "text")
                         );
    $query_rs2_limit = sprintf("%s LIMIT %d, %d",
                                $startRow_Recordset2,
                                $maxRows_Recordset2
                               );

    $Recordset2 = mysql_query($query_rs2_limit, $COS) or die(mysql_error());
    $row_Recordset2 = mysql_fetch_assoc($Recordset2);

    if (isset($_GET['totalRows_Recordset2']))
    {
        $totalRows_Recordset2 = $_GET['totalRows_Recordset2'];
    }
    else
    {
        $all_Recordset2 = mysql_query($query_rs2);
        $totalRows_Recordset2 = mysql_num_rows($all_Recordset2);
    }

$totalPages_Recordset2 = ceil($totalRows_Recordset2/$maxRows_Recordset2)-1;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.