Forum Moderators: coopster

Message Too Old, No Replies

Function result in SQL string query

         

DickWeed

7:13 am on Jan 10, 2010 (gmt 0)

10+ Year Member




function GenUID(){
$oid = . $iLastID();
if ($oid < 10)
$oid = '00' . $oid;
else if ($oid < 100)
$oid = '0' . $oid;

return 'ODG_' . $oid;
}

Trying to include this result in a string I'm passing to mysql_query

What way will this work?

e.g What I've tried....


$Gen_UID = GenUID()

$query = "INSERT into `my_table_name`(
id_with_prefix,
other_column)
VALUES ('"
. $Gen_UID . "','"
. $_POST['created_by_id'] . "')";

mysql_query($query)or die(mysql_error());

Can't seem to get the funcion to fire correctly.... All on the same *.php file. SQL qill fire with no problems..... as long I as leave the function out.

Is there another way to do what I'm trying to do with the fuction thats short and sweet? I can do other ways but its sloppy.

Have also tried putting the funtions inline... Can't remember how to use $this--> but I think that might be my solution.

Anyone?

Thanks in advance!

TheMadScientist

7:23 am on Jan 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Have you tried just changing your DB col to zero fill and not worrying about it in PHP?
Make sure it's a numeric col type...

MySQL Zero Fill [dev.mysql.com]

DickWeed

7:32 am on Jan 10, 2010 (gmt 0)

10+ Year Member



Can't..... the column I'm trying to update is text utf8 and contains modified UIDs from other tables to be used for content. Legacy app that I'm working with. related object id and the respective table names are in other columns.

I just need to evaluate PKs from tables and put a prefix in front of it.... i.e. uid 7 i need to make ODG_007 for the utf8 text. uid 77 i would need to make ODG_077

Again legacy app I'm trying to integrate.

BTW.... thanks for your help the other day. Worked like a charm.

TheMadScientist

8:34 am on Jan 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yeah, I figured you might not be able to, but didn't feel like coming back and editing the post so I figured I'd wait... NP on the help the other day. It's a nice break from my own projects that are a little bit more 'intensive' than a few minutes here thinking about a different challenge for a few minutes.

Have you tried concatenating the whole thing on each line?

function GenUID(){
$oid = . $iLastID();
if ($oid < 10)
$oid = 'ODG_' . '00' . $oid;
else if ($oid < 100)
$oid = 'ODG_' . '0' . $oid;

return $oid;
}

DickWeed

5:47 pm on Jan 10, 2010 (gmt 0)

10+ Year Member



That kind of works..... I'm missing something though...
I've changed the code up a little....

FYI.... after beating my head against the wall for a little bit... I realized I was trying to call the result of a function that didn't exist.

FIrst and formost.... $iListID is defined a ways back in the code by
$iListID = mysql_insert_id();

Its returned as an int no?

Thats my problem now. I've tried the following which will complete but only returns OGG_00. What I'm trying to accomplish is ODG_001 or ODG_010 depding on the evaluation of $iListID

function GenUID(){
$oid = $iListID;
if ($oid < 10)
$oid = 'ODG_' . '00' . $oid;
else if ($oid < 100)
$oid = 'ODG_' . '0' . $oid;

return $oid;
}

SO...... I was thinking its a format issue and I have to cast it. I've failed with all of the following solutions.

1st Tried to cast $iListID as $oid = number_format($iListID);
2nd Tried to cast $iListID as $oid = is_numeric($iListID);
3rd Tried to create an actual function as I initial was writting it:

That failure was something like this:
function GetOID(){
return $iListID;
}

function GenUID(){
$oid = GetOID();
if ($oid < 10)
$oid = 'ODG_' . '00' . $oid;
else if ($oid < 100)
$oid = 'ODG_' . '0' . $oid;

return $oid;
}

Any suggestions?

DickWeed

5:49 pm on Jan 10, 2010 (gmt 0)

10+ Year Member



Oh yeah.... I also tried to cast it as a strval too

TheMadScientist

8:25 pm on Jan 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If $iListID is already defined, you don't need the second function, you have to define $iListID as global. Sorry I didn't notice before...

function GenUID(){
global $iLastID;
$oid = $iLastID;
if ($oid < 10)
$oid = 'ODG_' . '00' . $oid;
else if ($oid < 100)
$oid = 'ODG_' . '0' . $oid;

return $oid;
}