I'm rewriting my application to make use of CodeIgniter. Currently I'm struggling with DB. I've configured databases in config/databases.php but have no idea how to access them in a scripts that are loaded in a traditional, non-MVC, way.
In a view login.php I have:
require_once("application/views/pages/include/membersite_config.php");
if(isset($_POST['submitted']))
{
if($fgmembersite->Login())
{
$fgmembersite->RedirectToURL("main");
}
}
in a required membersite_config.php :
require_once("fg_membersite.php");
in fg_membersite Login() leads to DBLogin() which is :
function DBLogin()
{
$this->connection = pg_connect("host=localhost port=5432 dbname=".$this->db->database." user=".$this->db->username." password=".$this->db->password."");
I receive errors:
Undefined property: FGMembersite::$db and Message: Trying to get property of non-object
Codeigniter is a MVC framework, so I'm afraid you have to folow that pattern.
Best way to do is rewrite your functions so that you can use it in a model.
Of course you can bypass it, but when you are rewriting your application, you better do it right.
By looking at your code, it seems likely that the variables you call in this piece of code have another name/location (correct me if I'm wrong):
function DBLogin()
{
$this->connection = pg_connect("host=localhost port=5432 dbname=".$this->db->database." user=".$this->db->username." password=".$this->db->password."");
Maybe instead of calling $this->db->database you need to call $this->db['database'] or something. If you supply the rest of the source code from that class we might be able to further help you.
Related
I am trying to call a function from one file to another in PHP, however, I am getting the following error:
Fatal error: Call to undefined method SQLite3::print_model_query_form()
I have two files. The first - dbfunctions.php contains the method print_model_query_form().
The second file query_models contains the following code:
include_once("functions/dbfunctions.php");
$db = new SQLite3('D:\xampp\sim\htdocs\dis_sqlite_database\disSQL3.db');
print $db->print_model_query_form("query_models.php");
The function looks a little like this:
function print_model_query_form($action, $current_values = 0){
$db = new SQLite3('D:\xampp\sim\htdocs\dis_sqlite_database\disSQL3.db');
if($current_values){
// set to previous values.
}else{
// get POST values.
}
// Code to print query form.
}
Thanks in advance for any help.
Since you only hit fatal error on 3rd line, $db should be instantiated successfully. So, the issue should be the print_model_query_form method.
Referring to PHP:SQLite3 - Manual
, there is no such built in method called print_model_query_form.
[Edit 1]
Try to use require_once instead of include_once to make sure you have included dbfunctions.php successfully.
[Edit 2]
Check If you are using PHP's built in SQLite3 class (check your php.ini for extension=php_sqlite3.dll or extension=php_sqlite3.so).
If this is the case, check your dbfunctions.php for:-
class Something
new SQLite3
function print_model_query_form
If all the above exists then you should replace your 2nd line with,
$db = new Something(..);
Note: It would be better if you can show dbfunctions.php instead or letting us make assumptions based on guessing.
I wonder how can I find my moodle's database contents.
I want to elaborate users etc.
My first try is with PHP code using global $DB but there is an error message:
Fatal error: Call to a member function get_record() on a non-object in C:\xampp\htdocs\test.php on line 3
I use the getrecord() function but I didn't do it right.
Can anybody help me? The name of my database is "base".
Note: moodle version is 2.7.2
This is an example that should allow you to access the Moodle DB.
require_once(dirname(__FILE__).'/../../config.php'; // Insert as many '/../' as you need to get from the subdirectory your file is in, back up to Moodle's config.php
function my_function() {
global $DB;
$record = $DB->get_record('user', array('id' => 2));
var_dump($record);
}
my_function();
I'm writing a simple blogging web app for my portfolio and I've come across this strange problem. I wrote a PHP script to connect to a database and read and write data to a database by RESTful calls.
I've done this before in other programs with no problems, but in this one I get an error. I wrote a simple test page to check that the RESTful calls would work before I started using them in my main app. Instead of working, I got an error back from my PHP script.
The error goes like this:
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): Can't connect to local MySQL server through socket '/No-MySQL-hostname-was-specified' (2) in /home/releesquirrel/storage.electricsquirrel.net/SimpleBlog/php/model_SimpleBlog.php on line 35
The code leading up to line 35 goes like this:
class model_SimpleBlog {
// Properties
// Database Info
private $serverName = "mysql.electricsquirrel.net";
private $userName = "esqrl_client";
private $userPassword = "fakepassword";
private $dbaseName = "esquirrel_simpleblog";
// Methods
public function model_SimpleBlog() {
//
}
// Load the ten latest entries after an offset
public function loadEntries($offset) {
$result = false;
// Connect to the Database Server
$connection = mysqli_connect($serverName, $userName, $userPassword, $dbaseName);
I've changed the password for privacy but that's the code that's throwing the error. I'm completely stumped. I've used code similar to this with no problems before, and I've tried googling the error code with no luck.
Does anybody know why I'm getting this error and what I can do to fix my code?
In PHP, you need to refer to object variables with $this->:
$connection = mysqli_connect($this->serverName, $this->userName, $this->userPassword, $this->dbaseName);
$serverName, for instance, looks for a variable with that name in the scope of the function. Obviously none exists.
See the manual for more information on PHP object properties.
Ok so Im working to update my website to mysqli and I am having a difficulty with DB.php include for the db connection.
So my db.php looks like this.
<?php
$username="usern";
$server="server";
$password="pwd";
$database="db";
//MySQLi methods
$mysqli = new mysqli($server, $username, $password, $database) or trigger_error ( mysqli_connect_errno().mysqli_connect_error() );
?>
Note: the sql error output is for debugging Im aware that outputting sql and verbose errors is not good practice ;)
Anyways, so that works just fine.
After updating the db.php file to mysqli im having issues with all the pages that use that. Basically even with the include I get the following errors mysqli in \display_functions.php on line 21 Fatal error: Call to a member function query() on a non-object in r\includes\display_functions.php on line 21
The relevant portion of that file is:
function showConfig ($configID)
{
date_default_timezone_set('America/Denver');
require_once($_SERVER["DOCUMENT_ROOT"] . "/includes/DB.php");
$tbl_name="avr";
// Get information from DB for display
$configQuery="SELECT * FROM $tbl_name WHERE configID=$configID";
$configResult=$mysqli->query($configQuery);
<some more code>
}
When this was all mysql it worked and now it doesnt and Im not really sure why. I thought the include statement was supposed to bring in everything from the noted file but the only way to make this function (and others like it) work is by basically putting the contents of DB.php inside the function. Which is of course a terrible practice.
Also, still kind of learning all of this so forgive this ignorant question but it only became a problem with the time zone change this weekend, how would i setup the default timezone function so I dont have to put it in everything?
the $mysqli inside the function has local scope. it's not the same variable. you need to either pass $mysqli as a parameter
function showConfig ($configID, $mysqli)
or use a global declaration inside the function
function showConfig ($configID)
{
global $mysqli;
One of my recent questions (here) asked about how to catch errors (and display them with the ErrorController) in the Bootstrap file. The example I used was during database instantiation: if an error occurred in the Bootstrap, it would just dump the error onto the screen without calling the ErrorController.
But, say, in my database instantiation plugin, I have this code:
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
$db = Zend_DB::factory("Pdo_Mysql", $this->config);
Zend_Registry::set("db", $db);
}
No error will be thrown, even if the database isn't connected. This is because Zend_DB doesn't bother to check that the database is live, so as to not waste resources and time.
My question is whether I should do something like this:
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
$db = Zend_DB::factory("Pdo_Mysql", $this->config);
$db->getConnection();
Zend_Registry::set("db", $db);
}
That way, any connectivity problems are caught early on and displayed. Is this a good idea? Should my application instead wait to check that the database connection is live on first usage?
I would setup specific handling of the Zend_Db exception type in my error controller.
PHP is not stateful, so initialization of the app is going to happen for every page request. What if my page request doesnt even need the DB (for example, my maintenance page, error pages, etc)?
Most importantly, what is the proper handling of no connection during bootstrap? How can you be sure that any other resources you need in order to render an error page have finished bootstrapping?
However, this is just my preference.