PHP/Sqlite3: Fatal error: Call to undefined function sqlite_num_rows() - php

I am getting this error when I call the function sqlite_num_rows. It must not be dependency issue since other Sqlite functions are working. I'm able to open connection and get data from DB.

4 Years late but i had the same issue so here is my Solution for anyone that has the same problem
//$db is the database handle
$result = $db->query("SELECT * FROM table_name");
$rows = 0; //set row counter to 0
while($row = $result->fetchArray()) {
$rows += 1; //+1 to the counter per row in result
}

Relative to info on php.net
neither of "sqlite_num_rows($result)" and "$result->numRows()" is not
working on SQLite3 ! you should use this way:
<?php
$db = new SQLite3('databasename.db');
$result = $db->query("SELECT * FROM users");
$rows = count ($result);
echo "Number of rows: $rows";
Click me

Related

Save SQL data to PHP array

I want to pull the data from a SQL table to an array in my PHP script. I need that because after that I want to compare two tables.
$sql = "select date, sum(clicks) from Table group by date";
$query = $Db->query($sql);
$result = array(); // Script does not work even if I remove this line
$result = $query->fetchAll();
print_r($result);
I am getting the error :
PHP Fatal error: Call to undefined method mysqli_result::fetchAll()
As #Mark said, use
$result = $query->fetch_all();
For PHP version prior to PHP 5.3.0, use:
while ($row = $result->fetch_assoc()) {
// do what you need.
}

How to fetch a row using mysqli query?

I am trying to fetch a row from my mysql DB using mysqli query.
PHP
$_SESSION['orderID'] = "632";
$orID = $_SESSION['orderID'];
$sql = $db->prepare('SELECT * FROM order_list WHERE order_id = ? ');
$sql->bind_param('s',$orID);
$sql->execute();
while($row = $sql->fetch())
{
$productid = $row[0];
$name = $row[1];
echo $price = $row[2];
}
give no error in console and no result,
I have been trying to check the answers on stack overflow, I also googled it but all the suggestions gives me the same error.
I am pretty new with mysqli, your help will be highly appreciated
mysqli_fetch_array is a mysqli_result method not a mysqli_stmt one
You could use ->fetch() upon a mysqli_stmt
So basically your code could change that way
while($sql->fetch()) {
//do something
}
but you need to call bind_result() before looping (otherwise you can't access returned values)

PHP JSON array from SQLite3

I'm trying convert data from a SQLite3 db to a JSON array by using PHP. I'm getting close, but I can't seem to get it right.
This is the error I'm getting:
PHP Warning: PDOStatement::fetchAll() expects parameter 1 to be long, object given in...
Thanks!
<?php
$db = new PDO('sqlite:example.db');
$result = $db->query('SELECT * FROM test');
$json = array();
$result->setFetchMode(PDO::FETCH_ASSOC);
while ($data = $result->fetchall($result)){
$x = $data['Time'];
$y = $data['Temperature'];
$json[] = array( (int($x)), (int($y)) );
}
?>
Got it working now. Thanks for your help!
<?php
$db = new PDO('sqlite:example.db');
$result = $db->query('SELECT * FROM test');
$datapie = array();
$result->setFetchMode(PDO::FETCH_ASSOC);
while ($row = $result->fetch()) {
extract($row);
$datapie[] = array(floatval($Temperature), $Time);
}
$data = json_encode($datapie);
?>
Change:
$result->fetchall($result)
to:
$result->fetch()
You had two problems: the argument to fetchAll() should be a fetch mode, not a result. And fetchAll() returns all the rows, not one row at a time. If you're calling in a loop you use fetch().
PDO's fetchAll function, doesn't expect the query itself as a param.
Check it's manual here - you can leave it blank, or set the fetch mode:
http://php.net/manual/en/pdostatement.fetchall.php

PHP, MYSQL - Query fails but no error meessage appears

Back again, thanks for all the help last time. I'm running this query:
$query = "SELECT * FROM event where evLoc = ".$loc." AND evChar = ".$char;
var_dump($query);
$result = mysql_query($query, $con) or die('Error 1:'.mysql_error());
if ($result) {
$row = mysql_fetch_array($result) or die('Error 2:'.mysql_error());
var_dump(mysql_num_rows($result));
exit();
I get a message Error 2: but no mysql_error printed out. The var_dump($query) printed out a query that ran without errors in phpMyAdmin. The var_dump(mysql_num_rows($result)) did not print.
This is a case of being too cautious and applying error checking where it doesn't belong.
Don't call die() in partnership with a fetch call. The fetch intentionally returns FALSE when there are no rows available, so you don't have an error, just no rows.
// No rows were returned, wich is FALSE
$row = mysql_fetch_array($result) or die('Error 2:'.mysql_error());
Instead, don't call die() here:
$row = mysql_fetch_array($result);
if ($row) {
// you got something
}
Or this way:
if ($row = mysql_fetch_array($result)) {
// you got something.
}
If multiple rows are expected to be returned, fetch in a while loop.
while ($row = mysql_fetch_array($result)) {
// loops until you're out of rows
// or not at all if you have no rows
}
Obviously, your request returns 0 rows and mysql_fetch_array returns FALSE
Apply Single Quotes in the fields of your query
$query = "SELECT * FROM event where evLoc = '".$loc."' AND evChar = '".$char."'";
You can write these in short form too. Like
$query = "SELECT * FROM event where evLoc = '$loc' AND evChar = '$char'";
Next, you might want to change your fetch portion.
while($row = mysql_fetch_assoc($result)) {
....
}
When you use this, you will avoid the error you would receive when no rows are returned.

Wordpress, mysql_data_seek, external query inside loop

Hey there, why does this code not work?
$qry = mysql_query("SELECT performerid,pic0 FROM ".$table." ORDER BY RAND() LIMIT 6");
$start = new WP_Query('showposts=6&orderby=rand');
if ($start->have_posts()) : while( $start->have_posts() ) : $start->the_post();
$rows = mysql_fetch_assoc($qry);
if (!$rows)
{
mysql_data_seek($rows,0);
$rows = mysql_fetch_assoc($qry);
}
$perfs = $rows['performerid'];
$pics = $rows['pic0'];
I ahve the following error:
Warning: mysql_data_seek(): supplied argument is not a valid MySQL result resource in /home/content/d/d/a/ddxxxx
Your call to mysql_data_seek only happens if $rows is null. If that's true, then the call to mysql_data_seek will certainly fail, because one of it's required args is null. That's why you're getting the error message.
The problem is you're passing the wrong thing to mysql_data_seek(). It's expecting you to pass it $qry (your results object) and not the empty $rows variable you just tested.

Categories