Jump to content


Photo

Sub Strings


  • Please log in to reply
3 replies to this topic

#1 jrodd32

jrodd32
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 18 September 2006 - 06:44 PM

in php if my code was:

substr(\"T\",49,1);

would that return a logical value T or F if the letter T was in the 49th position of the string with the one representing the number of values you want printed.

If so, is the syntax in mysql the same?


#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 18 September 2006 - 06:55 PM

to get a logical value, you'd have to run a comparison of some sort. substr() actually returns a string:
<?php
if (substr(49,1) == 'T') {
  // 49th position is 'T'
}
?>

it would be the same idea in mysql - the function returns a string, so you would then have to compare that value to something to get a logical return.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 jrodd32

jrodd32
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 18 September 2006 - 08:47 PM

function init_officials($sport)
{
  if (!($db = @mysql_connect('localhost', 'username','password')))
  {;
    print 'Temporarily unable to connect to database server. Please try again later.';
    exit;
  }
  mysql_select_db('Scoreboard',$db);
  global $officials;
  $officials=mysql_fetch_array(mysql_query("SELECT officials_complete.* FROM officials_complete where CURRENT=\"TRUE\""));
  global $officialslist;
  $officialslist=mysql_query("IF $sport1==\"T\") THEN SELECT lname, fname, address, city, st, zip, zip4, homephone, workphone, email FROM officials_complete WHERE CURRENT=\"TRUE\" ORDER by lname");
  print mysql_error();
}

Originally the $sport will come in as a variable within the database that I have assigned through a drop down list of options.
EX.) BA or BK or FB

This is to allow me to be able to sort the officials by which sports they are qualified to work. 
In the database the variables BA or BK etc. are strings which are 50 places long.
If the last place in the string is a 'T' then the official is cleared to work.
When, I echo $sport, after the mysql_query of the database it still returns as BA,etc.
Thus, when I try:
$sport1=substr($sport,49,1);
and echo $sport1;
I get BA9.
There is not a single 9 within the database.

What does this mean?


#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 19 September 2006 - 12:35 AM

you've hit an overflow somehow. if you're simply trying to see whether or not the last character of a string is a 'T' or not, just use the '-1' of substring to pull that character:
<?php
$string = 'BA';
echo substr($string, -1); // echos 'A'

$string = 'BASDJIBKEMS<>MBOSJEM';
echo substr($string, -1); // echos 'M'
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users