asna90 Posted March 19, 2012 Share Posted March 19, 2012 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 ! Quote Link to comment Share on other sites More sharing options...
dmikester1 Posted March 19, 2012 Share Posted March 19, 2012 Can we assume you know how to get that "first character" from an input box on a web page into a PHP variable? Or are you asking how to do that as well? Mike Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 19, 2012 Share Posted March 19, 2012 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; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.