I am not able to connect to mysql using laravel - mysql

I am able to migrate the migration tables to the database i configured in database.php. But when i am trying to fire following command in routes.php
$details=DB::query('SELECT * FROM test_user');
print_r($details);
. I am geeting error on my browser.

There is no such a method that you have used as given below:
DB::query('SELECT * FROM test_user');
Instead you can do it using:
// Get all records from "test_user" table
$users = DB::table('test_user')->get();
Read the Laravel documentation first.

Related

SQLAlchemy - Unable to reflect SQL view

I'm getting the following error message when trying to reflect any of my SQL views:
sqlalchemy/dialects/mysql/reflection.py", line 306, in _describe_to_create
buffer.append(" ".join(line))
TypeError: sequence item 2: expected str instance, bytes found
I have tried using both the autoload_with and autoload=True options in my select query constructor to no avail.
I have the appropriate permissions on my view. My query is pretty simple:
company_country = Table('company_country', metadata, autoload_with=engine)
query = select(company_country.c.country)
return query
I've tried the inspect utility and it does not list my SQL view, nor does the reflecting all tables described below the views section on this page: https://docs.sqlalchemy.org/en/14/core/reflection.html#reflecting-views
I'm using version SQLAlchemy->1.4.32, Python 3.x and mySQL 8.0.28 on Mac if that's any help
I should add that I can query my SQL views using the text() constructor but it would be far more preferable to use select() if possible.
Any tips appreciated
I was using the mysql-connector client for interop with other code, but after switching to the mysqlclient, I was able to reflect the views.
https://docs.sqlalchemy.org/en/14/dialects/mysql.html#module-sqlalchemy.dialects.mysql.mysqldb

Laravel seed database from existing database

i have now new structure of my database, but i need to import the old data in the new format. For that reason i want to use the Laravel seeder, but i need somehow to connect to the old database and make select queries and to tell the seeder how to put the data in the new database.
Is that possible ?
Try:
Examples:
php artisan iseed my_table
php artisan iseed my_table,another_table
Visit: https://github.com/orangehill/iseed
Configure your laravel app to use two mysql connections (How to use multiple database in Laravel), one for the new database, the other for the old one.
I'll fake it like old and new.
In your seeds read from the old database and write into the new.
$old_user = DB::connection('old')->table('users')->get();
foreach ($old_users as $user) {
DB::connection('new')->table('users')->insert([
'name' => $user->name,
'email' => $user->email,
'password' => $user->password,
'old_id' -> $user->id
// ...
]);
}
Make sure to add messages while seeding like $this->command->info('Users table seeded'); or even a progress bar (you can access command line methods) to know at which point of the import you are.
Download package from
Git repo : https://github.com/orangehill/iseed
then update below file src/Orangehill/Iseed/IseedCommand.php
Add below code at line number 75
// update package script
if($this->argument('tables') === null){
$tables = Schema::getConnection()->getDoctrineSchemaManager()->listTableNames();
}
and update getArguments method in same file with below code
array('tables', InputArgument::OPTIONAL, 'comma separated string of table names'),
and then run php artisan iseed so it will get all the tables from your existing db and start creating seeders for all tables

Change mysql content type before query laravel

I have a problem, I had this table (Pkb) :
It's basically a date (but in varchar format, YYYYMMDD), I want to select all the date before today using eloquent laravel 5
$todays = Carbon::today();
$filterdate = Pkb::whereDate('rtglb', '<=', $today)->get();
It works on result if used in controller but not with php artisan tinker (so basically it's not working and thrown an error) :
When I put it in handle function in command console, It's not working too because it's rtglb are in varchar table. (I need to put it in command console to create scheduller)
I have access to database, but I can't access the php code cause this table actually old module and I had to create new module that takes data from this table, calculate it using scheduller..
is there anyway to convert the content of rtglb to date before select it? using eloquent or raw mysql? or are there any solution for me?

How can I query to database from zend framework view (phtml)?

I need to access database from phtml (view) in zend. how can i do that?
$stmt = $db->query('SELECT * FROM mcommerce_cart');
Well, you should not do this (it's bad practice). Recommend way is to access DB via models. You can pass data to view from Controller, Zend_View_Helper or (also no so recommeneded) via direct call to model.
But if you really want to access DB in View than you can get Default Adapter by calling $db = Zend_Db_Table::getDefaultAdapter();

Warning: mysql_num_rows() But only on webhost, not localhost

On localhost this works great. But when I upload files to server I get this error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /storage/content/04/13fd39104/xxxxx.com/public_html/users/list.php on line 54
My code is:
//load users from database
$users = mysql_query("SELECT id,username FROM ".$sql_table_users." WHERE id!='".$_SESSION['user_id']."'");
if(mysql_num_rows($users) > 0){
while($user = mysql_fetch_assoc($users)){
//ALT tag contains user ID and user name
print '• '.$user['username'].'<br />';
}
}
Are you sure to have the same db strutture on server? Probably some column or table name are different from localhost. Check also if your db connection credentials are correct
Perhaps your database on server is not the same as your localhost.
add echo mysql_error(); after mysql_query to see details.
I would echo the query string before performing the query to see if it really is the string you think it should be. Most likely one of the variables is not defined so the query is failing. If that doesn't help you could also output any mysql errors after the query.
And of course, the obligatory suggestion to get off of the deprecated mysql_* functions! If you don't want to do PDO, check out mysqli and how very similar it is to the mysql functions if you use the procedural functions.