sql query not working for stored procedure in PHP - php

I have a stored procedure in sql server.When I try to use the php function to get the results,its not working.
echo mssql_query(exec MOB_uspcommunication_details $userID,'send');
This gives result as 1 instead of resourceID.When I run the query in sql server its giving me the result.
When I change the second parameter to some other value like this,its working from PHP side.
echo mssql_query(exec MOB_uspcommunication_details $userID,'inbox');
This gives me resourceid#10 and I am getting the results.
For the first query,I am not able to understand why this happens.Please help me

From the PHP documentation:
Return Values
Returns a MS SQL result resource on success, TRUE if no rows were returned, or FALSE on error.
So that means that the first function call returns 1 to indicate there were no rows (1 == true in PHP). The second function call actually retrieves the rows from your table.

Related

why does my SQL execute(select exists) function always returns 1?

I can't seem to figure out what's wrong with this PHP code:
$sql = $db->prepare("SELECT EXISTS(SELECT 1 FROM profile WHERE uid = ?)");
$p_already_exists = $sql->execute([$_SESSION['uid']]);
I'm running the sqlite3 PDO module, and it doesn't matter whether the 'uid' is in the database or not, $p_already_exists is always assigned 1. I'm expecting for it to be 0 if it's not in the database, and 1 if there is at least one record in the database.
I've double checked that echoing out $_SESSION['uid'] gives me the same value to uid (TEXT) in the database.
Does anybody know why this isn't working for me? At the end of the day I'm just after an efficient way of returning a boolean value (hence why I'm not using COUNT). Appreciate your help.
ref How to check whether SELECT EXISTS returns a value or not?
PDOStatement::execute() returns a boolean value, TRUE on success or FALSE on failure:
https://www.php.net/manual/en/pdostatement.execute.php
If you want to get the actual result returned by the SQL statement execution, rather than the status of the execution itself, you should use one of the fetch* functions, for example fetchColumn(), after calling execute().

check if PDO query returns results

Assume I have this piece of code:
foreach($creds as $cred){
$prev_fut = $pdo->query("SELECT importo FROM incassi_prev WHERE
data_prev='$data_fut' AND incasso=0 AND
id_cre='{$cred['id_cre']}'")->fetch();
if(count($prev_fut)>0){
//I have found a result
}else{
//I have found nothing
}
}
NOTE: It is an internal query for my application with no data posted by user so I don't worry about SQL injections.
I use to check if count($prev_fut)>0 to see if the query is returning data (if I find any row in the db with these criterias).
My question is:
is this check enough to verify that the query has at least a result? Is it better to perform any other check?
The question is mostly coming from my thoughts about this being in a for loop and is related to the option of emptying/unset the $prev_fut array before starting a new iteration of for loop.
fetchColumn returns a single value of the next row in the result set. count counts the length of an array. Since fetchColumn can't return an array (for most database implementations), using count on it is wrong. You want to test whether $prev_fut is false or not. false would indicate that no result could be fetched, while anything else means a result was fetched:
if ($prev_fut !== false) ..
Having said that, you should really use a COUNT() in the database and check that value:
$result = $pdo->query('SELECT COUNT(*) FROM ...')->fetchColumn();
if ($result > 0) ..
It's much more efficient to have the database count and summarise the result than fetching an entire result set into PHP just for this simple check.

how to show procedure output in php

i am a php beginer. i want to call a mysql procedure and display the output rows returned by the procedure. the code is below
mysql
delimiter //
create procedure get_ques_list(in p_ques_ctgr varchar(10))
begin
select ques from question_list where ques_categorey=p_ques_ctgr
order by ques;
end //
delimiter ;
php code
$result=mysql_query("CALL GET_QUES_LIST('S_001')");
while($row=mysql_result($result,0,0))
{
echo $row['ques'];
}
but it is giving warning messgge
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in
but the same code works fine and display 5 rows if i use query instead of calling procedure
$result=mysql_query("select ques from question_list where
ques_categorey='S_001' order by ques");
procedure is also working fine when executed in mysql
can anyone tell where m going wrong in that??
any help is appreciated

Codeigniter Query Num Rows

I have the following code in a CI app :
$query = "INSERT INTO user(user_name,user_email) VALUES(?,?)";
$result = $this->db->query($query,array($userName,$email));
if ($result->num_rows()>0)
return $this->db->insert_id();
else
return null;
This yields an error ; Fatal error: Call to a member function num_rows() on a non-object in C:\xampp\htdocs\cmdline\application\models\mlogin.php on line 38
line 38 is the condition for number of rows is greater than 0 .
Now I don't get what is happening - I am using CI's documentation to check whether number of rows is greater than 0 , why does this fail ?
It's because you're doing an INSERT query. Queries that modify data return TRUE in all cases, so what you're really attempting is TRUE->num_rows(), which doesn't make any sense, causing an error.
What you actually want to check is $this->db->affected_rows(), which will tell you how many rows were inserted into the database.
Although you're not doing it in this case, it's worth noting that affected_rows() will only return the count of rows that actually changed during the query, so UPDATE queries that don't actually modify data (i.e. if the field already has the value the query is trying to set it to), then that row won't be reflected in the affected_rows() value.
From CodeIgniter's documentation.
The query() function returns a database result object when "read" type queries are run, which you can use to show your results. When "write" type queries are run it simply returns TRUE or FALSE depending on success or failure. When retrieving data you will typically assign the query to your own variable, like this:
Your query, because it's an INSERT will simply return TRUE or FALSE.

php mysqli to call stored proc, get get results

I have a stored proc that does a geospatial query. The proc issues two sql statements but only the 2nd one does a query but unfortunately both statements produce a result set. I need the second result set which contains the results of the actual query.
The first statement sets a bounding box:
SET #bbox = 'POLYGON(($polygon))'; \n
SELECT * , AsText( location )
FROM users
WHERE Intersects( location, GeomFromText( #bbox ) ) [snipped for brevity]
If I run the above in phpMyAdmin, it works but I get the following message AFTER the SET command is issued and I want to throw this away:
# MySQL returned an empty result set (i.e. zero rows).
On the php side, I build the query string, calling the stored proc and on return the first thing I do is throw away the empty result set.
$query = "CALL usp_queryByPolygon('$polygon', $msg_id, $user_type)";
$result = mysqli_query($cxn, $query) or die("GEOCODE: queryPolygon - " .sql_error());
sql_free_result($result);
After throwing away the result set I now need the results of the query and this is what I have done:
$result = sql_next_result();
The problem is when I try to use this second result set as in:
if(mysqli_num_rows($result) > 0)
I get errors:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given
in /blah/blah/module.php on line 96
To complicate things, all of the above is in a loop and there could be dozens or 100's of polygons to search.
So the question is this: what is the proper way to get that 2nd result set?
You'd better be accurate of what functions you execute. sql_next_result() is no standard PHP function, nor is it in MySQLi which you seem to use. If it's some kind of database class, please just show the methods that class uses. Nobody here can but quess what sql_next_result() does.
Assuming you're talking about mysqli_next_result(), that indeed returns a boolean, you need to call mysqli_use_result() after that in order to retreive the next result set.
Found out the two statements: SET #bbox and SELECT can be executed sequentially so mysqli and the two results are just fodder that don't need to be dealt with.

Categories