Jump to content


Photo

SQL Query


  • Please log in to reply
4 replies to this topic

#1 Glenn

Glenn
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 14 November 2003 - 03:18 PM

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

#2 triphis

triphis
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 18 November 2003 - 09:25 PM

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

#3 Glenn

Glenn
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 19 November 2003 - 12:28 PM

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.

#4 triphis

triphis
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 19 November 2003 - 03:49 PM

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
Xac Attack

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 19 November 2003 - 04:16 PM

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);
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users