Echoing string from a mysql query - PHP - 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.

Related

What is the correct syntax in php in passing variable(s) in a HTML link to another form

This is what I am trying to do. I am trying pass variable(s) from one form to another. I can do it in "HTML" I would like to have done in a PHP echo statement
When I call the second program from the first program I have nothing coming back in the "GET" array. I think ($v_resource_id, $v_category_id) are not being translated correctly What I am doing wrong in my syntax ? What you see here is part of the first program or calling program
echo'';
Aside from the noted html error (missing quote), you are not retrieving the correct keys, you have var and var2 but are trying to retrieve v_resource_id. You need to retrieve $_GET['var'] and $_GET['var2'].
Do print_r($_GET); to see what is being sent via $_GET. You should get:
Array (
[var] => whatever
[var2] => something else
)
Also, you may want to use the native query building function by doing http_build_query(array('var'=>'something','var2'=>'what ever')) to get a query string. Makes this easier and I think it takes care of urlencode(), you'd have to double check that.
Try this, it's about proper quoting
echo "here";
you'll use your values as $_GET['var'] and $_GET['var2']

MySQL PDO request not working return "array" [duplicate]

sorry for being vague but I have a PHP script which extracts data from a database and displays it, however instead of outputting the contents of the database it outputs 'Array' the number of times their are results.
(Couldn't get the to work again, script uploaded here), the script's output is also here.)
Thanks :-)
Use print_r($array); or var_dump($array); to see
$row is an array, in contains all record fields. Use
echo $rows['unique_id'];
instead.
And don't use mysql extension
Have you tried printr or var_dump?
You need to specify the column or value in the array to echo.
echo $rows[unique_id];
As the name implies, mysql_fetch_array() returns an array. You probably want:
echo $rows['unique_id'];

Custom buffering of php mysql results - strange issue

I discovered something very strange with my PHP code and mysqli functions. When I have my code in the format below:
function mainline(){
$q=mysqli_query($this->conn,"select * from table",MYSQLI_USE_RESULT);
$dataset=parse($q);
}
function parse($q){
if (!$q){return NULL;}
while($res=mysqli_fetch_array($q)){$r[]=$res;}
mysqli_free_result($q);$q=NULL;$res=NULL;return $r;
}
I'm able to retrieve data and process it. In the above example, data is returned to $dataset and each element is retrieved in the form of $dataset[row number][field name].
Now when I change my code so its like this:
function mainline(){
$q=mysqli_query($this->conn,"select * from table",MYSQLI_USE_RESULT);
$dataset=parse($q);
}
function parse($q){
if (!$q){return NULL;}
while($r[]=mysqli_fetch_array($q)); // I made change here
mysqli_free_result($q);$q=NULL;return $r;
}
The data returned is always nothing even though the select statement is exactly the same and always returns rows. During both tests, nothing has modified the data in the database.
My question then is why does while($res=mysqli_fetch_array($q)){$r[]=$res;} retrieve correct results and while($r[]=mysqli_fetch_array($q)); does not?
With the second while loop, I won't have to allocate an extra variable and I'm trying to cut down on the use of system memory so that I can run more apache processes on my system instead of waste memory unnecessarily on PHP.
Any ideas why while($r[]=mysqli_fetch_array($q)); wont work? or any ideas how I can make it work without using an extra variable? or am I stuck?
if you want to store all result in array than why not use
mysqli_fetch_all($q)
and store result in whatever you want. Though if you want to have quick access I
think caching sounds more appropriate.
mysqli_fetch_all — Fetches all result rows as an associative array, a numeric array, or both

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

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.

PHP - Echo 'Array' error

sorry for being vague but I have a PHP script which extracts data from a database and displays it, however instead of outputting the contents of the database it outputs 'Array' the number of times their are results.
(Couldn't get the to work again, script uploaded here), the script's output is also here.)
Thanks :-)
Use print_r($array); or var_dump($array); to see
$row is an array, in contains all record fields. Use
echo $rows['unique_id'];
instead.
And don't use mysql extension
Have you tried printr or var_dump?
You need to specify the column or value in the array to echo.
echo $rows[unique_id];
As the name implies, mysql_fetch_array() returns an array. You probably want:
echo $rows['unique_id'];

Categories