Been struggling at this for a couple of hours now. Can anybody point out where I'm going wrong.
The first error I get is:
Fatal error: Call to undefined method PDO::rowCount() in
pm_wall.php on line 8
Call Stack: 0.0000 643680 1. {main}()
index.php:0 0.0015 662904 2.
include('pm_wall.php')
index.php:117
The second error I get is:
Warning: Invalid argument supplied for foreach() in pm_wall.php on line 19
Call Stack:
0.0000 643296 1. {main}() index.php:0
0.0015 649864 2. include(index.php:117
How do I go about resolving this? Here is my code below:
$sql = "SELECT * from pm_msg";
$result = $pdo->query($sql);
if($pdo->rowCount() > 0 && !empty($result)) // first error occurs here
{
foreach ($result as $row)
{
$user_id = $row['user_id'];
echo '<div id="content">
<div class="stbody">
<div class="stimg">';
// set profile picture
$sql = "SELECT img FROM pm_user WHERE id=$user_id";
$result = $pdo->query($sql);
foreach ($result as $row) // second error occurs here
{
echo $row['img'];
}
echo '<div id="stexpandbox"><div></div>';
echo '</div></div></div></div>';
}
}
else
{
echo 'Wanna be starting soemthing?';
}
rowCount() has to be called on your PDO statement, not on your PDO instance !
$result->rowCount(); Reading errors might help...
Something like this maybe:
$sql = "SELECT * from pm_msg";
$result = $pdo->query($sql);
if($result->rowCount() > 0 && !empty($result)) // first error occurs here
{
while( $row = $result->fetch(PDO::FETCH_ASSOC) )
Take a little time(a day or two), and read about PDO and its usage over on php.net.
Use this option in PDO to get rows that are affected and matched:
PDO::MYSQL_ATTR_FOUND_ROWS => TRUE
If the row is not found while update for instance - it will return 0. Hope this helps.
Related
I'm trying to do something fairly complicated but I hope it makes sense in text.
So I have a link on a page which take me to post.php?postid=3
In my database there is a a field which is integer called camp_id. When for example I'm on a post which has the field camp_id with a value of 1, I want to display everything in the table that has the value of 1 in that field.
If I change the URL to post.php?postid=2 and that post has a camp_id of say 4, I would display a list of everything that has a camp_id of 4.
Anyway here is my code below and the current error at the bottom.
Here is my function:
public function getartfromcamp($campid)
{
$con = $this->db->OpenCon();
$campid = $con->real_escape_string($campid);
$stmt = "SELECT * from post WHERE camp_id = '$campid'";
$relatedlinks = $con->query($stmt);
if ($relatedlinks->num_rows > 1) {
$sql = $relatedlinks;
} else {
$sql = "No article";
echo "";
}
$this->db->CloseCon();
return $sql;
}
Here is the code on the page:
include 'postclass.php';
$postid = $_GET['postid'];
$article = new Post();
$relatedlinks = $article->getartfromcamp($postid);
?>
<div class='row'>
<?php
while ($row = $relatedlinks->fetch_assoc()) {
?>
<ul>
<ul>
<li><?php echo $row['article_name'];?></li>
</ul>
It seems to work with postid=1 but when I change it to something else I get the error below:
Fatal error: Uncaught Error: Call to a member function fetch_assoc()
on string in
C:\inetpub\wwwroot\local.test.co.uk\blog-example\camp1.php:18 Stack
trace: #0 {main} thrown in
C:\inetpub\wwwroot\local.test.co.uk\blog-example\camp1.php on line 18
Line: 18:
while ($row = $relatedlinks->fetch_assoc()) {
In function getartfromcamp, you are returning $sql string, instead of the connection object, when there is no result.
In this particular case, no result is coming, hence string is being returned. So it throws out error, as you are trying to run fetch_assoc on a string. You should let the function return connection object only, even if there are no rows being returned.
Change to following:
public function getartfromcamp($campid)
{
$con = $this->db->OpenCon();
$campid = $con->real_escape_string($campid);
$stmt = "SELECT * from post WHERE camp_id = '$campid'";
$relatedlinks = $con->query($stmt);
$this->db->CloseCon();
return $relatedlinks;
}
SideNote: You should switch to Prepared statements, to prevent SQL injection related issues.
This question already has answers here:
"Can't use function return value in write context" error in PHP
(3 answers)
Closed 6 years ago.
Guys i read all the questions asked in site but there is no answer for me, please forgive me to ask new question.
I need to fetch mysqli db. I did it but it has 18k rows and i'm using each row in a bot. It takes so long time and i took error for execution time. I need to do this in short time.
Here is my solution for this problem but it didnt work any.
<?php
include "baglan2.php";
$q = mysqli_query($baglanti, "SELECT bolumlink FROM bolumler WHERE id=?");
$row = mysqli_fetch_assoc($q);
$r = $row['bolumlink'];
for($a=1; $a<=count($r); $a++){
for($b=1; $b<=count($r); $b+300) {
$statement = $baglanti->prepare($query);
$statement->bind_param("i", $id);
$statement->execute();
$statement->bind_result($bolumlink);
$statement->fetch();
$link_array($a) = array();
if ($query) {
while ($row = mysqli_fetch_assoc($query)) {
$link_array($a)[] = $row['bolumlink'];
}
}
}
$statement->close();
}
?>
this codes receipt is:
Fatal error: Can't use function return value in write context in C:\xampp\htdocs\xampp\dizipub\playerbaglan.php on line 15
i tried to delete ($a) on link_array s then it gives error:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\xampp\dizipub\playerbaglan.php on line 5
Notice: Undefined variable: link_array1 in C:\xampp\htdocs\xampp\dizipub\playerlinkler.php on line 8
Warning: file_get_contents(): Filename cannot be empty in C:\xampp\htdocs\xampp\dizipub\playerlinkler.php on line 8
<html>
<p><font face=Arial color=Black size=2>
<?php
include "baglan.php";
include "playerbaglan.php";
$parcala = '#<iframe width="100%" height="300" src="(.*?)" frameborder="0" allowfullscreen></iframe></span></div><div id="2">#si';
$bot2ara = file_get_contents($link_array1);
preg_match_all($parcala,$bot2ara,$playerlar);
for($a=0; $a<count($playerlar[0]); $a++) {
foreach (array($playerlar[0][$a]) as $playerlar2);
$playerlar3 = explode('"', $playerlar2,-5);
echo "<pre/>"; print_r($playerlar3[5]);
}
}
?>
</font></p>
</html>
i'm trying to explode link_array cause file_get_contents cant work with 18k rows. I tried to share this 18k rows to 60 variables by using $a and for. but every method i tried is not working. I tried making 60 php for 60 variable also but it didnt work too and it is kind of a stupidity :)
Please help me guys :)
It appears to be a typo.
Your $link_array uses parenthesis instead of square brackets.
I'd suggest you to initialize it as an empty array before the for loops.
include "baglan2.php";
$q = mysqli_query($baglanti, "SELECT bolumlink FROM bolumler WHERE id=?");
$row = mysqli_fetch_assoc($q);
$r = $row['bolumlink'];
$link_array = [];
for ($a=1; $a<=count($r); $a++) {
for ($b=1; $b<=count($r); $b+300) {
$statement = $baglanti->prepare($query);
$statement->bind_param("i", $id);
$statement->execute();
$statement->bind_result($bolumlink);
$statement->fetch();
$link_array[$a] = array();
if ($query) {
while ($row = mysqli_fetch_assoc($query)) {
$link_array[$a][] = $row['bolumlink'];
}
}
}
$statement->close();
}
Beware, also, that $link_array1 does not exist. Maybe you meant $link_array[1]?
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
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.
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.