CodeIgniter Forums
Not connecting to DB - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Installation & Setup (https://forum.codeigniter.com/forumdisplay.php?fid=9)
+--- Thread: Not connecting to DB (/showthread.php?tid=88037)



Not connecting to DB - cbd - 07-10-2023

Hello,

I'm following the tutorial and I'm in the Database chapter.

I have a XAMPP running on port 8082 and I've tested a connect.php in XAMPP connecting to localhost and my user and password, and database ci4tutorial, and this file works.

However, the same parameters in the Database.php file of CodeIgniter, running the code with php spark serve, both outside htdocs and inside htdocs of XAMPP give a not able to connect to the database message.

Do you have any ideas what I may be doing wrong?

Should the DB connection work with the php spark serve command?

Or should I build it and use it inside XAMPP only?

How could I debug this?

Thank you,


RE: Not connecting to DB - kenjis - 07-10-2023

Showing exact error message helps you.

I recommend you don't use "php spark serve", when you use XAMPP.
Why don't you use XAMPP Apache?


RE: Not connecting to DB - cbd - 07-11-2023

Is it possible to test database connection outside XAMPP ?

I have this error, but the credentials are correct :

Main connection [MySQLi]: Access denied for user '****'@localhost (using password: YES)

Is this code of the Model correct ?
Shouldn't I have something like $this->db = \Config\Database::connect() in the constructor ?

class NewsModel extends Model
{
protected $table = 'news';

public function getNews($slug = false)
{
if ($slug === false) {
return $this->findAll();
}

return $this->where(['slug' => $slug])->first();
}
}


RE: Not connecting to DB - JustJohnQ - 07-11-2023

You can use phpmyadmin in xampp to test your db connection.


RE: Not connecting to DB - kenjis - 07-11-2023

(07-11-2023, 01:17 PM)cbd Wrote: Is it possible to test database connection outside XAMPP ?
Yes, it is possilbe if you configure correctly.

(07-11-2023, 01:17 PM)cbd Wrote: I have this error, but the credentials are correct :

Main connection [MySQLi]: Access denied for user '****'@localhost (using password: YES)

Is this code of the Model correct ?
Shouldn't I have something like $this->db = \Config\Database::connect() in the constructor ?

No, you don't.

(07-11-2023, 01:17 PM)cbd Wrote:
PHP Code:
class NewsModel extends Model
{
    protected $table 'news';

    public function getNews($slug false)
    {
        if ($slug === false) {
            return $this->findAll();
        }

        return $this->where(['slug' => $slug])->first();
    }




RE: Not connecting to DB - InsiteFX - 07-11-2023

You usally get that error because the password is set to yes!

In the XAMPP Control Panel select shell from the right side listing.

then enter this on the command line, change user and password to your own that you want to use..

Code:
// Shell from Control Panel or CMD - change your_password to yours.
cd mysql\bin
mysqladmin --user=root password "your_password"

NOTE: You need to restart the MySQL Server after all changes made.



RE: Not connecting to DB - cbd - 07-13-2023

This last command always give me access denied

mysqladmin --user=root password "your_password"

I have tried with and without password, and the other user I have with password, it also fails.


RE: Not connecting to DB - cbd - 07-13-2023

Great !
Thank you to you all !!
After I touched the shell of XAMPP I set up a new password which I couldn't unchange, and so I decided to uninstall and install XAMPP again.
I've looked at the table user and I didn't have the user ''@localhost, as I've seen in some foruns, it is a common MySQL problem, turning valid users to anonymous because of this empty user.
Nevertheless, I also noticed I had the .env uncommented with another password (different passwords in Database.php and .env file) so the .env file was overriding my Database.php configuration.
I just commented again those lines and now my connection worked, giving just another error of Selecting fields of objects as arrays, so I changed to View of the tutorial to this, and now it worked !

<?php foreach ($news as $news_item): ?>

<h3><?= esc($news_item->title) ?></h3>

<div class="main">
<?= esc($news_item->body) ?>
</div>
<p><a href="/news/<?= esc($news_item->slug, 'url') ?>">View article</a></p>

<?php endforeach ?>

Just a note: I connected know with the ci-news application outside of the XAMPP, using mysql xampp database to store the news.

Thank you

Now I'm just not able to open individual news page.
It is giving me a 404.

My Route.php

$routes->get('/', 'Home::index');
$routes->get('news', ['App\Controllers\News', 'index']);
$routes->get('news/(Confusedegment)', ['App\Controllers\News', 'view']);
$routes->get('pages', ['App\Controllers\Pages', 'index']);
$routes->get('(Confusedegment)', ['App\Controllers\Pages', 'view']);

I'm not really sure what is wrong, I see in older versions there is $1 after the news view path.

It tries to search for the news "elvis-sighted" for example as it was a file inside a Views folder


RE: Not connecting to DB - InsiteFX - 07-13-2023

Try this.

PHP Code:
use App\Controllers\News;
use 
App\Controllers\Pages;

$routes->get('/''Home::index');
$routes->get('news', ['News''index']);
$routes->get('news/(segment)', ['News''view']);
$routes->get('pages', ['Pages''index']);
$routes->get('(segment)', ['Pages''view']); 



RE: Not connecting to DB - cbd - 07-21-2023

This is not working for me.

Apparently I had them in the wrong order.
The other day I had tried every order combination, but today I've put this one and it worked.
It seemed before it was taking Confusedegment of pages before searching inside news folder.

$routes->get('/', 'Home::index');

$routes->get('news', ['App\Controllers\News', 'index']);
$routes->get('news/(Confusedegment)', ['App\Controllers\News', 'view']);

$routes->get('pages', ['App\Controllers\Pages', 'index']);
$routes->get('(Confusedegment)', ['App\Controllers\Pages', 'view']);


Thank you all very much for your support !
Without you I have could not arrived to the end of this part of the tutorial !!