How Can I refer to the Variable in PHP (pg_query)? - php

I want to insert a WKT format polygon to the PostGIS. It works with the PostGIS SQL. Now I want to use PHP call this function but it fails. It should be something wrong with the reference to the variable. My code is as following:
<?php
$data = A string format data;
$con="host=localhost dbname=database user=postgres password=great";
$dbcon= pg_connect($con);
$sql="INSERT INTO polygons(geometry) VALUES (ST_GeomFromText(('$data'))";
$result= pg_query($dbcon,$pgsql);
?>
There should be something wrong with the $data, it doesn't refer to my data. But I don't know how to fix it.

What every PHP user have to understand, that it is not into pg_query variable being inserted but into regular PHP string.
And thus it is very simple to verify if it was inserted or not - by just echoing the string out:
echo $sql;
This way you can see if it was inserted or not.
if it was - the problem somewhere else.
if won't - you have to verify variable itself.

Related

Retrieve value from array

While retrieving values from array without index in PHP I am getting nothing.
From my database value is stored like ["SCHOOL"] and I want to get just SCHOOL from this.
It's not treated as array instead it's treated as a string.
What will be the solution if want to treat this as array and get value of that array.
My Code is as follows :
$stream_arr = $res_tbl['streams'];
which gives result as ["SCHOOL"]
and I want just SCHOOL from this.
I am using code as $stream = $stream_arr[0];
and I get '[' this as result.
If I assign manual value to $stream_arr = ["SCHOOL"],
above code works and returns SCHOOL as expected.
But for results from my database is not working where result is same as the one I manually assigned.
To eliminate braces from the string you can use below code.
$stream_arr = '["SCHOOL"]';
$temp=explode('"',$stream_arr);
$val=$temp[1];
echo $val; //it will dispaly 'SCHOOL'.
But check the insert query(from the place you store data into db) that why it is storing with braces? if it is done by you as required then proceed else first fix that issue instead of eliminating braces in php code.

Using a php variable inside an sql query

In my php script,i am using a php variable inside an sql query.The php variable acquires its value through a post variable as follows:
$desc0=$_POST['desc0'];
$desc1=$_POST['desc1'];
$desc2=$_POST['desc2'];
$desc3=$_POST['desc3'];
$desc4=$_POST['desc4'];
$desc5=$_POST['desc5'];
$desc6=$_POST['desc6'];
$desc7=$_POST['desc7'];
$desc8=$_POST['desc8'];
$desc9=$_POST['desc9'];
The query is:
for($i=0;$i<10;$i++)
{
$q="insert into photos(name,category,description) values ('{$name{$i}}','$category','{$desc{$i}}')";
}
The problem is that on submitting the form i am getting an error which says
"undefined variable desc".
Therefore its not taking the values from the previously defined variables?
Any help?
First of, you code is completely unsafe - you should not pass user data directly into your query. There are many topics about it, and this is a good start.
Next, you don't need to store your data in such weird way. What if you'll want to pass 20 photos? In HTML, name your fields like photos[] - and in PHP, your values will be correctly parsed as an array $_POST['photos'], so you will be able to work with array:
$photos = $_POST['photos'];
foreach($photos as $photo)
{
//$photo contains certain item, so handle it with your logic
}
Finally, your issue is because of non-obvious PHP possibility for array-dereference with curly brackets. So your $desc{$i} is an attempt to access $i-th index for non-existent array $desc. Either use $desc$i or use concatenation to separate your variables.
You must change $desc{$i} to ${"desc" . $i}

php - get value from variable

I need to take value form variable, hode one part and send the other part in email.
This variable is sql anywhere query result.
This is what i have so far:
$res=sqlanywhere_query(...)
$resID=explode('#',$res);
$email.=$email_footer.$resID[1].$email_footer2;
When I had in email $res, in email I get something liike Resource #163.
When I put $resID[1], in place where should be 163, space was empty.
That is because your $res is a resource, you have to get the results.
You should have for that library something like
$sql = sqlanywhere_query(...)
$res = sqlanywhere_fetch($sql);
and $res will be an array with your query result;
It's a resource, which means that it's a special variable that holds a reference to an external source.
See the PHP manual on Resources.
please, using database with php. when you query, you must pass the result to a fetch function before you can access the values.
$res=sqlanywhere_query(...)
//fetch one
$data = sqlanywhere_fetch_row($res)
// or u loop through
while($row = sqlanywhere_fetch_row($res))
{
echo $row["id"];
}
all these functiosn are deprecated. you can use mysql_query and mysql_fetch_row (or other fetch functions).
you can also use mysqli_ functions. read PHP manual.
hope it helps

Echoing string from a mysql query - PHP

How do I echo a simple string from a MySQL Query?
I'm trying trying to accomplish this with the following code but it is not working...The data I am pulling is fine so I know that my mysql_query is working (I've checked that via a different URL GET method.
<?php
$myQuery = mysql_query("fetch some stuff....");
$myResult = mysql_fetch_object($myQuery);
echo $myResult;
you need to know what is returned type. in what your doing you assume that it printable but most of what db queries return are either in object form or an array
try doing a
echo "<pre>" ,print_r($myResult, TRUE),"</pre>";
First of all use var_dump($myResult) to see the data and it's structure.
Since it's an object it will have properties named as the columns returned by the SELECT statement you used.
echo $myResult->column_name; // Should work fine
Usually if echo $variable; doesn't work it means that the variable is either en empty string '' or a null value NULL or a false value FALSE which all show "nothing" when echoed.
But when using var_dump() on them you get a report of the type of data and size of it.
Providing your query is correct, it looks like your php tags are incorrect:
<?php ?>
P.S. It might help if you post the actual query so it can be troubleshooted here. It's hard to ask why something is not working and get an answer if you don't show any of it.
First, var_dump($myResult);. If you see NULL, your query is failing. If you see a big block o' jumbled text, the query is, in fact, working. Since you are echoing $myResult, it is no surprise that nothing is being output, as you are trying to echo the object directly rather than the property you want. Try echoing $myResult->myColumn;
Also, please use MySQLi or PDO, as php_mysql is deprecated.

Getting values from the query string using Janrain's PHP OpenID library

I am using Janrains PHP openID library. All is working perfectly except when I try and pull values from the query string on the script that is returned to after the user is authorized.
Here is the first part of the query string:
http://localhost/Cloudshare/trunk/Cloudshare/php/openID/recieve_auth_request.php? janrain_nonce=2010-11-08T22:29:59Zp9PTgs&openid.ns=http://specs.openid.net/auth/2.0&openid.mode=id_res&openid.op_endpoint=https://www.google.com/accounts/o8/ud.
I am able to get the value of the 'janrain_nonce' item on the query string, but am not able to get any values for things like 'openid.op_endpoint'..
For example, this
$test = $_GET["janrain_nonce"];
returns a value ok into $test, but the the value of $test is blank using the following...
$test = $_GET["openid.claimed_id"];
What I am trying to do is get one of the values from the query string to see if it contains 'google' or not, as I have some processing specific to Google to do.
Any help is much appreciated.
Thanks.
I didn't see openid.claimed_id in your querystring, but try getting $_GET['openid_claimed_id'];
testing your querystring at my API seems to indicate that the "." is being converted to "_" FWIW
I've confirmed that. var.var2 gets converted to var_var2 see
http://api.fatherstorm.com/?query&test_is[]=1&test.is[]=2

Categories