mysql not showing all databases - php

I'm trying to list all my databases. But I only return information schema and one other table.. I checked my user settings/privileges in mysql and I have access to everything.. How can I return all databases
here is the code i used:
$set = mysql_query('SHOW DATABASES;');
$dbs = array();
while($db = mysql_fetch_row($set)) $dbs[] = $db[0]; echo implode('<br/>', $dbs);

As pointed out in the comments, you really should start using mysqli instead of mysql.
This should solve your problem though:
<?php
$link = mysqli_connect("localhost", "mysql_username", "mysql_password");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$res = mysqli_query($link, "SHOW DATABASES");
while ($row = mysqli_fetch_assoc($res)) {
var_dump($row['Database']);
}

Related

Return MySQL query in PHP using mysqli class

It appears that I am connecting to a MySQL database but unable able to return a query using mysqli class. I've done some searches and what I have coded should return the results, but I am missing something.
<?php
set_include_path('/Applications/MAMP/db/mysql57');
$host = "127.0.0.1";
$user = "admin";
$password = " ";
$bookDatbase = "Books";
$mysqli = new mysqli($host,$user, $password, $bookDatabase);
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$result = $mysqli->query("SELECT Title FROM Book");
echo $result;
$conn = new mysqli("localhost","username","password","Books");
$result = $conn->query("SELECT Title FROM Book");
if(mysqli_error($conn)){
echo mysqli_error($conn);
}
else{
$titles = [];
while ($r = mysqli_fetch_assoc($result)){
$titles[] = $r;
}
echo "<pre>";
print_r($titles);
echo "</pre>";
}
You need to pass mysqli object as well as below:
$result = mysqli_query($mysqli, "SELECT Title FROM Book");
Hope it helps you.

SQLi Error when trying to echo a certain ID in PHP

I am making a status update page for practicing PHP and MySQL.
I am currently trying to echo the saved status on the page.
The statuses are saved in a row called "Status" and they are all given a certain ID in a row called "statusID."
The problem I am having is which fetch I want to use because converting it into a string using (string)$var doesn't work. ($var is an example).
Also, the $idNum variable is something for later use, shouldn't have anything to do with this.
Here is the code: (Obviously the first variables are censored so none tries to connect to the database, the connection working in the actual code.)
The problem lies in the $fetchRes I believe.
<?php
$idNum = 1;
$servername = "censored";
$username = "censored";
$password = "censored";
$db_name = "censored";
$conn = mysqli_connect($servername, $username, $password, $db_name);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Status FROM SavedStatuses WHERE statusID=1;";
$statusQuery = mysqli_query($conn, $sql);
$fetchRes = mysqli_fetch_assoc($statusQuery);
if($conn->query($sql) == TRUE)
{
echo $fetchRes;
} else {
echo "Failed to retrieve status, error: " . $conn->error;
}
?>
As I mentioned in comments, you are querying twice and not looping over (successful) results.
A "loop" is to use either a while or a foreach. I used a while loop for this example.
From the "official" manual:
http://php.net/manual/en/mysqli-result.fetch-assoc.php
Example:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>
So in your case, your code would read as:
Sidenote: Status and status are two different animals, so make sure the letter case matches (in the loop).
<?php
$mysqli = new mysqli("censored", "censored", "censored", "censored");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT Status FROM SavedStatuses WHERE statusID=1";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
echo $row["Status"];
}
/* free result set */
$result->free();
}else{
echo "The query failed: " . mysqli_error($mysqli);
}
/* close connection */
$mysqli->close();
?>

List all the tables in your MySQL database using PHP

