The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/printthread.php 16 require_once



UserSpice
Display MySQL data - Printable Version

+- UserSpice (https://userspice.com/forums)
+-- Forum: Miscellaneous (https://userspice.com/forums/forumdisplay.php?fid=28)
+--- Forum: Documentation (https://userspice.com/forums/forumdisplay.php?fid=30)
+--- Thread: Display MySQL data (/showthread.php?tid=201)



Display MySQL data - kang08 - 08-07-2016

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>';

}

}
?>


Display MySQL data - mudmin - 08-11-2016

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.


Display MySQL data - kang08 - 08-12-2016

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.


Display MySQL data - mudmin - 08-12-2016

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.


Display MySQL data - kang08 - 08-13-2016

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;


Display MySQL data - mudmin - 08-14-2016

I'm going to send you an email from a personal email address. If you don't get it, check your spam folder.


Display MySQL data - mudmin - 08-18-2016

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 ....)



Display MySQL data - picassoo - 08-22-2016

I have the same problem of updeting my tabel in DB, and for insert.


Display MySQL data - mudmin - 08-22-2016

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.