PHP - Get list of databases names [duplicate] - php

This question already has answers here:
How to get a list of databases?
(4 answers)
Closed 11 months ago.
How can I get a list of all the MySQL databases that exist on a server using PHP?

$result = mysqli_query($db_conn,"SHOW DATABASES");
while ($row = mysqli_fetch_array($result)) {
echo $row[0]."<br>";
}

$dbcnx = mysql_connect ($dbhost, $dbusername, $dbpassword);
$result = #mysql_query('SHOW DATABASES');
while ($row = mysql_fetch_array($result)) {
print_r ($row)
}

At the MySQL prompt, SHOW DATABASES does what you want.
You can run this command as a query from PDO or the native PHP MySQL library and read the returned rows. Pretend it is a normal select.
You will only see the databases that the account used to connected to MySQL can see.

The MySQL command for this is
SHOW DATABASES
See the manual for more info on the SHOW command

Just use SHOW DATABASES.It will show all the databases present in your MySQL.

Write the SQL query:
show databases

Related

MYSQLi queries returning blank [duplicate]

This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 years ago.
So I had my website working but the hoster forced me to migrate, so I had to backup all I had and change it to the new host. The code was working but since I changed I cant get this code working. The connection to the database seems fine.
The code had to be changed from mysql to mysqli
All I get from mysqli_num_rows or mysqli_query is blank.
I checked the encoding and its UTF-8.
The query works fine on phpmyadmin
Since I only have ftp access to the publish folder Im not sure what options i have to find the problem.
$link = mysqli_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later." . mysqli_connect_error());
mysqli_set_charset($link, "utf8");
mysqli_select_db($dbname, $link);
$result = mysqli_query($link, "SELECT * FROM $usertable ORDER BY Date DESC");
//echo "result:" . $result; returns blank
echo "results:" . mysqli_num_rows($result); returns blank

Excute big Mysql file with PDO? [duplicate]

This question already has answers here:
Slow performance MySql
(2 answers)
Closed 6 years ago.
i am buiding a automatic create website system...when i am installing web for my customer, at step i am excute the my sql file i am using like this :
# MySQL with PDO_MYSQL
$db = new PDO("mysql:host=$mysqlHostName;dbname=$mysqlDatabaseName", $mysqlUserName, $mysqlPassword);
$query = file_get_contents($local_sql); //Local file abc.sql
$stmt = $db->prepare($query);
if ($stmt->execute()){
My problem is i have up to 117 table need to create and many many insert sql on this file.
Everytime i buidling 2 or more than website it freeze my server and let it dead.
My question is have any solution ? can excute a big sql file like that faster the way i am using ?
Sorry if my english so bad.
Thank all !
Try to execute your queries one after another. Parse SQL file and execute queries in loop like this:
$queries = explode(';', $query);
foreach ($queries as $query) {
$stmt = $db->prepare($query);
$stmt->execute();
}

How do I fix Function mysql_list_tables() is deprecated? [duplicate]

This question already has answers here:
what is the alternate function mysql_list_tables() in php 5
(5 answers)
Closed 9 years ago.
I'm trying to add a voting system for my website, and I get this error:
Deprecated: Function mysql_list_tables() is deprecated in /home/yokoscap/public_html/vote/classes/vote.class.php on line 11
Access denied for user 'yokoscap'#'localhost' (using password: NO)
I'm new to PHP and MySQL, so if you could dumb it down/help me fix it it'd be great.
Here's the code paste, I thought it was too big to go in here so I uploaded to pastebin.
Did you check the manual? There is a complete example on a replacement for it:
<?php
$dbname = 'mysql_dbname';
if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
echo 'Could not connect to mysql';
exit;
}
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "Table: {$row[0]}\n";
}
mysql_free_result($result);
use this sql query
show tables from dbname
hope this will help
The mysql_list_tables() function has been deprecated since PHP 4.3.7 (!) because it's entirely too specific.
To get a list of tables in the current database, run a SHOW TABLES query. It's a perfectly normal query; you don't need a special function just to do that.
I says here in the manual
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
According to mysql_list_tables ...changelog secton
This function became deprecated from version 4.3.7
Use SHOW TABLES

How to connect multiple database,servers in mysql and query from both tables of each other?

I am looking forward to connect two different database from different servers. Also, i would want to run a query that fetches data from both the database into a single result. Am doing this in PHP script with mysql. here is how am looking forward to do it [ without success :) ]
$dbh1 = mysql_connect('server1', 'uname', 'pwd')or die("Unable to connect to MySQL1");
$dbh2 = mysql_connect('server2', 'uname', 'pwd') or die("Unable to connect to MySQL2");
mysql_select_db('db1', $dbh1);
mysql_select_db('db2', $dbh2); //both will have same table name though
$qry = mysql_query("select * from db1.table1 where db1.table1.id='100' and db1.table1.id=db2.table1.id",$dbh1) or die(mysql_error());
$row= mysql_fetch_array($qry);
echo $row[2];
Am not getting any result or either error. Any help is appreciated, tnx.
According to the PHP docs:
http://il2.php.net/manual/en/function.mysql-query.php
"If the link identifier is not specified, the last link opened by mysql_connect() is assumed."
So in this case you're only retrieving data from $dbh2.
I don't think it's possible to do what you are trying to do with one query. You should merge the results after you get them.
It doesn't work that way. You can use multiple databases in a single SQL query, but it always operates on one connection handle. If you need to connect to two different servers, you have to use two queries and merge data in PHP.

php # mysql help [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Extremely basic PHP and Mysql
i am new to web scripting languages. i want to know what are the function's and theories i need to know if i want to submit & retrieve data using php # mysql...\
ex;- user submits a phone of a picture with details,user can search them ...likewise
You may take a look at many tutorials freely available on the net. For example:
http://www.freewebmasterhelp.com/tutorials/phpmysql - describes it in fine details.
first of all to work with mysql in php you need to have a basic understanding of SQL commands like SELECT,INSERT,DELETE... which can be found all over the web.
then you must use the mysql related functions in php to actually execute those commands.
you can find the manual in http://php.net/manual/en/book.mysql.php.
Before you can make any queries, you have to connect to the correct database!
$link = mysql_connect('localhost', 'db_user', 'db_pass');
mysql_select_db('db_database',$link));
Please replace DB_USER and DB_PASS with your login info and the DB_DATABASE witht he corresponding database.
Here's some basic queries:
database table (table):
name text
Here's some code:
mysql_query("INSERT INTO table (name, text) VALUES ('Yo', 'hello')");
This will put a new entry of "yo" and "hello" into the database table (table).
And if you want to display a table, you can do something like this:
$result = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_array($result)) {
echo $row['name'] . " - " . $row['text'];
echo "<br />";
}
Now this is by far a very simple explanation. There are tons of resources on the internet you can look at.
Have fun! (but not too much)
w3school.com has some great examples and easy to understand MYSQL explanations.

Categories