1

I am testing a local page using xampp. Searching how to fix a "No database selected" mysql error lead me to testing this out:

mysql_connect('localhost') or die ("Connect error");

$res = mysql_query("SHOW DATABASES");
while ($row = mysql_fetch_row($res))
{
    echo $row[0], '<br/>';
}

But that query only shows two results: "information_schema" and "test". Which I don't understand cause on phpMyAdmin I can see all this databases:

cdcol, information_schema, mysql, performance_schema, phpmyadmin, test, xxxxxxxx_db (the one I created) and webauth

And (on phpMyAdmin) I can see that root has privileges to this database. Root has no password.

Thanks in advance.


EDIT:

I have an actual site online, I use mysql_connect and such to connect to the database... But I had to reinstall xampp and dreamweaver and everything on my PC and then had to download the database and the whole site cause I need to test some things locally. I can't afforrd to change the mysql querys and use PDO for now, maybe later when I have the time.

My main problem is the connection, it doesn't work, It prints "No database selected". I don't know why it doesn't find my xxxxxxxx_db database.

5
  • 2
    use PDO as mysql_* is deprecated. Commented Jan 12, 2014 at 12:58
  • 1
    What does this print: echo ini_get('mysql.default_username'), ini_get('mysql.default_password');? Commented Jan 12, 2014 at 13:03
  • Should be mysql_fetch_array() Commented Jan 12, 2014 at 13:11
  • 1
    @iBrazilian2 PDO printed my databases. I guess I'm gonna try conecting via PDO and not mysql_*... It used to work before though, a few months ago before I changed PCs. Commented Jan 12, 2014 at 13:39
  • @DanFromGermany That prints absolutely nothing :( Mr.Alien That shows the same results Commented Jan 12, 2014 at 13:40

2 Answers 2

1

In the comments you said your default config is empty, so when you connect to mysql, you have to pass the username to the function:

$connection = mysql_connect('localhost', 'root', '') or die ("Connect error");

You should also keep the connectino resource in a variable and pass it to the query:

$res = mysql_query("SHOW DATABASES", $connection);
while ($row = mysql_fetch_row($res))
{
    echo $row[0], '<br/>';
}

If you cannot rewrite all the statements, well, you SHOULD sooner or later you will have even more trouble.

Sign up to request clarification or add additional context in comments.

1 Comment

Passing the username (root) works perfectly. Wow, I didn't know I was connecting to nothing, that's why It only showed two databases. I actually thought I was connecting to root. Thanks a lot. @JenniferDias solution also works.
1

Please try this:

$link = mysqli_connect('localhost', 'root', '');

if ($res = mysqli_query($link, 'SHOW DATABASES')) {
    echo 'Connected.';
} else {
    echo 'Error occurred', mysqli_error($link);
}

while ($row = mysqli_fetch_row($res)) {
    echo $row[0], '<br/>';
}

4 Comments

It prints this: Connected Warning: mysql_fetch_row() expects parameter 1 to be resource, object given in C:\xampp\htdocs_____________________.php on line 13
try this one Or use mysqli_fetch_array()
mysqli_fetch_array() works! thanks Jennifer. Problem solved, now on to the other problems :P Cheers
Really great improvement Martin Bean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.