Jump to content

Archived

This topic is now archived and is closed to further replies.

Glenn

SQL Query

Recommended Posts

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

Share this post


Link to post
Share on other sites

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 :)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Oh, I\'m a moron XD I completely forgot about using the %s thing, and having variables outside >.> *DOH!* Anyway, glad I could help :P

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites

×

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.