Jump to content

Old code Mysql to Mysqli


Lightme

Recommended Posts

On the mainpage I am getting this error:

 

Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\page\controller\function.php on line 393

 

 

	$result = $db->query($query);

And on the admin section:

 

Notice: Undefined index: role in C:\xampp\htdocs\page\admin\header.php on line 7

Notice: Undefined index: role in C:\xampp\htdocs\page\admin\header.php on line 7

Fatal error: Undefined class constant 'site_url' in C:\xampp\htdocs\page\admin\header.php on line 8

 

 

if ($_SESSION['role'] !== 'admin' and $_SESSION['role'] !== 'moderator'){header( 'Location:'.config::site_url.'index.php' );

 

Link to comment
Share on other sites

1. Where are you defining $db to be an object prior to that call to the query() method?

 

2. Where is $_SESSION['role'] defined? And have you included the class "config" and does it have a constant called site_url defined? The first two errors for that are just "Notice" errors and should be suppressed in a production environment, but the last one is a fatal error. That you definitely need to resolve. You shoudl also put an exit() after the header redirect.

Link to comment
Share on other sites

First off all thanks for the awnser I am still in a learning curve. 

1. Hope I got this right but it is in the config.php file line 55:

$db = new mysqli($config['db_host'], $config['db_user'], $config['db_password'], $config['db_name']);

2. This question I am not complete getting. But I give it a try. (function.php):

	$urls = isset($_SESSION['user_id']) ?detail($row):'./signup';
Link to comment
Share on other sites

#1, how do you know that creating the new $db object is not failing? Do you have any error handling? The error you are getting would seem to indicate that $db is not getting created correctly. Try doing this right after line 55 to see if $db is an object or not. If it is FALSE, then it is failing somehow.

 

var_dump($db);

 

#2, That line does not show that $_SESSION['role'] is being defined. This error

 

 

Notice: Undefined index: role in C:\xampp\htdocs\page\admin\header.php on line 7

indicates that you are trying to reference that value but it is not defined.

 

This error

 

 

Fatal error: Undefined class constant 'site_url' in C:\xampp\htdocs\page\admin\header.php on line 8

indicates that there is no "site_url" defined in the class "config" which you are referencing like this: config::site_url

Link to comment
Share on other sites

1. This I have for error handling of the database:

config.php *line 57

if(!$db){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

2, Hope I get this I am really trying hard to understand this line, (really need to study more PHP, really apriciate let me think about the script.

header.php

if ($_SESSION['role'] !== 'admin' and $_SESSION['role'] !== 'moderator'){
header( 'Location:'.config::site_url.'index.php' );
Edited by Lightme
Link to comment
Share on other sites

Just because you're not getting the errors doesn't mean they're not happening - production servers typically do and should suppress error display - the user doesn't need to know that much about your server set-up. Granted, you should be seeing your fatal error.

 

Have you used session_start() to actually start the browser session and have you defined the constant $site_url in the config class before attempting to use either $_SESSION['role'] or config::site_url?

Link to comment
Share on other sites

when you use OOP mysqli syntax, the connection (new mysqli(...)) will always return an object. you must specifically test for connection errors using one of the two methods shown in the php.net documentation - 

/*
 * This is the "official" OO way to do it,
 * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
 */
if ($mysqli->connect_error) {
    // your error handling code here...
}

/*
 * Use this instead of $connect_error if you need to ensure
 * compatibility with PHP versions prior to 5.2.9 and 5.3.0.
 */
if (mysqli_connect_error()) {
    // your error handling code here....
}


had you used the procedural mysqli_connect() syntax, your testing of the $db value would have worked.

 

i recommend skipping mysqli and use PDO as it has far fewer consistency problems like this, especially if you are going to use prepared queries.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.