on 2014 Apr 09 12:55 PM
Hello,
I'm using Sybase SQL Anywhere sasql_* PHP functions in order to connect to a Sybase database.
I'm trying to launch this query:
$query = "SELECT contract_head.contract_number FROM contract_head WHERE contract_head.year = '2014'";
If I use this code everyting works just fine (I get 152 results):
$results = sasql_query($db, $query); // $db is the connection handler previously created using sasql_connect(); while( $res = sasql_fetch_array($results, SASQL_ASSOC) ) { // use $res to read the results }
Let's say I want to use prepared statements:
$a = sasql_prepare($db, "SELECT contract_head.contract_number FROM contract_head WHERE contract_head.year = ? "); echo '$a: '; var_dump($a); $year = 2014; $b = sasql_stmt_bind_param($a, 'i', $year); // if I use 's' nothing changes echo '$b: '; var_dump($b); $e = sasql_stmt_execute($a); echo '$e: '; var_dump($e); $i = sasql_stmt_num_rows($a); echo '$i: '; var_dump($i); $n = sasql_stmt_next_result($a); echo '$n: '; var_dump($n); $r = sasql_stmt_result_metadata($a); echo '$r: '; var_dump($r); var_dump($r);
What I see on my browser after executing this code is:
$a: resource(3) of type (SQLAnywhere statement) $e: bool(true) $i: int(0) $n: bool(false) $r: bool(false) bool(false)
What should I check in order to make prepared statements work?
Here's an example that might help. It's similar to what you want to do.
<?php
$conn = sasql_connect("Server=demo;UID=DBA;PWD=sql;LOG=C:\\Temp\\MyClient.Log");
$stmt = sasql_prepare($conn, "SELECT ID,Name,Description FROM Products WHERE ID > ? ");
$year = 500;
$b = sasql_stmt_bind_param($stmt, 'i', $year);
$num_params = sasql_stmt_param_count($stmt);
echo "Number of params = $num_params\\n";
$b = sasql_stmt_execute($stmt);
# // one way to fetch the result set
# // for query "SELECT Name FROM Products WHERE ID > ? "
# $b = sasql_stmt_bind_result($stmt, $name );
# while( sasql_stmt_fetch( $stmt ) )
# {
# echo "Fetched: $name\\n";
# }
# // a second, more general, method
$res = sasql_stmt_result_metadata($stmt);
$num_cols = sasql_num_fields($res);
echo "Number of cols = $num_cols\\n";
$num_rows = sasql_num_rows($res);
echo "Number of rows = $num_rows\\n";
$curr_row = 0;
echo "Fetching rows:\\n";
while( ($row = sasql_fetch_row( $res )) )
{
echo "FetchedRow:\\n";
$curr_row++;
$curr_col = 0;
while( $curr_col < $num_cols ) {
echo "$row[$curr_col]\\t|";
$curr_col++;
}
echo "\\n";
}
# Clean up.
sasql_free_result( $res );
sasql_disconnect( $conn );
?>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@JBSchueler Thank you very much! Everything works now. I prefer using sasql_fetch_assoc instead of sasql_fetch_row, so that I can obtain an associative array in $row with the tables fields as keys (it's more confortable). Just one more question: in most databases (MSSSQL, Postgre, MySql, etc...) I can use prep stmts with labels instead of "?" question marks (ie.: "....WHERE id = :productID"). Then I bind every param like for example: stmt_bind(':productID', $_POST['product_id'], SQL_INT); Is there a way to obtain the same behaviour also on SQLAnywhere? Thank you again very very much!
User | Count |
---|---|
67 | |
8 | |
8 | |
6 | |
6 | |
6 | |
6 | |
6 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.