Jump to content

Archived

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

jrodd32

Sub Strings

Recommended Posts

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?

Share this post


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

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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:
[code]
<?php
$string = 'BA';
echo substr($string, -1); // echos 'A'

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

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.