Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: Jineps - helmikuu 28, 2014, 11:01:29 AP

Otsikko: Problem with SSI.php - No database selected
Kirjoitti: Jineps - helmikuu 28, 2014, 11:01:29 AP
Lainaus käyttäjältä: Havenworld - heinäkuu 12, 2009, 09:15:49 AP
I'm having a similar problem, the "No database selected" error. It is returned every time my PHP scripts try to execute any basic query.


global $scripturl, $db_connection, $db_passwd, $maintenance, $db_server, $options;
global $db_name, $db_user, $db_prefix, $db_persist, $db_error_send, $db_last_error, $sc, $context;
global $settings, $sourcedir;
require("../forums.havenworld.co.uk/SSI.php");


All the ssi functions work with that in my websites config file, but whenever I execute a NORMAL MySQL query using the default PHP functions I get the "No database selected" error.


I have too like this problem, anybody can help me ? same database but some tables havent got smf_ prefix.
SSI.php
No database selected
Otsikko: Re: Problem with SSI.php - No database selected
Kirjoitti: margarett - helmikuu 28, 2014, 11:23:14 AP
Please don't hijack such an old topic, especially already marked as solved. I'm splitting this (and replying shortly)

Otsikko: Re: Problem with SSI.php - No database selected
Kirjoitti: margarett - helmikuu 28, 2014, 11:29:52 AP
If you don't have the prefix, you need to build the table information yourself. Something like this:



<?php
// Include the SSI file.
require(dirname(__FILE__) . '/SSI.php');
global 
$db_name$smcFunc;

$my_prefix '`' $db_name '`.';

.................

//Before your query:
$table $my_prefix 'table_name';

//The query, finally...
$result $smcFunc['db_query']('''
SELECT something
FROM ' 
$table '
WHERE conditions'
);
....
$smcFunc['db_free_result'] ($result);
?>

Otsikko: Re: Problem with SSI.php - No database selected
Kirjoitti: Jineps - helmikuu 28, 2014, 12:32:34 IP

@margarett thank you its worked.But why I cant use simple query like $query = mysql_query("Select * bla bla bla"); can i use like this ?
Otsikko: Re: Problem with SSI.php - No database selected
Kirjoitti: margarett - helmikuu 28, 2014, 12:44:53 IP
For one, that's a deprecated function in latest PHP versions. In your custom PHP code you should be able to use it, but inside SMF it is blocked for security and cross-database operation.

Quoting Arantor: :P
Lainaa
The Book of Arantor, 17:3-5
  And I said unto him, thy database query shalt always be sent by the messenger of $smcFunc
  And $smcFunc shall protect you against injections and evil
  And so it came to pass that mysql_query was declared deprecated and even though he says he is not dead yet, the time was soon to come to pass when mysql_query shall be gone and no more
Otsikko: Re: Problem with SSI.php - No database selected
Kirjoitti: Jineps - helmikuu 28, 2014, 12:54:03 IP
Thank you @margarett, I'm changing all queries now.

I found all of queries :)

http://wiki.simplemachines.org/smf/$smcFunc

Otsikko: Re: Problem with SSI.php - No database selected
Kirjoitti: margarett - helmikuu 28, 2014, 01:19:22 IP
Great ;)

I'm now marking this as solved, feel free to undo it if you need further help.