I have a hard time trying to list all the tables in my database.
I tried
<?php
//Configuration
$dbname = 'local';
$user = 'root';
$host = '127.0.0.1';
$pass = '';
$date = date('Y-m-d');
$export_type = 'mysql'; // option : mysql | psql
$file_name = $date.'-portal';
$file_path = $file_name;
// Create connection
$conn = mysqli_connect($host, $user, $pass);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "SHOW TABLES FROM $dbname";
$res = mysqli_query($conn, $sql);
if($res != false){
echo "Connected successfully";
$FILE = fopen("output.csv", "w");
$table = array();
while($row = mysql_fetch_array($res)){
$table[] = $row['0'];
}
foreach($tables as $table) {
$columns = array();
$res = mysqli_query($conn, "SHOW COLUMNS FROM $table");
while($row = mysql_fetch_array($res, MYSQL_NUM)) {
$columns[] = "$row[0]";
}
fwrite($FILE, implode(",", $columns)); fwrite("\n");
$resTable = mysqli_query($conn, "SELECT * FROM $table");
while($row = mysql_fetch_array($resTable, MYSQL_NUM)) {
fwrite($FILE, implode(",", $row)); fwrite("\n");
}
}
}else{
die(mysql_error());
}
?>
Result
if($res != false){
//.. everything in here never get executed
}
`$res` kept returning `false`.
What did I do wrong that could have lead to this ?
You can always execute the query:
Show tables;
After you selected database.
By the way you also can execute:
Show databases;
To list all of the databases your current user has permission to view.
Use db in your connection
mysqli_connect($host, $user, $pass, $dbname);
And use query like this
$sql = "SHOW TABLES";
You need to pass through your database in the connection script.
Like so:
$conn = mysqli_connect($host, $username, $pass, $dbname);
Then, when you want to pull rows from a table, you do it like this:
mysqli_query($conn, "SELECT rows FROM table");
One of the reasons this wasn't working for you was because you weren't passing through your database name through the connection. Also, rather than doing the above query, you selected a table from a database; rather than a row from a table.
Also, I noticed that you're using the mysql_* error output on the last line.
Here is a working version
Changes:
Added $dbname to mysqli_connect function
Added the backtick ` char between the table names, to avoid errors with reserved keyword from MySQL
Changed mysql_ functions to mysqli_
Close the file
Close the connection
Here is the code
NOTE: sorry I don't why, but when I pasted the code in the answer, all de code identation was messed up, even trying to indent it properly, I wasted like 10 minutes :(

How come html_entity_decode is not working?

I am having a lot of trouble trying to decode the html in my database columns. Each column in my table has the potential to include something that needs to be decoded. For example / to / is one of the many that needs to be decoded. I am pretty bad at php. My code does not do the decoding. Please help.
<?php
$connection = mysql_connect($host, $user, $pass);
if(!$connection)
{
die("Database server connection failed.");
}
else
{
$dbconnect = mysql_select_db($db, $connection);
if(!$dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$query = "SELECT * FROM Table WHERE area='ON'";
$resultset = mysql_query($query, $connection);
$records = array();
while($r = mysql_fetch_assoc($resultset))
{
$records[] = $r;
}
mysqli_close($connection);
json_encode($records);
echo html_entity_decode($records);
}
}
?>

Why query not working in from my php

I have query, and i have database. When I query in dbforge(mysql) i got result. When from php, i got nothing. Can't understand why? If I write query simple like "select * from table where id = 1", its working.
Below my code:
<?php
$conn = mysqli_connect("localhost", "username", "pass", "db");
ini_set('max_execution_time', 300);
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
mysqli_query($conn, "SET CHARACTER SET utf8 ");
$result = mysqli_query($conn, "Select distinct level_two FROM reportls._report_sales_report Where level_one = 'Музыка'");
if($result == false)
{
die('INVALID QUERY: '.mysqli_error($conn));
echo "error";
}
while ($data = mysqli_fetch_row($result))
{
if(!empty($data[0]))
$sendBack = $sendBack."<option value=\"$data[0]\">$data[0]</option>";
}
echo ($sendBack);
$sendBack = '';
mysqli_free_result($result);
?>
This code return nothing. But if I paste my query into my dbforge, it returns rows.
Could you help me, or write some hits? What i wrote not correct?

Categories