Jump to content

SQL Query


Glenn

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

Link to comment
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 :)

Link to comment
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.

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.