I'm executing a simple query, without bound parameters, using PDO. I have tested it directly against my database and it executes cleanly, returning the expected results. However, when I plug it in to my PDO object and call fetchAll(), it returns an empty array.
$query = 'SELECT count(*) as mycount FROM mytable';
$mysql = $connection->prepare($query);
$result = $mysql->fetchAll();
print_r($result);
Expected result:
array
(
[mycount] => 8
)
Actual result:
array
(
)
Any ideas what might be causing this, or how to go about troubleshooting this?
You've prepared, but haven't executed the statement. You need
$mysql->execute();
first
First you need to:
$mysql->execute();
Then you can
$result = $mysql->fetchAll();
Related
Trying to get array from using MySQLi and PHP, so that I could work with each element of the array (like $array[0], array[1])
$titlesquery = $db->prepare("SELECT title FROM books WHERE id = ?");
$titlesquery->bind_param('i', $id);
$titlesquery->execute();
$titlesquery->bind_result($returned_title);
$json = $titlesquery->fetch($returned_title, MYSQLI_ASSOC);
echo json_encode($json);
This doesn't work. I get such warning:
mysqli_stmt::fetch() expects exactly 0 parameters, 2 given
If it helps, just $titlesquery->fetch(); works fine but I get not a kind of array(?) structure, just single element for that column. What is my mistake?
Like what exactly says on the error message, it needs no parameters:
http://php.net/manual/en/mysqli-stmt.fetch.php
bool mysqli_stmt::fetch ( void )
$titlesquery->bind_result($returned_title);
$titlesquery->fetch();
$json = array('title' => $returned_title);
After you have invoked ->bind_result(), this already binds results from your prepared statement.
If you want an array row fetching and this is available to you (needs mysqlnd), use ->get_result() instead.
$titlesquery = $db->prepare("SELECT title FROM books WHERE id = ?");
$titlesquery->bind_param('i', $id);
$titlesquery->execute();
$result = $titlesquery->get_result();
$json = $result->fetch_assoc();
echo json_encode($json);
I'm trying to bind parameters (for security) and then get the result into an array, so I created code like the following. The problem is that it doesn't work: $arr is null. (BTW I know the query in getList works.)
if ($stmt = mysqli_prepare($con, "call getList(?)")) {
mysqli_stmt_bind_param($stmt, 's', $userInputSearch);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$arr = mysqli_fetch_all($result, MYSQLI_ASSOC);
$jsonArr = json_encode($arr);
echo $jsonArr;
Note that I've got mysqli_fetch_all to work when using mysqli_query().
On mysqli_fetch_all reference, I found this comment:
Also, mysqli_fetch_all works only for buffered result sets, which are
the default for mysqli_query. MYSQLI_USE_RESULT will be supported in
5.3.4+ However, it makes little sense to use it this way, materialising unbuffered sets. In this case choose STORE_RESULT, and
fetch_all won't copy the data, but reference it, as it is stored
already in mysqlnd.
I discovered that prepared statements return unbuffered results, so I tried using $result = mysqli_stmt_store_result($stmt) instead of $result = mysqli_stmt_get_result($stmt); However that didn't help.
That leaves me not completely at a loss — I understand that I could loop to load the data one row at a time, but I really don't want to have to loop in PHP just to do something as simple as get an array from a prepared statement. Is there a way to acquire and deal with the result set as one object?
I've just reread documentation...
I have no possibility to test mysql right now.
But just a guess:
mysqli_fetch_all ( mysqli_result $result ...
Parameters result Procedural style only: A result set identifier
returned by mysqli_query(), mysqli_store_result() or
mysqli_use_result().
I want to highlight that all functions mysqli_query(), mysqli_store_result() mysqli_use_result() belongs to mysqli class, not to stmt and they return type mysqli_result
but you are trying to use mysqli_stmt_get_result($stmt);
mysqli_result mysqli_stmt_get_result ( mysqli_stmt $stmt )
...
Return Values
Returns a resultset or FALSE on failure.
So this function return type resultset not mysqli_result.
So you have to change your code to use no mysqli_stmt
so change :
if ($stmt = mysqli_prepare($con, "call getList(?)")) {
mysqli_stmt_bind_param($stmt, 's', $userInputSearch);
mysqli_stmt_execute($stmt);
to
$result = mysqli_query ( $con , "call getList(".mysqli_real_escape_string($userInputSearch).")" );
, or
just change your line:
$arr = mysqli_fetch_all($result, MYSQLI_ASSOC);
to
$arr = array()
while ($row = $result->fetch_array())
{
arr[] = $row ;
}
Sorry if any error, have no possibility to trace and debug right now, hope it is helpful.
This seems like it should be a very easy task, but I'm not sure it is.
I would like to use PDO to do a SELECT query, and then immediately after find out if there are any results (rows), and if so how many.
I'd like to fetch the result as an object not an array, as I like the $obj->col_name syntax, and it just feels a bit wrong to return an array just to find the above out.
$qryh = $conn->query("SELECT ...");
Then use $qryh to first find out if there are any rows, and if so how many.
Can this be done without falling back to using arrays..?
UPDATE:
I know about rowCount(), but I think it only works on UPDATE, INSERT and DELETE - my question relates to SELECT.
UPDATE 2:
I'm using SQL Server and MS Access, not MySQL, and rocount() does not work.
SOLUTION FOUND
The accepted answer lead to the solution. Here is what I found:
I was setting the PDO::ATTR_CURSOR option to PDO::CURSOR_SCROLL when creating the PDO object (new PDO(...), which returned -1 on a rowCount(). However if I set the cursor within ->prepare(... using an array (array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL)) it works and returns the row count.
http://technet.microsoft.com/en-us/library/ff628154(v=sql.105).aspx
So, the example on the above link works, setting the cursor when you create the PDO object doesn't work. Also note that in the 'remarks' section on the above link, it mentions PDO::CURSOR_SCROLLABLE which doesn't for me (I get Undefined class constant 'CURSOR_SCROLLABLE').
Try this way:
$query = "select * from ...";
$stmt = $conn->prepare( $query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();
print $stmt->rowCount()
Should work
$stmt = $conn->query('SELECT ...');
// number of rows:
$rows = $stmt->rowCount();
// get an array with objects:
$objects = $stmt->fetchAll();
// another way to count the results:
$rows = count($objects);
This is what I do in my database class:
$stmt = $conn->prepare('SELECT ...');
$stmt->execute();
$arrayOfObjects = $stmt->fetchALL(PDO::FETCH_OBJ);
$numberOfRows = count($arrayOfObjects);
print $numberOfRows;
By passing PDO::FETCH_OBJ each object is an stdClass meaning you can use the -> syntax.
I believe this is what you are looking for, and it just uses the PHP count() function instead of mySQL rowCount().
If you want more context, here is the DB class I use in all of my projects: https://gist.github.com/pxlsqre/9f6471220ef187343f54
You're right, PDO::rowCount does not work properly with SELECT
One workaround is to Add the count to your query
$qryh = $conn->query("SELECT COUNT(*) as numRows FROM Table");
$result = $qryh->(PDO::FETCH_OBJ);;
$count = $result->numRows;
$qryh->fetchColumn()
Returns the number of rows selected through a SELECT query
I'm strugging to get to grips with this mysqli style of queries. Can someone help me with this code please?
The intention is to put the results from the query (or which there are three in the database) into an array so that I can display them in a table, for example.
Here is my code...
$get_orders_stmt = $db->prepare(
"select cap_transaction_id, company_id, company_transaction_id, cap_points, transaction2login
from transactions
where transaction2login = ?");
$get_orders_stmt->bind_param("s", $_SESSION['username']);
$get_orders_stmt->execute();
while($row = $get_orders_stmt->fetch()) {
$results[] = $row;
print_r($results);
I was hoping to put $row as the returned records into the array, results but I fear I have got this totally wrong! My print_r() gives me this...
Array ( [0] => 1 ) Array ( [0] => 1 [1] => 1 ) Array ( [0] => 1 [1] => 1 [2] => 1 )
Any thoughts or pointer please?
UPDATE!
Turns out the my machine isn't up to spec with the best answer on here. I need MYSQLIND for get_results(); I'm moving to something more compatible.
You should check the documentation. mysqli_stmt::fetch returns a boolean. You need to bind your result to variables with mysqli_stmt::bind_result.
With mysqli it is always a pain.
So, if you're going to use raw API calls in your code, change API then:
$sql = "select cap_transaction_id, company_id, company_transaction_id,
cap_points, transaction2login
from transactions where transaction2login = ?"
$stmt = $db->prepare($sql);
$stmt->execute(array($_SESSION['username']));
$results = $stmt->fetchAll();
print_r($results);
fetch() is not the right function. You should
...
$get_orders_stmt->execute();
$res = $get_orders_stmt->get_result();
while($row = $res->fetch_assoc())
$results[] = $row;
See documentation to choose between fetch_assoc (returning field associative array), fetch_row (returning non-associative array, indexable by column number) and fetch_array(merge between row and assoc)
I created this code:
$statement = $db->prepare("SELECT * FROM phptech_contact");
$statement->execute();
$result = $statement->result_metadata();
$object = $result->fetch_object();
print_r( $object );
When I run it, it doesn't work. Can anybody tell me why it doesn't work?
I have 20 rows in this table so data should be returned.
From http://ch.php.net/manual/en/mysqli-stmt.result-metadata.php
Note: The result set returned by mysqli_stmt_result_metadata() contains only metadata. It does not contain any row results. The rows are obtained by using the statement handle with mysqli_stmt_fetch().
As long as you don't need this meta data you don't need to call this method.
$statement = $db->prepare("SELECT fld1, fld2 FROM phptech_contact");
$statement->execute();
$stmt->bind_result($fld1, $fld2);
while ($stmt->fetch()) {
echo "$fld1 and $fld2<br />";
}
But I really dislike the mysqli extension. PDO is much cooler ... ;-)
$db = new PDO('...');
$stmt = $db->prepare("SELECT fld1, fld2 FROM phptech_contact");
$stmt->execute();
while ($obj = $stmt->fetchObject()) {
// ...
}
or
$objs = stmt->fetchAll(PDO::FETCH_OBJ);
if you're trying to get the rows from the database, the function you need is mysqli_stmt::fetch(), not mysqli_stmt::fetch_metadata()
You're also missing a few steps. When using prepared statements, you must specify the fields you would like to return instead of using the star wildcard, and then use mysqli_stmt::bind_result() to specify which variables the database fields should be placed in.
If you're more familiar with the original MySQL extension, prepared statements have a different process to use. If your select statement has a parameter (eg., "WHERE value=?") prepared statements are definitely recommended, but for your simple query, mysqli:query() would be sufficient, and not very different from the process of mysql_query()
I believe the problem is that mysqli_stmt::result_metadata() returns a mysqli_result object without any of the actual results — it only holds metadata.
So what you want to do is use $result = $statement->bind_result(...) and then call $result->fetch() repeatedly to get the results.
One of the comments under the bind-result() article shows how to do this for a query like yours, where you don't necessarily know all of the columns being returned.