I want to trunacate the table 'core_session' by loaded a php script.
How can I fix this, how should this script look like?
No need to load fancy pantsy models and stuff. It can be done much simpler. The only thing you need is a connection to the database:
require_once('./app/Mage.php');
Mage::app();
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$connection->truncateTable('core_session');
or:
$connection->query('TRUNCATE TABLE `core_session`');
Having such scripts globally accessible in the root of your site is a huge security risk by the way, but we'll save that discussion for later.
Disclaimer: Truncating table logic is taken from @sv3n's answer
Create a file truncate.php and put it in the root folder. Add below code in that PHP file:
<?php
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
ini_set('display_errors', 1);
Mage::app();
$model = Mage::getModel('core/session');
$resource = $model->getResource();
$connection = $resource->getReadConnection();
/* @see Varien_Db_Adapter_Pdo_Mysql */
$connection->truncateTable($resource->getMainTable());
$connection->changeTableAutoIncrement($resource->getMainTable(), 1);
echo "table is successfully truncated.";
Make sure you replace the $model with your model. Now load the page www.yourdomain.com/truncate.php. You are done.
Also please consider going through Marius's answer.
$model, when I want to truncate table 'core_session'?