I am having problems getting the return from the sasql_stmt_result_metadata statement, in cases where I have several different parameter types I have empty return. Currently using php 7.1.24 and sqlanywhere 16 Ex: $p = array( 'dbname' => 'mydbname', 'user' => 'userDB', 'password' => 'passDB', 'host' => '192.168.x.xxx', 'port' => 9999, 'driver' => 'sqlanywhere', ); $dsn = "HOST=%s:%u;DBN=%s;UID=%s;PWD=%s"; $conn = sasql_connect(sprintf($dsn, $p['host'], $p['port'], $p['dbname'], $p['user'], $p['password'])); sasql_set_option($conn, 'verbose_errors', false); sasql_set_option($conn, 'auto_commit', 'on'); $sql = "SELECT * FROM foo.tableSample WHERE (fieldInteger = ?) AND (fieldString = ?)"; $param = []; array_push($param, [1, 'i']); // If you use "s" get error ERRO: State 00000 - (Code: -157) Cannot convert 'sampleParamString' to a numeric array_push($param, ['sampleParamString', 's']); $stmt = sasql_prepare($conn, $sql); foreach ($param as $key => $field) { [$value, $type] = $field; sasql_stmt_bind_param_ex($stmt, $key, $value, $type, false); } try { if (!sasql_stmt_execute($stmt)) { $state = sasql_sqlstate($conn); $code = sasql_stmt_errno($stmt); $message = sasql_stmt_error($stmt); throw new \Exception(sprintf("ERRO: State %s - (Code: %s) %s", $state, $code, $message), 1); } $result = sasql_stmt_result_metadata($stmt); } catch (\Exception $e) { print_r($e); exit(); } $arr = []; while($row = sasql_fetch_array($result)) { array_push($arr, $row); } print_r($arr); |
The problem is with how you are using the sasql_stmt_bind_param_ex() function. When you call sasql_stmt_bind_param_ex() the variable $value is being bound by reference, however, as soon as you leave the scope of the foreach block, that variable is being destroyed. When you call sasql_stmt_execute() we will use whatever junk is sitting in memory at where the variable $value was sitting. That's why you will be getting incorrect results. Instead of copying the parameters in the $param variable to local variables, bind directly to them. Change your foreach loop as follows: foreach ($param as $key => $field) { sasql_stmt_bind_param_ex($stmt, $key, $param[$key][0], $param[$key][1], false); } |