homepage Welcome to WebmasterWorld Guest from 54.161.175.231
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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




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

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]

 

phparion




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

$res = mysql_query($query);

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

btw try this in php

print_r($GLOBALS);

eelixduppy




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

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. ;)

eelixduppy




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

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'];

shams




msg:3339741
 3:44 am on May 15, 2007 (gmt 0)

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

shams




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

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

eelixduppy




msg:3339749
 4:01 am on May 15, 2007 (gmt 0)

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 :)

shams




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

thanks so much for the help the last script worked perfect.

phparion




msg:3339800
 6:03 am on May 15, 2007 (gmt 0)


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?

shams




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved