The SQL call below keeps returning:
Fatal error: Call to a member function fetch_assoc() on a non-object in /home/content/76/10930776/html/apprentice/report.php on line 86
$sql = "select sum(".$column.") as totalmeetings,username from data where datediff(max(dateinput),min(dateinput)) <= ".$daysdifference." group by username ";
echo $sql;
$result = $mysqli->query($sql);
while($pcresult = $result->fetch_assoc())//line 86
{
}
What am i doing wrong?
The SQL call prints out like this:
select sum(prospects1stmeeting) as totalmeetings,username from data where datediff(max(dateinput),min(dateinput)) <= 500 group by username
try this query
select sum(prospects1stmeeting) as totalmeetings,username from data
group by username
having datediff(max(dateinput),min(dateinput)) <= 500
Look at how you're trying to access the results:
$result = $mysqli->query($sql);
while($pcresult = $result_percentile->fetch_assoc())//line 86
{
}
$result and $results_percentile are not the same thing. You should try:
$result = $mysqli->query($sql);
while($pcresult = $result->fetch_assoc())//line 86
{
}
unless you're not showing your whole code.
EDIT
But since you edited your question to no longer reflect this answer, the answer is still that you're trying to access a non object. That means $result does not produce data that can be read as an associative array.
Related
I got php fatal error after transfer server with php v5.6.19, before that I had no problem at all with following script
Fetch data from db table:
function get_department_list($mysqli)
{
$sql = $mysqli->query("SELECT * FROM `dept` ORDER BY `dept_id` ASC");
if($sql->num_rows > 0){
return $sql;
}else{
return false;
}
}
Populate data in HTML:
<ul class="department overflow-scroll text-center">
<?php
$shop = new Shop;
$depts = $shop->get_department_list($mysqli);
while($dept = $depts->fetch_object()){
echo '<li>'.$dept->dept_name.'</li>';
}
?>
</ul>
In the end I got an error:
Fatal error: Call to a member function fetch_object() on boolean in C:\xampp\htdocs\project\include\header.php on line 206
First, you are returning a boolean from your function. So, no wonder PHP says you so.
Second, you should keep the matters separated. a function that works with mysqli should keep all mysqli stuff inside. An return just an array, that can be used anywhere without the need to call mysqli functions again.
function get_department_list($mysqli)
{
$sql = $mysqli->query("SELECT * FROM `dept` ORDER BY `dept_id` ASC");
return $sql->fetch_all();
}
And then use not while but foreach
foreach ($depts as $dept) ...
Besides (and more for the people who may chance to land on this question looking for an answer to their question) you should always set proper error reporting for mysqli, like it shown in this answer
Update your while loop for that case when you get false from $shop->get_department_list() call
updated while like this check for $depts if any data then get $dept:
while($depts && $dept = $depts->fetch_object()){
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.
}
I have this function on my model, which receives a parameter so I can pre-load some information on the view page, but somehow is coming back empty:
function addTicket($idt)
{
//Db Connection
$DB2 = $this-> load-> database('DB2', TRUE);
if (!empty($idt)){
$query = $DB2->query ("
Select TROUBLE_ID, ASSIGNED_DATE, CREATOR, PROBLEM_DESCRIPTION, RESOLUTION, RESOLVED_DATE
FROM TABLE1
WHERE TROUBLE_ID = ".$idt."
");
if($query){
return $query->result_array();
}
else
{
echo 'No Queries to display';
}
}
else {echo 'No results to display';}
}
I have an Oracle DB with a ton of entries, but the query keeps coming back empty, Just in case I did an echo 'id:'.$idt.; to check if the ID is being passed. And yes it is.
Also Im getting this message:
Fatal error: Call to a member function result_array() on a non-object
On my view page i have this code:
foreach($results as $row){ }
And im getting this message now:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Any idea why this is not working?
Is this CodeIgniter? If so a better way to check for results is if($query->num_rows() > 0) { } rather than just if($query) { }
Also if its CodeIgniter make sure you have db_debug set to TRUE in the config/database.php otherwise you won't get to see the database errors.
For some reason the query is failing
$this->db->_error_message();
should tell you why
$this->output->enable_profiler(TRUE);
Should give you much more information about the query
Update:
$DB2 = $this-> load-> database('DB2', TRUE);
if (!empty($idt)){
$query = $DB2->query ("
change ^^ to vv
$DB2 = $this-> load-> database('DB2', TRUE);
if (!empty($idt)){
$query = $this->DB2->query ("
Finally I was able to fix this problem when in my query condition i used single quotes before double quotes, like this:
Select TROUBLE_ID, ASSIGNED_DATE, CREATOR, PROBLEM_DESCRIPTION, RESOLUTION, RESOLVED_DATE
FROM TABLE1
WHERE TROUBLE_ID = '" . $idt . "'
I'm trying to calculate the number of students who've completed an their homework for an online gradebook, and I can't figure the code out...
// SELECT THE TOTAL
$gettotal = "SELECT enroll FROM student_course WHERE classID = $classID";
$showtotal = #mysqli_query ($dbc, $gettotal); // Run the query.
//THIS IS LINE 108
$numtotal = mysql_num_rows($showtotal);
echo '$numtotal';
// SELECT THOSE PASSED
$getpassed = "SELECT entry FROM grades WHERE classID = $classID AND test_result >= 80";
$showpassed = #mysqli_query ($dbc, $getpassed); // Run the query.
$numpassed = mysql_num_rows($showpassed);
//THIS IS LINE 117
echo '$numpassed';
// PERFORM THE PERCENTAGE FUNCTION
function percent($numpassed, $numtotal) {
$count1 = $numpassed / $numtotal;
$count2 = $count1 * 100;
$count = number_format($count2, 0);
echo $count;
}
//THIS IS LINE 124
percent($numpassed, $numtotal);
I get the following error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource on line 108
$numtotal
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource on line 117
$numpassed
Warning: Division by zero in on line 124
0
Okay - while I thank everyone for their concern removing the # ... no one noticed that the problem was using mysqli_query and then mysql_num_rows. It needed to be changed to mysqli_num_rows.
Thanks though :)
Chances are the query has failed.
Also mysql_query will return FALSE on error so you can check for that.
$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
Btw: You might want to remove the # operator before mysql_query so you see if something goes wrong there.
Remove error suppression (#) in your code and use inspection methods print_r($var) and var_dump($var) to verify the returned database call values.
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.