Welcome to WebmasterWorld Guest from 54.144.206.214

Forum Moderators: coopster & jatar k

php script to display the current mysql database and user name?

php script to display the current mysql database and user name?

   
3:13 am on May 15, 2007 (gmt 0)

5+ Year Member



hi,
this is a php mysql script that should display the current database and user name:

?php
include 'library/config.php';
include 'library/opendb.php';
$query="select database(), user()";
$result=mysql_query($query);
echo $result;
include 'library/closedb.php';
?>

but the output of this script is:
Resource id #5
any one can help please?

[edited by: shams at 3:27 am (utc) on May 15, 2007]

3:21 am on May 15, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



$res = mysql_query($query);

while($row=mysql_fetch_array($res)) {
echo ''; //field names
}

btw try this in php

print_r($GLOBALS);

3:23 am on May 15, 2007 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



Well, running this query would kind of be unnecessary...

The reason being that in order to send a query to mysql, you must first connect to the server using mysql_connect [php.net]() where one of the arguments is the username. Secondly, before you can send a query, you must select a database using mysql_select_db [php.net](). Since both of these things must be done before you can send a query, you should already know who the current user is and what database they are using. I hope this makes sense. ;)

3:35 am on May 15, 2007 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



Changing the question, are we? ;)

You cannot echo the result directly because it is not a string, it is of a result type. You must 'extract' the data from it using something like phparion has shown you. In this case, there should only be one row returned, however, so you will not need the loop:


$query="select database() AS `db`, user() AS `user`";
$result=mysql_query($query);
$row = [url=http://www.php.net/mysql-fetch-assoc]mysql_fetch_assoc[/url]($result);
echo 'database: '.$row['db'],'<p>';
echo 'user: '.$row['user'];
3:44 am on May 15, 2007 (gmt 0)

5+ Year Member



hi
thanks for the replies i run the script print_r($GLOBALS); like:
<?php
include 'library/config.php';
include 'library/opendb.php';
print_r($GLOBALS);
include 'library/closedb.php';
?>
the output is:
Array ( [GLOBALS] => Array *RECURSION* [_POST] => Array ( ) [_GET] => Array ( ) [_COOKIE] => Array ( [SQMSESSID] => 5es2t67vaqv56mkbs3b02u27d6 ) [_FILES] => Array ( ) [db_host] => localhost [db_user] => root [db_pass] => [db_name] => y2006 [conn] => Resource id #4 )

how i can print the output somtehing like this:
db_hsot => localhost
db_user => root
db_name => y2006

3:58 am on May 15, 2007 (gmt 0)

5+ Year Member



hi eelixduppy,
thanks for reply this is the script:
<?php
include 'library/config.php';
include 'library/opendb.php';
$query="select database() AS `db`, user() AS `user`";
$result=mysql_query($query);
$row = mysql_fetch_assoc($result);
echo 'database: '.$row['db'],'<p>';
echo 'user: '.$row['user'];
include 'library/closedb.php';
?>
and this is the output:
database:
user: apache@localhost
4:01 am on May 15, 2007 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



I'm not sure why the database isn't showing up in the code, but based on the output from your GLOBALS, you can just directly echo the variables assigned these values:

echo 'host: '.$db_host.'<p>';
echo 'user: '.$db_user.'<p>';
echo 'database: '.$db_name.'<p>';

Good luck :)

4:26 am on May 15, 2007 (gmt 0)

5+ Year Member



thanks so much for the help the last script worked perfect.
6:03 am on May 15, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member




include 'library/config.php';
include 'library/opendb.php';

I bet you already have current database and username in one of the above files which you are including in this page then you can simply use them as variables because after inclusion they are the part of your code, why do you need to fetch current db and username again with some extra code?

1:36 am on May 16, 2007 (gmt 0)

5+ Year Member



this is a part program for a hospital, just for when the open they should now in which database they are working now.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month