-3

I am trying to convert my PHP application to support multi tenancy(SaaS). I came across this article and modified my existing config.php file as below.

<?php

$data = explode('.',$_SERVER['SERVER_NAME']);

if (!empty($data[0])) {
    $subdomain = $data[0];
}

define('SITE_DB_SERVER','localhost');
define('SITE_DB_PORT','3306');
define('SITE_DB_USERNAME','root');
define('SITE_DB_PASSWORD','mysql');
define('SITE_DB_NAME','central');

$sitedbh = mysql_connect(SITE_DB_SERVER.':'.SITE_DB_PORT,SITE_DB_USERNAME,SITE_DB_PASSWORD);

$sitemysql = mysql_query('select id,dbusername,dbpassword from central.client where subdomain = '.$subdomain.'');

$appdata = mysql_fetch_row($sitemysql);

if (empty($appdata['id'])) {
    echo "Oops! Sorry, we are unable to find you! Please email us at support@mydummyapp.com";
    exit();
}

define('DB_HOSTNAME', '127.0.0.1');

define('DB_PORT', '3306');

define('DB_USERNAME', 'root');

define('DB_PASSWORD', 'mysql');

define('DB_DATABASE', 'myapp_'.$appdata['id']);

define('LAST_ACTIVITY_TIMEOUT', '3600');

define('SESSION_RENEG_TIMEOUT', '600');

define('USE_DATABASE_FOR_SESSIONS', 'true');

define('CSP_ENABLED', 'false');

date_default_timezone_set('America/Chicago');

?>

When trying to run my application I am facing "Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in /myapp_path/config.php on line 19" error. Please refer the screenshot. I tried some solutions available for the past 10 hours and nothing worked for me. Kindly help me in getting this up and running.

Error faced when trying to run my app

Sri Harsha Kappala
  • 3,182
  • 25
  • 24
  • 30
  • Your query failed. Look at your quotes. – John Conde Apr 10 '14 at 15:13
  • @JohnConde can you please help me rephrase correct query? – Sri Harsha Kappala Apr 10 '14 at 15:15
  • Please look at the sidebar of this page – Hanky Panky Apr 10 '14 at 15:17
  • 2
    You should also be extremely careful when using `mysql_query` and avoid using it if at all possible, it's deprecated and being removed from PHP in the future. A modern replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/). If you're new to PHP, a guide like [PHP The Right Way](http://www.phptherightway.com/) can help explain best practices. – tadman Apr 10 '14 at 15:36
  • 1
    @tadman +1 for recommending me PDO. Just like a charm my job went well with PDO. Yeah am new to PHP and thanks for referring me those good articles. You made my day.. – Sri Harsha Kappala Apr 10 '14 at 18:39

1 Answers1

2

as john conde said ... the query is wrong, change this:

$sitemysql = mysql_query('select id,dbusername,dbpassword from central.client where subdomain = '.$subdomain.'');

to this:

$sitemysql = mysql_query("select id,dbusername,dbpassword from central.client where subdomain = '".$subdomain."')";

Notice the new quotation marks around the string value in your query

Hanky Panky
  • 45,969
  • 8
  • 69
  • 95
Carsten Hellweg
  • 234
  • 1
  • 3
  • as you said i have changed the query to $sitemysql = mysql_query("select id,dbusername,dbpassword from central.client where subdomain = '".$subdomain."'"); i am not seeing any warning now, but am still getting the error which was in if condition in my above code. Tried echoing $sitemysql and $appdata['id']. $sitemysql is printing "Resource id #6" and $appdata['id'] is showing empty string. – Sri Harsha Kappala Apr 10 '14 at 17:17
  • pls replace $appdata = mysql_fetch_row($sitemysql); with $appdata = mysql_fetch_assoc($sitemysql); your results are just numbered arrays like $appdata[ 0 ] is your id-filed $appdata[1] is your username etc... – Carsten Hellweg Apr 11 '14 at 12:02
  • why is the closing parenthesis `)` part of the string? looks still wrong. – ZeissS Apr 12 '14 at 08:38
  • ooops ... the ")" is a typo – Carsten Hellweg Apr 14 '14 at 09:05