Can't select the current database name - php

I need to write a code that finds the name of the current sql database and puts that name in a variable. I'll take any suggestions, but what I was thinking was querying the current database name and putting that in a variable. I don't know if that will work because I can't seem to query my database name at all. The database I'm using for this is named "wp_plugin_development" but the query won't show me that. I'm using the code:
SELECT DB_NAME() AS [Current Database];
to get the database name in phpMyAdmin, but it brings up an error saying:
1305 - FUNCTION wp_plugin_development.DB_NAME does not exist
I don't know why it's doing that. Thanks for any help.

If you are using MySQL try:
SELECT DATABASE();
or refer to this:
MySQL Doc: How to get DB Info

The WordPress database name is already stored as a constant in a standard wp-config.php. You can access it with DB_NAME.
<?php $database_name = DB_NAME; ?>

SELECT schema();
can be used to retrieve the current db name in Mysql. this can also be assigned to a variable using AS keyword.

try this:
global $wpdb;
echo $wpdb->dbname;

Related

How do I get database information after running pg_connect?

I have some code that runs a function and within it executes pg_connect.
$db = pg_connect("$dsn");
Is it possible to retrieve the database information from the $db variable, specifically the database name? If I run a var_dump on $db I get:
resource(18) of type (pgsql link)
Another approach is to use the PHP function that was specifically developed for this purpose:
$db_name = pg_dbname($db);
http://php.net/manual/en/function.pg-dbname.php
This saves you the SELECT.
You can simply run a command on the database you've connected to that asks for its name; as documented in the Postgres manual, the relevant query would be:
SELECT current_database() as database_name;
(I will assume you know how to run an SQL query using your $db variable, so won't bother with PHP samples.)

Different SQL result with same query in PHP and MySQL

After a quick search, I haven't find solution of my problem so I post a new one.
So I have to create view in a MySQL database from a Web interface (using PHP).
I use a PEAR framework for the connection with MySQL(5.0.26)
I have the SQL request :
CREATE VIEW test AS SELECT cswc.code_de_la_copie, cswc.service_de_reference, cswc.libelle_de_la_copie, cswc.direction_de_securite_logique
FROM pressi_copiesServiceWithCibles cswc LEFT OUTER JOIN pressi_servicesReferenceWithCibles srwc ON cswc.service_de_reference = srwc.code_du_service
WHERE cswc.cible is null
AND (srwc.cible LIKE '%£DOMAIN£%' OR srwc.cible LIKE '%$DOMAIN$%');
When I execute this request directly on mon local MySQL Database, I obtain a result with 470 lines.
However, when I execute this request in my PHP code, I have a different result (I have 386 line), and I don't know why !
$values['request'] = "SELECT cswc.code_de_la_copie, cswc.service_de_reference, cswc.libelle_de_la_copie, cswc.direction_de_securite_logique
FROM pressi_copiesServiceWithCibles cswc LEFT OUTER JOIN pressi_servicesReferenceWithCibles srwc ON cswc.service_de_reference = srwc.code_du_service
WHERE cswc.cible is null
AND (srwc.cible LIKE '%£DOMAIN£%' OR srwc.cible LIKE '%$DOMAIN$%');";
$baseView = "test";
$sqlView = 'CREATE VIEW '.$baseView.' AS '.$values['request'];
$res =& $this->mdb2->query($sqlView);
if (PEAR::isError($res)) {
return false;
}
Moreover, I have already create 6 views before this one without any problem (same result in PHP and in MySQL)
Thank you for your help
Note that your connection to the database also has a CHARSET and a COLLATION for any string values you include in your query. Although your query looks the same to you in both situations, it must not from the MySQL servers point of view.
Maybe the client CHARSET (and/or COLLATION) differ when you connect via PHP from when you connect via MySQL console.
See the MySQL manual for more information on the client charset and collation.
For comparison, you can use this query:
SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';

Fetch data from second database frequently while connection to first database is always available

I m working on old existing project which uses mysql function for database operation. The existing system connects to the database, say cdcol. The connection to this database is available through site wise.
Now I want to fetch data from another database say crawlerdb, assign fetched data to an array and close connection to this database. The connection to second database is inside a function say GetAccess, and each time the extra data needed, the function is called, data fetched and connection closed to the second database.
All I want is connection to first database should be available every time.
The problem I m facing is. If i don't close connection to second database. Then mysql query used after calling the function GetAccess, still search items from second database, because the connection to second database is active. If I close the connection to second database, still the query doesnot work. Following code explains my situation.
<?php
//$conn1 is permanent connection that is used sitewise.
$conn1=mysql_connect("localhost","root","",true) or die(mysql_error());
mysql_select_db("cdcol",$conn1) or die(mysql_error());
echo "1. Current Database = ".mysql_current_db();//prints cdcol
echo "<Br> Function Returned Value = ".GetAccess();
echo "<Br>2. Current Database = ".mysql_current_db(); //In GetAccess function, which is called above if mysql_close($conn2) is used, the mysql_current_db() returns empty value.
//A FUNCTION TO GET EXTRA DATA FROM SECOND DATABASE
function GetAccess(){
$conn2=mysql_connect("localhost","root","",true) or die(mysql_error());
mysql_select_db("crawlerdb",$conn2) or die(mysql_error());
$test=mysql_query("select * from tbllensinfo",$conn2); //here i have used $conn2 as link identifier
$var= mysql_num_rows($test);
mysql_close($conn2);
return $var;
}
//FUNCTION TO IDENTIFY WHICH DATABASE IS CURRENTLY BEING USED
function mysql_current_db() {
$r = mysql_query("SELECT DATABASE()") or die(mysql_error());
return mysql_result($r,0);
}
$res=mysql_query("select * from cds"); //here link identifier $conn1 is not used, i cant change this code because there are several 100s codes, so not possible to change in all of them. Everything will work if $conn1 is used here though
echo "<br>".mysql_num_rows($res);
?>
NOTE:
The two database are hosted on same server, but database users are different, one of which have no access to other database.
So in short What I need is I need to fetch data from second database frequently while connection to first database is always available.
Any help will highly be appreciable, thanks !
Thanks
Sharmila
The mysql functions, such as mysql_query, all have an optional resource parameter identifying the database connection to use. If you omit this second parameter, the functions use the most recently opened connection. That is, they use the connection resulting from the most recent call to mysql_connect. It's considered the most recent result even if you have closed it already.
(Global variable! Let's party like it's 1999!)
If you're going to use more than one connection with mysql calls in your program, you must specify the resource parameter in all mysql_* calls in your program.
Please consider switching to PDO or mysqli. The PHP people have been trying to get rid of this mysql API for years, partly because of this problem, and mostly because it has serious insecurities.

PHP MYSQL: Correct Code to Increment a views column every time a page is loaded in the browser

I am trying to create a "views" system on my books website.
I have the following tables with the following columns:
Books
-bookid
-bookname
-authorid
-views
my webpage is set up to display a book based on the $_GET['bookid'] variable and I want to add 1 (increment the views column by one for that particular book)
I tried using the following code but it didn't update my table:
<?php $sql = "UPDATE `books` \n" . "SET views = views+1 WHERE" . $_GET['bookid'] .= "bookid"; ?>
ALSO: I used dreamweaver to run the recordset query) so maybe something is different.
Please Help!
Sidenote: Can you please recommend a good book/video or written tutorial to learn php and mysql for absolute beginners like my self!
This is important: don't include $_GET paramaters directly in your SQL query.
This makes your website vulnerable to an SQL Injection attack. Sanatise your inputs by using:
$book_id = mysql_real_escape_string($_GET['book_id']); // If it is a string
$book_id = intval($_GET['book_id']); // It it is an integer
// Assuming it is an integer
$sql = "UPDATE books SET views = views+1 WHERE bookid = $book_id";
You obviously need to execute that query, are you doing that?
$user="username";
$password="password";
$database="database";
mysql_connect(localhost,$user,$password);
mysql_select_db($database) or die( "Unable to select database");
mysql_query($sql);
mysql_close();
EDIT:
Also, just a tip, since you're using $_GET you should be executing something like yourscript.php?book_id=12345, is that what you're doing?
you've already found some of the best ways to learn PHP: writing code and coming here when you don't know further :) (don't have a real good tutorial on my hands beyond that ;)
As for your question:
check the value of $_GET['bookid']
check the value of $sql
if all looks as intended, run the query directly
oh wait.
you're not actually executing the sql in your code, just generating a string with the query. you need to open a connection etc, or are you doing that and leaving it out here?
Your query looks slightly off. Try this:
$sql = 'UPDATE books SET views = views+1 WHERE bookid = ' . intval($_GET['book_id']);

Can't access the SQLite database with MAMP and PHP

I have been learning how to program websites lately and the time has come for me to add a database. I have in fact already successfully created a MySQL database and interacted with it with PHP.
My problem is I can't seem to access a SQLite database file with it. I am using MAMP to host locally for now. Here is a snippet of the code I am using to access the db and find and print out a value stored on it.
<?php
$dbhandle = sqlite_open('/Applications/MAMP/db/sqlite/Users');
if ($dbhandle == false) die ('Unable to open database');
$dbquery = "SELECT * FROM usernames WHERE username=trevor";
$dbresult = sqlite_query($dbhandle, $dbquery);
echo sqlite_fetch_single($dbresult);
sqlite_close($dbhandle);
?>
As you have access to the database (your code doesn't die), I'd say there's got to be an error later ;-)
Looking at your SQL query, I see this :
SELECT * FROM usernames WHERE username=trevor
Are you sure you don't need to put quotes arround that string ?
Like this :
SELECT * FROM usernames WHERE username='trevor'
Also, notice that sqlite_fetch_single will only fetch the first row of your data -- which means you might need to use sqlite_fetch_array or sqlite_fetch_object if you want to access all the fields of your resulset.

Categories