Cannot make an MySQL(i) connection from SMFs action

Started by e_xrt123, August 20, 2017, 05:09:51 PM

Previous topic - Next topic

e_xrt123

Hi,
I'm trying to connect to my external MySQL database from a custom SMF action which I made but I approached a very strange problem. MySQL connect() functions don't return anything when called in the action (both mysql and mysqli), there isn't any mysql error at all, errno returns 0. However executing the exact same mysql code in a blank php file outside of SMF works flawlessly and even selects data properly. I have tried many things and nothing has helped me. I even tried making the connection in an external php file included in the action but it resulted in the same behaviour.
Any ideas how to fix this? Or how to make this.

Action code:

<?php

if (!defined('SMF'))
die('Hacking attempt...');

function 
Action_Main()
{
global $context$scripturl$txt$smcFunc;

if(!$context['user']['is_admin'] || $context['user']['is_guest'])
header("Location:index.php");

loadTemplate('MyAction');

$context['page_title'] = $context['forum_name'] . ' - My Action';

$subAction = !empty($_GET['sa']) ? $_GET['sa'] : 'none';


if($subAction == 'search'// sub Action triggered by sending a form containing username from my database
{
$name $_POST['username'];


// Initiate the connection
$con mysqli_connect("my_host""my_user""my_password""my_db");

if($con)
                {
echo 'MySQL success';
                
$res mysqli_query($con"SELECT id, name FROM users WHERE name = '" $name "' LIMIT 1;");
$row $res->fetch_assoc();
echo 'name ' $row["name"] . 'id ' $row["id"];

mysqli_close($con);
                }
else echo 'Mysql error ' mysqli_connect_errno() . ' ' mysqli_connect_error();
}
}
?>



I know that the variable isn't escaped - this is just a testing code and I'm totally aware of that.

What I'm trying to do is to find the ID of the user in a different database by given username, but that doesn't really matter.
Exactly the same MySQL code copied into a blank PHP file on the same server runs flawlessly - prints "MySQL success name [name] id [id]" with correct data. The code in SMF although prints "Mysql error 0 ", no error at all, $con is null and trying to do anything like mysqli_real_escape_string which requires passing $con results in a "Fatal error: Call to a member function fetch_assoc() on a non-object "

Any ideas?

Dzonny

Hey there,

As far as I'm aware you should use $smcFunc to achieve this:
https://wiki.simplemachines.org/smf/$smcFunc
Similar question here:
https://www.simplemachines.org/community/index.php?topic=554614.msg3931513#msg3931513

You should modify your code to use $smcFunc instead of using external php file with query in it, because of the security issues that may expose your site to attacks.

e_xrt123

But I'm not trying to connect to SMF's database but a completely external database not related to SMF. So users of my forum can "connect" their accounts by giving the username and password from external database (it's a game server database whete players have their own registered accounts, I want to let them confirm their accouny ny logging in and then I can display it in their profile or let others view their stats from game etc)

The connection works outside of SMF, but inside of the custom actiom hook it just doesnt even throw any errors and doesnt return any mysql connection link/handle.

Dzonny

Oh, sorry, my bad, I haven't read through your message carefully! Sorry!

Well, the only thing that comes to my mind is to make sure that you're not using same variables as SMF use. I can't check your code right now as I'm browsing from my phone, but I guess that you can re-check variable names. Can't think of another reason why the same code wouldn't work from custom action if it's working as a standalone php file though.

Advertisement: