Glenn Posted November 14, 2003 Share Posted November 14, 2003 Hello, I am new to PHP and MYSql so this may seem like a very easy question. I\'m attempting to do a query. When I use the following code the query returns no results: $LastName = \"MILL%\"; mysql_select_db($database_UFAMySql, $UFAMySql); $SqlString = printf (\"SELECT * FROM u01_caspenrl WHERE CENLAST LIKE \'%s\'\",$LastName); $Recordset1 = mysql_query($SqlString, $UFAMySql) or die(mysql_error()); However, if I change the variable $SqlString to this, I get all the matching records. $SqlString = \"SELECT * FROM u01_caspenrl WHERE CENLAST LIKE \'MILL%\'\"; What am I missing? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/1366-sql-query/ Share on other sites More sharing options...
triphis Posted November 18, 2003 Share Posted November 18, 2003 Hmm, it appears that you are looking for last names like Mill%s: [php:1:07a6c03d8c]<?php $LastName = \"MILL%\"; mysql_select_db($database_UFAMySql, $UFAMySql); $SqlString = printf (\"SELECT * FROM u01_caspenrl WHERE CENLAST LIKE \'%s\'\",$LastName); $Recordset1 = mysql_query($SqlString, $UFAMySql) or die(mysql_error()); ?>[/php:1:07a6c03d8c] Your SqlString syntax is slightly off: [php:1:07a6c03d8c] $SqlString = printf (\"SELECT * FROM u01_caspenrl WHERE CENLAST LIKE \'%s\'\",$LastName); [/php:1:07a6c03d8c] The $lastname variable isn\'t even inside the command, and I have never seen anyone use the printf function that way before Also, I personally wouldn\'t have the \'%\' set in a variable. Look below. Try this instead: [php:1:07a6c03d8c] $SqlString=\"SELECT * FROM u01_caspenrl WHERE CENLAST LIKE \'$LastName%s\'\"; [/php:1:07a6c03d8c] In the case that you just want all names that begin with \'MILL\' as you have displayed in the code that works as you say, simply omit the \'S\' after the % in my query. That will do what I think it is you want... Mill___s. Reply again if that doesn\'t help, I\'ll be more than happy to try again Quote Link to comment https://forums.phpfreaks.com/topic/1366-sql-query/#findComment-4607 Share on other sites More sharing options...
Glenn Posted November 19, 2003 Author Share Posted November 19, 2003 Thanks for the help. I ended up using the method you suggested. As far as using printf to build the string, that was the method suggested by the TUTORIAL!!! I simplified the query to post on line. The original query was looking for a social security number and the last name and looked like this: $SqlString = printf(\"SELECT * FROM u01_caspenrl WHERE CENMSSN = \'%s\' AND CENLAST LIKE \'%s\'\",$Social,$LastName); When I displayed it on the screen it looked correct but wouldn\'t work. Any way thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/1366-sql-query/#findComment-4622 Share on other sites More sharing options...
triphis Posted November 19, 2003 Share Posted November 19, 2003 Oh, I\'m a moron XD I completely forgot about using the %s thing, and having variables outside >.> *DOH!* Anyway, glad I could help Quote Link to comment https://forums.phpfreaks.com/topic/1366-sql-query/#findComment-4623 Share on other sites More sharing options...
Barand Posted November 19, 2003 Share Posted November 19, 2003 When using printf, you need %% to use a percent othwise it thinks its a placeholder for a variable $SqlString = printf (\"SELECT * FROM u01_caspenrl WHERE CENLAST LIKE \'%s%%\'\",$LastName); Quote Link to comment https://forums.phpfreaks.com/topic/1366-sql-query/#findComment-4625 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.