The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.1.2-1ubuntu2.14 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display MySQL data
#1
Hi, I am new to user spice and just started working on PhP MYSQL as i'm building a blog system. Sorry for asking stupid question. How can i fetch all data from mysql and display it? Here is the error code:

<pre>
Code:
Fatal error: Uncaught Error: Call to a member function query() on null
</pre>
<?php
try {

$stmt = $db->query('SELECT postID, postTitle, postDesc, postDate FROM blog_posts ORDER BY postID DESC');
while($row = $stmt->fetch()){

echo '<div>';
echo '<h1>.']'.$row['postTitle'].'</h1>';
echo '<p>Posted on '.date('jS M Y H:iConfused', strtotime($row['postDate'])).'</p>';
echo '<p>'.$row['postDesc'].'</p>';
echo '<p>.']Read More</p>';
echo '</div>';

}

}
?>
  Reply
#2
I think the problem is in that statement->fetch line.

I think you want to do something like

$results = $stmt->results();

If you want to see what that's going to give you, you can do

dump($results);

From there, you can do a foreach loop to loop through your results. I've been traveling so I don't have great internet, but I can try to help from the road.
  Reply
#3
appreciate for your help!
but now i'm facing another issue, i couldn't update my database but there's no problem with insert table.

Code (update):
$fields=array('postTitle'=>$postTitle, 'postDesc'=>$postDesc, 'postCont'=>$postCont);
$db->update('blog_posts',$postID,$fields);

Code (insert):
$fields=array('postTitle'=>$postTitle, 'postDesc'=>$postDesc, 'postCont'=>$postCont);
$db->insert('blog_posts',$fields);

p.s. There's no problem with the string & variable as i've already test it.
  Reply
#4
The code looks right to me. You've done a dump of your variables? Especially $postID? usually when you can't update, somehow your id isn't being passed along.
  Reply
#5
i've dump my variable and also tested the $postID. Even if i don't use any variable and type the string myself, the database still don't update.

And, how can i get the max id of the table? the code below shows me nothing.

$getid = DB::getInstance()->query("SELECT MAX(id) FROM users");
$results = $getid->results();
$id = $results['MAX(id)'];
echo $id;
  Reply
#6
I'm going to send you an email from a personal email address. If you don't get it, check your spam folder.
  Reply
#7
For anyone reading this forum in the future (and I will do a better job documenting this). You MUST have your auto-incrementing column in your database called id if you want to use the built in update tool. You can't use userID or ID or postID. It must be just id. If you don't want to do that, you can always run a regular $db->query(UPDATE users WHERE ....)
  Reply
#8
I have the same problem of updeting my tabel in DB, and for insert.
  Reply
#9
Is your first column labled id and set to auto increment? If so, try to run the query manually

$db->query("...")

instead of doing the UserSpice update query. But...if those things don't work, it's almost always because it can't find your id.
  Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)