简体   繁体   中英

sqlsrv_query() expects parameter 1

I was trying to run the following query.

SELECT 
   min(rd.region) AS region, 
   min(ri.ri_id) AS ri_id, 
   ri.service_date, 
   min(ri.tt_route_start_time) AS tt_route_start_time, 
   min(ri.tt_route_end_time) AS tt_route_end_time, 
   min(ri.tgqh_exit_time) AS tgqh_exit_time, 
   min(ri.violation_vehicle_details) AS violation_vehicle_details, 
   min(ri.tt_sop_violation_start_route) AS tt_sop_violation_start_route, 
   min(ri.crew_violation_details) AS crew_violation_details, 
   rd.route_name, 
   vd.v_no, 
   min(ri.vehicle_violation_justify) AS vehicle_violation_justify, 
   min(ri.crew_violation_justify) AS crew_violation_justify, 
   min(ri.start_time_violation_justify) AS start_time_violation_justify, 
   min(ri.v_justification) AS v_justification, 
   min(ri.c_justification) AS c_justification, 
   min(ri.st_justification) AS st_justification, 
   min(vd.v_type) AS v_type 
FROM vehicles3.ride_details AS ri 
INNER JOIN vehicles3.route_details AS rd ON ri.route_id = rd.r_id 
INNER JOIN vehicles3.vehicle_details AS vd ON vd.v_id = ri.v_id 
WHERE 
   ri.status = 1 AND 
   rd.r_id = '93'AND 
   ri.service_date = '2019-10-14' 
GROUP BY ri.service_date, rd.route_name, vd.v_no, rd.r_id 
ORDER BY ri.service_date, rd.route_name, vd.v_no, rd.r_id

But when I run it shows the following errors:

Warning: sqlsrv_query() expects parameter 1 to be resource,

Warning: sqlsrv_num_rows() expects parameter 1 to be resource

But, when I echoed run the query and run it in SQL Server it works perfectly.

expects parameter 1 to be resource means that you are passing to that function a null resource, maybe because the connection to database failed.

The same issue about the sqlsrv_num_rows error.

You must check for the connection status before performing queries.

From the official documentation :

$serverName = "serverName\sqlexpress";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password" );
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
     die( print_r( sqlsrv_errors(), true));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM