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))
Related
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.
I have my data stored in a JSON string like these...
a:1:{s:15:"s2member_level1";s:1:"1";}
How can i read this values in mysql?
I need to know if the value "s2member_level1" is 1.
Thanks!!!
That's not JSON but a string resulted from calling serialize() in PHP. You cannot parse it easily in MySQL. If you can use PHP, use the unserialize function:
$obj = unserialize($data_from_mysql);
if ($obj['s2member_level1'] == 1) {
// more code here
}
You can convert data to JSON in PHP using the json_encode function. In a similar way, you construct an object from a JSON string using json_decode.
#Lekensteyn is correct, but you could do a like statement, although its performance would most likely be very poor. My true answer is to change how you store this information to take advantages of best performing queries.
select * from table
where column like '%s:15:"s2member_level1";s:1:"1";%';
#Lekensteyn is right about the type of this particular String, but for others, PHP has json_decode which you can use to convert a JSON object to a PHP object. It would be considerably more difficult to read such an object using MySQL only.
This is no json, but serialized data. It was probably serialized with the 'serialize' function of PHP. Try:
print_r(unserialize('a:1:{s:15:"s2member_level1";s:1:"1";}'));
... to unserialize it.
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.
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.
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.