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);

asked 12 Dec '18, 07:23

cbsa's gravatar image

cbsa
51226
accept rate: 0%

edited 12 Dec '18, 07:30


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);
}
permanent link

answered 14 Dec '18, 13:38

Mohammed%20Abouzour's gravatar image

Mohammed Abo...
91167
accept rate: 42%

edited 15 Dec '18, 08:04

1

Solved problem, tks (:

(17 Dec '18, 05:02) cbsa
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×236
×55
×15

question asked: 12 Dec '18, 07:23

question was seen: 89 times

last updated: 17 Dec '18, 05:02