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.
}
Related
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
I am trying to select a table within my database with a GET Method.
Now when I hardcode the value of the variable in there (the table name) it works as expected and it returns the values in an array.
But when I try to determine the table name through a variable, I get the following error:
Fatal error: Call to a member function fetch_array() on a non-object in
Now I have tried the var_dump($result); but that returns bool(false).
Now the variable does carry a value, because when I echo it back to the screen it gives the value I would expect.
So why does not return the value when making the query for my table search???
$result = $mysqli->query("SELECT * FROM PodcastSermons WHERE sermonSeries = ". $series); //This where a change needs to happen
var_dump($result);
$posts = array();
while($row = $result->fetch_array())
{
$ID=$row['ID'];
$sermonTitle=$row['sermonTitle'];
$sermonSpeaker=$row['sermonSpeaker'];
$sermonSeries=$row['sermonSeries'];
$sermonDate=$row['sermonDate'];
$linkToImage=$row['linkToImage'];
$linkToAudioFile=$row['linkToAudioFile'];
$posts []= array (
'ID'=> $ID,
'sermonTitle'=> $sermonTitle,
'sermonSpeaker'=> $sermonSpeaker,
'sermonSeries'=> $sermonSeries,
'sermonDate'=> $sermonDate,
'linkToImage'=> $linkToImage,
'linkToAudioFile'=> $linkToAudioFile
);
}
$response['posts'] = $posts;
var_dump($posts);
PS I have read about the depreciation in mysql style and that I know have to use mysqli writing. I am running PHP Version 5.2.6-1+lenny16
If the $series is a string you need to put quotes around the variable..
Try...
$result = $mysqli->query("SELECT * FROM PodcastSermons WHERE sermonSeries = '". $series ."'");
Hope it helps.
Now I have tried the var_dump($result); but that returns bool(false).
Because your query failed.
Try:
if( ! $result = $mysqli->query("SELECT * FROM PodcastSermons WHERE sermonSeries = ". $series); ) {
echo "An error has occurred: \n" . var_export($mysqli->error_list, TRUE);
} else {
//do stuff
}
The central question seems to me: Where does $series come from? Where does that variable ever get initialized?
If you're passing this in from the web form, two things: either use $_GET or $_POST (whatever action you use in your form). And then you have to sanitize what comes from there, in order to not be vulnerable to SQL injection attacks. Prepared statements are your friend in this case; they help harden your script against this kind of attacks.
try this
$result = $mysqli->query("SELECT * FROM PodcastSermons WHERE sermonSeries = '$series' ");
$result = $mysqli->query("SELECT * FROM PodcastSermons WHERE sermonSeries = ". $series); //This where a change needs to happen
You should be using Prepared Statements if the variable: $series is user defined.
$result->prepare("SELECT * FROM PodcastSermons WHERE `sermonSeries`=?");
$result->bind_param('s', $series);
$result->execute();
Also, Print_r($result); to check if your initial $result to see if it has been populated; Furthermore, in your SQL Query is sermonSeries properly matched to your SQL Table?
Update:
while($row = $result->fetch_array())
{
Try Modifying this to:
while($row = $result->fetch_array(MYSQLI_ASSOC))
{
http://uk1.php.net/manual/en/mysqli-result.fetch-array.php
your query simply fails. check var_dump($series); before executing.
i assume it might be a string and you just don't quote it?
just a tip: first build a string with your commandtext before
calling $mysqli->query. and use that string (like $mysqli->query($cmd);
dump that string :) might open your eyes ;)
that way you can extract it and execute it directly against the database (f.e. phpmyadmin).
I am trying to load a list of IDs into a PHP array which I can loop through. The SQL query I am using returns 283 rows when I run it in PHPMyAdmin. However, when I run the following PHP script, it only returns a single row (the first row). How can I modify my code to include all the rows from the resulting SQL query in my PHP array?
Code:
//Get active listing IDs
$active = "SELECT L_ListingID FROM `markers`";
$active = mysql_query($active) or die(mysql_error());
if(is_resource($active) and mysql_num_rows($active)>0){
$row = mysql_fetch_array($active);
print_r($row);
};
Thanks,
Using mysql_fetch_array will return only the first row and then advance the internal counter. You need to implement it as part of a loop like the following to get what you want.
while($row = mysql_fetch_array($active)) {
// Your code here
}
Keep in mind that mysql_ functions are now also deprecated and slated to be removed in future version of php. Use mysqli_ functions or PDO.
In PDO it's rather straight forward:
$rows = $conn->query($active)->fetchAll();
See PDO::queryDocs and PDOStatement::fetchAllDocs.
With mysqli you would use mysqli_result::fetch_all and with PDO there's PDOStatement::fetchAll to fetch all rows into an array.
Code for mysqli
$sql = "SELECT L_ListingID FROM `markers`";
$result = $mysqli->query($sql);
if ($result !== false) {
$rows = $result->fetch_all();
}
with PDO it's nearly the same
$sql = "SELECT L_ListingID FROM `markers`";
$result = $pdo->query($sql);
if ($result !== false) {
$rows = $result->fetchAll();
}
I try to call 3 SQL Server 2000 Stored Procedures one after the other using PDO DBLIB in PHP 5.4.4 (Linux) and I get an error at the second query : Fatal error: Call to a member function fetchAll() on a non-object
The first query works perfectly, returning results as expected. If I move query order, every time the first query succeeds and the others fail.
Also, when the exact same code is run on a PHP 5.3.14 server, everything works great.
Example code:
$dbh = new PDO ("dblib:host=myhost;dbname=mydb","user","pass");
$query = $dbh->query("EXEC dbo.storedProc1 'param1'");
$result = $query->fetchAll();
var_dump($result);
$query = $dbh->query("EXEC dbo.storedProc2 'param1'");
$result = $query->fetchAll(); // <-- Fails here
var_dump($result);
$query = $dbh->query("EXEC dbo.storedProc3 'param1'");
$result = $query->fetchAll();
var_dump($result);
Any clue to make this code run on PHP 5.4 ?
EDIT : PDO::errorInfo gives me that error : Attempt to initiate a new Adaptive Server operation with results pending [20019] (severity 7) [EXEC dbo.storedProc2 'param1']
Also, calling query with a SELECT (SELECT 1, SELECT 3 and SELECT 3 for example) gives the same result (first result is given, following are empty)
EDIT 2 : Looks like it's related to a PHP bug, as noticed by Capilé in the comments
I'm going to do out on a limb and say that your stored procedure returns more than one result set. Either that, or SQL Server 2000 is bloody-mindedly insisting that you close the cursor before next query when it's empty. Either way, this should fix the problem:
$dbh = new PDO ("dblib:host=myhost;dbname=mydb","user","pass");
$results = array();
$query = $dbh->query("EXEC dbo.storedProc1 'param1'");
do {
$results[] = $query->fetchAll();
} while ($query->nextRowset());
$query->closeCursor();
var_dump($results);
$results = array();
$query = $dbh->query("EXEC dbo.storedProc2 'param1'");
do {
$results[] = $query->fetchAll();
} while ($query->nextRowset());
$query->closeCursor();
var_dump($results);
$results = array();
$query = $dbh->query("EXEC dbo.storedProc3 'param1'");
do {
$results[] = $query->fetchAll();
} while ($query->nextRowset());
$query->closeCursor();
var_dump($result);
Be aware that when actually using $results it will be one level deeper than you might expect, because it can store multiple result sets, and these would be stored in separate keys.
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.