Is there a way of printing a result from an sql query rather than just reading a resource id? Something like the way 'print_r' works?
(Assuming MySQL.)
There is no native PHP functionality to iterate a resultset through a resource handle, no. You must iterate yourself using mysql_fetch_assoc.
On the upside, you can write a function to do it.
function print_rs($recordset) {
while ($row = $recordset->fetch_assoc())
print_r($row);
}
print_rs($db->query("SELECT * FROM `lol`"));
.. or something along these lines.
Are you printing the return value of mysql_connect? You should be looking in the output of mysql_fetch_assoc instead.
Related
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
I got a basic simple query to get data from a table. It doesn't need any parameters so I want to use a basic PDO::Query way.
I got this:
$items = $cmsDbh->query("SELECT * FROM `tbl1`");
Now this is my AJAX file, I want to return the data as a JSON object. I tried using:
echo json_encode($items);
But then I get a JSON object which contains the queryString (the SELECT * FROM tbl1).
I don't want to use PDO::Prepare, is there another way of doing it?
echo json_encode($items->fetchAll());
Was stupid. Appearantly PDO::Query returns a PDOStatement object, so you can just use fetchAll. Working code:
echo json_encode($items->fetchAll(PDO::FETCH_ASSOC))
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
Ive been running some queries using PHP's PDO library. It seems that when i use:
<?php
$smtp->execute();
$result = stmt->fecthArray();
?>
It unsets the array inside PDO. I know this because when i call that very same line again, it returns an empty array. Why does it do this? Is this normal behavior?
When building the resulting array, fetchAll() removes all the results from the result set. Instead of calling it again, re-use the array you retrieved in the first time.
I am confused about this, I run a query such as
foreach($dbh->query("SELECT * FROM ...") as $row) {
...do something with row()
But when I var_dump $dbh it is a PDOstatement object.
This leads me to two questions:
How does foreach somehow resolve the object into separate rows?
How can I store all rows in an array, like $arr = $dbh->query(...)? This does not work because it is still an object
I can of course run under the foreach, and do $arr[] = $row, but that seems a bit silly..
PHP has some interesting interfaces that let you handle objects as arrays. In this case the PDOStatement class implements Traversable and this allows the use of foreach on it. See here http://www.php.net/manual/en/class.pdostatement.php
To get all your results in one array take a look at PDOStatement::fetchAll. And also in the future consider checking the PHP documentation on php.net if you run into problems. It's packed with examples and useful user comments.
Use PDOStatement::fetchAll() to get the entire result set as array. AS far as what PDO does internally it uses an iterator to access some representation of a recordset. This way you use less memory because youre not pulling the entire result set into php, youre using something resembling a resource thats internal to the implementation.