Store Community Support Documentation
Hi guys,

just a question, I tried several times with different codes but I haven't been able to solve my problem.

I wrote the following code:

Quote:
$link = mysql_connect("localhost", "root", "rootpassword");
mysql_select_db("my_database", $link);

$result = mysql_query("SELECT * FROM test_poll", $link);
$num_rows = mysql_num_rows($result);

Here the rest of the code in which $num_rows is read into another function...


This code works fine, what I just need to do is to find a way for don't make appear username and password ("root" and "rootpassword") into the code.

Is there a way to do that?

Thank you guys!
Be the first person to like this.
Donna(data66)
#1
moved to proper section
Be the first person to like this.
webwolf
#2
You will have to put that information somewhere. Maybe create a php file defining those variables and put that file above the root so it cannot be served from apache. Then do an include in the current file before where you have:
PHP:
$link mysql_connect("localhost"$user$password); 

You would have:
PHP:
include /var/www/mysite/myfolder/filename.php

In this case, your website would be in /var/www/mysite/public_html/.
(the path would be different for your website.)
Last update on March 12, 8:45 pm by webwolf.
Be the first person to like this.
#3
Hi Wolf,

your are great as always! thumbs_up

Your advice has worked fine for me, also if I have had using the following code instead than yours:

Quote:
include '.\.\.\.\.\.\filename.php';


Can it be ok at the same way in your opinion?


Just another thing, a curiosity, but why if I write the following code I don't need to specify any username and password and the connection to the database happens correctly anyway?

Quote:
$oDb = Phpfox::getLib('database');
$result = $oDb->select('*')->from('mytable_test')->execute('getSlaveRow');
d($result);
$num_rows = mysql_num_rows($result);


Why it works anyway?
Be the first person to like this.
#4
Also, you can try to do something like this if you are using it in the phpfox website:

PHP:
<?php

define
('PHPFOX'1);
require_once(
"include/setting/server.sett.php"); //it will uses the same database as the phpfox website
global $_CONF;

$host $_CONF['db']['host'];
$user $_CONF['db']['user'];
$password $_CONF['db']['pass'];
$database $_CONF['db']['name'];
$prefix $_CONF['db']['prefix'];

$con mysql_connect($host$user$password);

if (!
$con)
{
    die(
'Could not connect: ' mysql_error());
}

else
{
    
$db mysql_select_db($database$con);
    if (!
$db)
    {
        die(
'Could not select database.');
    }
    else
    {
        
// Query HERE: 
        
$query "Select * from `".$prefix."user`"//the default prefix is phpfox
        
$result mysql_query($query$con); 
        
    }
}

mysql_close($con);

?>
Last update on March 13, 4:08 pm by EricMart.
Be the first person to like this.
#5
Hi Eric,

great code... I think this solution could be the best one, but the code show: "Could not select database" at the moment.

But it does the connection correctly because the code passes the first if.

It seems like it is not able to select the database...

What can be the problem in your opinion? It would be great if your code works...!
Last update on March 13, 5:26 pm by project-G.
Be the first person to like this.
#6
Just place the file in where the phpfox website is localized.

For example, let says it's in the /public_html/, then just place the file there and it will be able to get connected to the database.
Be the first person to like this.
JohnJr
#8
Webwolf....I gave you another like....it will help me later on...thanks.
Be the first person to like this.
webwolf
#7
From your first post, I assumed that you are trying to use a separate database from phpfox. If that is the case, you can use the phpfox variables for user and password if you establish the phpfox user as an authorized user of your other database.

As far as the path, whether you use a relative or absolute path doesn't matter as long as it works.
Be the first person to like this.
#9
Quote:
Originally posted by: EricMart

Just place the file in where the phpfox website is localized.

For example, let says it's in the /public_html/, then just place the file there and it will be able to get connected to the database.


Hi Eric, I tried to do as you suggested putting the file into the public_html folder and then adding an include into the file .class.php of my module, but I still get the sentence: "Could not select database."

I'm doing something wrong?
Be the first person to like this.
#10
Quote:
Originally posted by: webwolf

From your first post, I assumed that you are trying to use a separate database from phpfox. If that is the case, you can use the phpfox variables for user and password if you establish the phpfox user as an authorized user of your other database.

As far as the path, whether you use a relative or absolute path doesn't matter as long as it works.


Hi Wolf,
sorry maybe I didn't explain well the question at the beginning... I just have the same database of my phpfox website. My curiosity was why if I write the following code writing the username and password it works fine:

Quote:
$link = mysql_connect("localhost", "root", "mypassord");
mysql_select_db("mydb", $link);
$result = mysql_query("SELECT * FROM mytable_test", $link);
$num_rows = mysql_num_rows($result);



Then, in another example if I don't specify any username and password the connection to the database happens correctly anyway...

Quote:
$oDb = Phpfox::getLib('database');
$result = $oDb->select('*')->from('mytable_test')->execute('getSlaveRow');
d($result);



And the same mixed code does not work in the following script?

Quote:
$oDb = Phpfox::getLib('database');
$result = $oDb->select('*')->from('mytable_test')->execute('getSlaveRow');
$num_rows = mysql_num_rows($result);
Be the first person to like this.
webwolf
#11
If you use the phpfox database object, you do not need to enter any database info. It has already been defined when you installed phpfox.

If you use the execute() method, then you do not have to process the result. It is done for you by phpfox's database object. In The code shown above you are using $result as if it is a resource when it is an array.

Try:
PHP:
$oDb Phpfox::getLib('database');
$result $oDb->select('*')
   ->
from('mytable_test')
   ->
execute('getSlaveRows');
$num_rows count($result); 


Another way of doing this is:
PHP:
$num_rows Phpfox::getLib('database')
   ->
select('COUNT(*)')
   ->
from('mytable_test')
   ->
execute('getSlaveField'); 
Last update on March 14, 12:35 pm by webwolf.
Be the first person to like this.
#12
Wolf,

I tried both your codes... they has worked fine for me.

Your advices are very useful! Always!

Thank you very much also for your explanations... I am learning a lot by you!
Be the first person to like this.
JohnJr
#13
"Your advices are very useful! Always!"

Yes I agree project-G and it is why webwolf has almost as many likes as post!
Be the first person to like this.