Aureole Posted February 11, 2008 Share Posted February 11, 2008 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 More sharing options...
aschk Posted February 11, 2008 Share Posted February 11, 2008 Yeah where's {$cdata} coming from? Because the only scope it exists in is the compile_update_array function, NOT outside it. Link to comment https://forums.phpfreaks.com/topic/90516-function-isnt-doing-anything/#findComment-464060 Share on other sites More sharing options...
Aureole Posted February 11, 2008 Author Share Posted February 11, 2008 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. Link to comment https://forums.phpfreaks.com/topic/90516-function-isnt-doing-anything/#findComment-464062 Share on other sites More sharing options...
kenrbnsn Posted February 11, 2008 Share Posted February 11, 2008 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 Link to comment https://forums.phpfreaks.com/topic/90516-function-isnt-doing-anything/#findComment-464083 Share on other sites More sharing options...
Aureole Posted February 11, 2008 Author Share Posted February 11, 2008 I know not to echo from within a function, I was just doing that to test it. Thanks a lot for helping, I'll try your code now. Link to comment https://forums.phpfreaks.com/topic/90516-function-isnt-doing-anything/#findComment-464119 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.