get value from stored procedure in php - php

This is my SP:
DELIMITER $$
CREATE DEFINER=FUNCTION `test`(p_begin varchar(10),p_end varchar(10),p_code varchar(2)) RETURNS varchar(10) CHARSET latin1
BEGIN
DECLARE V_ADA VARCHAR(8);
DECLARE V_LAST VARCHAR(8);
DECLARE V_NIK VARCHAR(8);
select NIK INTO V_NIK from absen where join_date >= p_begin and join_date<= p_end AND company_id=p_code ORDER BY NIK DESC LIMIT 1 ;
SET V_NIK=V_NIK+1;
return V_NIK;
END
I am trying to get the return value with php:
$query=$this->db->query("select test(\"$begin\",\"$end\", \"$code\")")->result();
var_dump($query);
die;
The result is:
array(1) { [0]=> object(stdClass)#20 (1) { ["test("2007-01-01","2007-12-31", "1")"]=> string(7) "118" } }
My problem is that I want to get the value from stored procedure ("118") and I don't know how to change the object into a string.

Try this:
$query=$this->db->query("select test(\"$begin\",\"$end\", \"$code\")")->result();
// Cast object to array (also you can try get_object_vars() to do that)
$query = (array)$query[0];
// Get the last value from array (it the only on so it is OK)
echo end($query);
Perhaps will help..

If you need to send the object as text to somewhere else (database, Javascript code), you have a choice of serialize, json_encode, XML conversion, and many more, depending on your exact situation.

Related

mySQL min() and max() query array echoing zero

I have a table set up with 5 columns:
user_id, 5k, 10k, halfmarathon, and marathon.
I want to display the user's best times on a user page for each distance of run. The user will be able to update any of the times which creates a new row and the rest of the columns are set to null. So for example,
Row 1 is:
user_id: 5, 5k: null, 10k: 45:00, half: null, marathon: null.
Then the user runs another 10k and gets a better time, plus wants to update their 5k time :
Row 2 is then:
user_id: 5, 5k: 15:53, 10k: 40:40, half: null, marathon: null.
When I run the following SQL query
$query = "SELECT MIN(5k), MIN(10k), MIN(halfmartahon), MIN(marathon)
FROM Times
WHERE user_id = ".$userID."
GROUP BY user_id";
$db->query($query);
//Assign Result Set
$user_benchmarks = $db->single();`
I get an array that is correct when I vardump() (I am storing the times in seconds) :
object(stdClass)#18 (4) { ["MIN(5k)"]=> string(3) "953" ["MIN(10k)"]=> string(4) "2440" ["MIN(halfmarathon)"]=> string(1) "0" ["MIN(marathon)"]=> string(1) "0" }
However, when I try to echo this, so $user_benchmarks->5k it doesn't show anything and when I run print_r($user_benchmarks->5k) it comes back as NULL.
Has anyone encountered this / know what's happening? I've also tried turning the string to an integer before printing it to no avail - still get NULL.
var_dump already showed you exactly what keys to use:
object(stdClass)#18 (4) { ["MIN(5k)"]=> string(3) "953"
^^^^^^^
so why are you using
$user_benchmarks->5k
^^^
?
It's because the "5k" property of the $user_benchmarks object doesn't exist. You need to access the "MIN(5k)" property instead. So for example:
echo $user_benchmarks->{"MIN(5k)"};
On the other hand you can change the query to something like this:
SELECT MIN(5k) AS `5k`, MIN(10k) AS `10k` ...
Then you will be able to access properties "5k" and "10k" just like you wanted.
Use
SELECT MIN(5k) as 5k,
MIN(10k) as 10k,
MIN(halfmartahon) as halfmartahon,
MIN(marathon) as marathon

select query of sphinxql returns everything in string irrespective of data type in table

I wrote my config file as attribute as -:
sql_attr_float = rad_latitude
sql_attr_float = rad_longitude
but on a select query i get the data as
array(2) {
["rad_latitude"]=>
string(9) "-0.371600" //this needs to be float
["rad_longitude"]=>
string(9) "-0.878434" //this needs to be float
}
it comes out as a string and i cant calculate geodist as it's in string.
sphinxql's execute function returns the data in string but you can use it in geodist query as it is. just remember not to use quotes on the columns name in your query, if you put quotes over it sphinx treats it as a string and i wont produce any result.

PHP - Extract value from query array

Do you have to use a loop to extract the value from a query array.
Query:
$fetchRegion = Singlequery("SELECT region FROM regions WHERE id = :id LIMIT 1",
array('id' => $_GET['region']),
$conn);
This is my array:
array(1) {
[0]=>
array(1) {
["region"]=>
string(10) "South West"
}
}
I want to take the value and use it in another query, I didn't know if I had to use a foreach for example to get the value to use in my next query. The other stackoverflow questions that I saw used a loop
If i understand your question correctly and you want to acces to value then access it like so:
$fetchRegion[0]['region'];
You don't need to use foreach or any other loop since it will return at most one element because LIMIT 1 you used in query.
Using loop :-
foreach($fetchRegion as $v) {
$var = $v["region"];
}
or you get directly like:-
echo $fetchRegion[0]["region"];
No, there is no need to use a loop with that query because it will not return more than a single row. Instead, just check that you got a row back (it could return no results), and then use it.
If you're certain that your result is going to look like that, reset(reset($fetchRegion)) will give you the value 'south west'. It will behave badly if you don't get that exact format back though - for example, if the query does not return a row.

return data from mysql and unserialize it

My SQL
$query = "SELECT value FROM oc_setting WHERE setting_id =7258";
$result = $this->db->query($query);
return $result->rows;
when I var_dump it show
array(1) {
[0]=>
array(1) {
["value"]=>
string(158) "a:1:{i:0;a:5:{s:8:"rss_link";s:11:"ddddddddddd";s:9:"layout_id";s:1:"2";s:8:"position";s:14:"content_bottom";s:6:"status";s:1:"1";s:10:"sort_order";s:1:"5";}}"
}
}
how can I unserialize array like this? cast to string first? I tried unserializ(var) but it say error coz it's an array.
PHP function unserialize (http://php.net/manual/en/function.unserialize.php) will help you.
unserialize($result[0]['value']);
will return you the array.
It seems you're using some PHP Framework (very similar to CodeIgniter one, but not sure).
There should be a function to retrieve exactly one (first) row from the result set.
Right now you're asking about a workaround which is not good. Usually DB drivers have a good interface to get how many rows/columns it took from query/view.
I usually use the following statement if I need go through the result array:
if ($result -> num_rows())
{
foreach ($result -> result_array() as $entry)
{
// do something
}
}

PHP MYSQL unserialize() issues

I have a "recruiter" table in my database which has different attributes and one of them is "Professions". "Professions" is a serialized array which I get from a multiple select form. And this works fine.
When I unserialize this attribute nothing is printed - no error, no text.
This is a code I was testing serialization with:
$sql = 'SELECT Company_name, Status, Size, Professions, Seniority_levels, Sector, Website, Location FROM Recruiter';
$query = mysql_query($sql, $con);
while($result = mysql_fetch_array($query, MYSQL_BOTH)){
$recruiters[] = array($result[0], $result[1], $result[2], $result[3], $result[4], $result[5], $result[6], $result[7]);
}
foreach($recruiters AS $recruiter){
$test = unserialize($recruiter[3]);
echo $test[0].'<br>';
}
So basically $test[0] prints nothing although the new lines are printed. Please help!
try printing the $test array and the $recruiters and the $recruiter arrays. See if the result is fine before the unserialisation of the data. If the query returns any data. Also try the while loop with mysql_fetch_assoc. Let me know of the results and if this solves the problem
test = unserialize($recruiter[3]); should become test = unserialize($recruiter[5]); since the sector field is the sixth column .
However what if somewhere in the future you might need to select rows where sectors equal smth ? serialize whont help you then so i suggest you have a look at a different implementation for the sector filed witch is called bitwize http://www.litfuel.net/tutorials/bitwise.htm
Edit
Asuming you hit the right column and the column contains a:1:{i:0;s:27: a:1:{i:0;s:27: a:38:{i:0;s:27: a:9:{i:0;s:39:, it looks like the serialized array is not fully saved in you're db, it's only part of it . So the unserialize function whont return you an array . Have a look at the length of the mysql field i assume you've set it smaller than you need so you're data is trimmed on insert/update .
Edit
a:1:{i:0;s:27: you're still missing the rest of the serialized array . s:27: means a string is following containint 27 characters, and you're serialized array stops there when it should look like
a:1:{i:0;s:27:"123456789012345678901234567";}
( a:1 stands for an array containing 1 value with it's content between {}, i:0; is the array key 0, s:27:""; stands for a string containing 27 characters as the value for the i:0 key ) .

Categories