Jump to content

Function isn't doing anything


Aureole

Recommended Posts

Does anyone have any idea why this isn't working? I'm stumped.

 

The output is:

 

UPDATE `members` SET WHERE `mem_id` = '1'

 

When it should be a little more like:

 

UPDATE `members` SET `mem_title` = 'Testing...', `mem_profile_message` = 'Testing...' WHERE `mem_id` = '1'

<?php
function quick_update( $table, $array, $where = '' )
{
    compile_update_array( $array );
    $query = "UPDATE `{$table}` SET {$cdata}";

    if( $where !== '' )
    {
        $where = explode( '|', $where );

        $query .= " WHERE `{$where[0]}` = '{$where[1]}'";
    }

    echo( $query );
    exit;
}

function compile_update_array( $data )
{
    $cdata = '';

    foreach( $data as $k => $v )
    {
        if( is_numeric( $v ) and intval( $v ) == $v )
        {
            $cdata .= '`' . $k . '`' . " = " . $v . ",";
        }
        else
        {
            $cdata .=  '`' . $k . '`' . " = '" . $v . "',";
        }
    }

    $cdata = preg_replace( "/,$/", "", $cdata );

    return $cdata;
}

quick_update( 'members', array( 'mem_title' => 'Testing...', 'mem_profile_message' => 'Testing...' ), 'mem_id|1' );
?>

Link to comment
https://forums.phpfreaks.com/topic/90516-function-isnt-doing-anything/
Share on other sites

I also tried this and it didn't work, so that's not it:

 

$whatever = compile_update_array( $array );
$query = "UPDATE `{$table}` SET {$whatever}";

 

You are right about the scope, I forgot about that... but even when I assign the result of compile_update_array() to a variable inside the other function, then use it - it still doesn't work.

I made a few changes to your code:

<?php
function quick_update( $table, $array, $where = '' )
{
    $cdata = compile_update_array( $array );
    $query = "UPDATE `$table` SET $cdata";

    if( $where !== '' )
    {
        $where = explode( '|', $where );

        $query .= " WHERE `$where[0]` = '$where[1]'";
    }

    return( $query );
}

function compile_update_array( $data )
{
    $cdata = '';

    foreach( $data as $k => $v )
    {
        if( is_numeric( $v ) and intval( $v ) == $v )
        {
            $cdata .= '`' . $k . '`' . " = " . $v . ",";
        }
        else
        {
            $cdata .=  '`' . $k . '`' . " = '" . $v . "',";
        }
    }

    $cdata = preg_replace( "/,$/", "", $cdata );

    return $cdata;
}

$query = quick_update( 'members', array( 'mem_title' => 'Testing...', 'mem_profile_message' => 'Testing...' ), 'mem_id|1' );
echo $query . "<br>\n";
?>

 

You should always return values from a function. Using echo from within the function can cause output to placed in places you don't want it.

 

I got rid of the "{ }" where they were not needed.

 

When I tested my script, it seemed to work correctly.

 

Ken

Archived

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

×
×
  • 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.