how to call sybase view using odbc in PHP - php

I am trying to fetch the records from the sybase view ODBC in PHP but it does not return data.
I am getting records from table but not able to get data from view.
Query -- select * from odb.VP_myview;
odb = odbc DSN
VP_myview = view
Above query does not show any error but same time it does not return any records.
Below is PHP code
<?php
$con = #odbc_connect('DSN','USERNAME','PASSWORD',SQL_CUR_USE_ODBC) or die("<B>Error!</B> Couldn't Connect To Database. Error Code: ".odbc_error());
$query = 'select * from odb.VP_myview';
$result = odbc_exec($con,$query);
$totrec = odbc_num_rows($result);
var_dump($totrec);// it return negative integer number
exit;
?>
It would be really great help. I am trying since last 3-4 days but no success.

From the manual:
Using odbc_num_rows() to determine the number of rows available after
a SELECT will return -1 with many drivers.
That's especially true for Sybase. C.f. this question

Related

How to retrieve and display a COUNT query result in PHP?

I'm a beginner who has problems with PHP :(
I have a PHP function which shows all the rows from the database table. Now I have to create paging to show only limited number of rows per one page.
I have a problem with retrieving a COUNT result from query. I want to create a condition where PHP & MySQL use LIMIT if number of rows is bigger than needed on one page. The following code:
$count = "SELECT COUNT(*) FROM articles";
$countq = $db->query($count);
$countrs = mysql_fetch_array($countq);
echo $countrs;
should display a number of rows. However, it does not. What am I doing wrong? I want to see a result to make sure that everything else will work fine. But I can't get it working.
Error: mysql_fetch_array() expects parameter 1 to be resource, object given
$db contains database connection information (server, user...) and is working
Use PDO for MySQL query.
$db = new PDO('mysql:host=#YOUR HOST#;dbname=#YOUR DB#;charset=utf8', '#YOUR LOGIN#', '#YOUR PASSWORD#');
$query = $db->query('SELECT COUNT(*) AS count FROM articles');
$countq = $query->fetch();
$query->closeCursor();
echo $countq['count'];
I hope this will help you
You will have to set the limit in the query like
$count = "SELECT COUNT(*) FROM articles LIMIT 5,10";
where 5 is the starting point and 10 is the total number of results you want.
You mention: $db but not what $db is? i mean is it a database object class? this will work directly if you are using the a database class, and if that's the case the class will also have functions which will allow you to query data without using mysql_fetch_array (actually mysqli_fetch_array).

Not getting all rows from a query with odbc_exec in php

I'm trying to display how many id's does my procedure finds, but the variable $processz only got the first row of the sql result. It should display that there are 17 rows or id's, and only got 1. Why does it happends?
$conexion = con_abrir();
$sqlquery = "OEE.dbo.VerPlanillas_fechas '$Linea_ID','$fecha1','$fecha2'";
$processz = odbc_exec($conexion,$sqlquery);
con_cerrar($conexion);
$res = count($processz);
echo $res;
count ($processz) tells you how many results you have - one.
If you want to know how many rows are in the result, you need to call odbc_num_rows ($processz);
Look into using PDO rather than odbc specific functions.

SQLite Full-Text Search with PDO

I created the sqlite-FTS4 table with multiple columns such as:
CREATE VIRTUAL TABLE [selection_fts] USING fts4 (tender_id, tender_orderName_clean, tender_fm, branch, lot_name_clean);
and I try to execute code like
$q = $db->query("SELECT * FROM selection_fts WHERE selection_fts MATCH (\"(tender_orderName_clean:двер* OR lot_name_clean:двер*) AND tender_fm:Министерство Иностранных Дел AND branch:на строительство\") limit 0,250");
$rows = $q->fetchAll(PDO::FETCH_ASSOC);
var_dump($rows);
die();
The code above returns 0 rows, but in my SQlite manager I see some results.
Do you please explain my mistake?
Although this is an old question, I just ran into the same issue and found out that (atleast in my case) the exact MATCH to the database entry didn't return any results. When I took irrelevant parts out of the MATCH string, I got results back.
Might be a bug in the SQLite driver, maybe...

Query works on SQL Query Analyzer, but fails on PHP mssql_query()

I have a complex MSSQL query with multiple joins. When I query using SQL Query Analyzer, it returns results after consuming about 5 minutes. However, when I query using PHP mssql_query() function, it returns an error mssql_query() failed with no useful information.
Followings are the analysis of this problem so far.
Response time limit of php server is not a problem. Time limit is set to infinite. In fact, this same query returns results after about 30 seconds on different production servers.
I tried using mssql_get_last_message() to get more details on why it failed. But so far, no luck. All I get is "Changed database context to 'dbsdb'", where 'dbsdb' is the name of database I am querying.
This is the query I am using, if that matters.
SELECT L.[strStation], L.[nEntitlement1], L.[nEntitlement2], O.nOrders,
L.[nEntitlement3], L.[nEntitlement4], S.[strFirm], S.[nStatus],
S.[nFeatures], S.[nFeatures2], S.[nFeatures3], S.[strDescription]
FROM [tblLogin] AS L
LEFT JOIN [tblStation] AS S ON L.[strStation] = S.[strStation]
LEFT JOIN (
SELECT [strStation], COUNT(DISTINCT(nRecordId)) as nOrders
FROM [tblOrder]
WHERE [timeOrderDate] >= '2013-06-04' AND
[timeOrderDate] <= '2013-06-28'
GROUP BY strStation
) AS O ON O.[strStation] = S.[strStation]
WHERE L.[timeLoginTime] >= '2013-06-04 00:00:00' AND
L.[timeLoginTime] <= '2013-06-28 23:59:59' AND
S.[strFirm] in ('FIRMA','FIRMB')
This is the code that I use to query.
$link = mssql_connect('192.168.251.90', 'sa', '');
if (!mssql_select_db('dbsdb', $link))
var_dump(mssql_get_last_message());
$query = mssql_query($sql, $link);
EDIT I
I narrowed down to one spot. When I remove O.nOrders part from the SELECT clause, the query works perfectly fine.
But WHY???
EDIT II
This is driving me crazy. Query works sometimes even with O.nOrders part in SELECT clause. This seems totally random to me, but I know there is no such thing as real random in this world...there should be a reason...

How to check if table is empty , Execute and FetchRow methods in php and mysql

$RSGetID = $this->MyDBObject->Prepare("SELECT FinalID FROM clothes
WHERE ClothID=:|1 AND PriceID = :|2 LIMIT 1");
$RSGetID->Execute(2, 199);
$ClothIDRow = $RSGetID->FetchRow();
return $ClothIDRow->FinalID;
This last line gives an error, because there are no rows in the table, so it says:
"the query did not return any records"
How do I put a condition, that if the table is empty then return 0 , else return the fetched FinalID from the database table?
You're using some custom DB layer (MyDBObject?) rather than straight-up PDO - it's impossible for us to know how this behaves. There's probably a method along the lines of ->RowCount() or ->NumRows() you can call to see if you got anything back after the ->Execute() - but this is just guessing, since I can't see the DB object you're using.

Categories