Question:

PHP and MySQL question

by  |  earlier

0 LIKES UnLike

Hello,

I have a wamp server, and have created a database and table (users) in phpmyadmin. I can connect to the server via php, but then I cannot connect to the database. The code I'm using is below:

<?php

$username = "";

$password = "";

$hostname = "localhost";

$dbh = mysql_connect($hostname, $username, $password)

or die("Unable to connect to MySQL");

print "Connected to MySQL<br>";

$selected = mysql_select_db("homegame",$dbh)

or die("Could not select db");

// other stuff here

mysql_close($dbh);

?>

I get the output:

Connected to MySQL

Could not select db

Can anyone tell me what the issue is or give me alternative code? Please make a note if the question is too vague.

Thanks,

Legs

 Tags:

   Report

4 ANSWERS


  1. There are two possible reasons you got that error

    1.) You didn&#039;t make the database

    2.) You didn&#039;t give yourself permission to use the database

    I included a &quot;how to&quot; that will take you step-by-step through both. Good luck!

    P.S. If you have access to your server&#039;s error log, that would help too.

    Edit: What the person before me said, too, although that would only matter if it were a permissions problem (and from what you said, you were able to connect to the db to begin with, so I&#039;m guessing that&#039;s not the case, since you probably haven&#039;t set up a username or password yet).


  2. First, before you use this you MUST set up valid user name and password for the database. Otherwise your system becomes VERY vulnerable. The user should have all rights to the data except the GRANT option. You then control the user actions through your scripts. You must be sure that the database name is correct. Is it homegame? Or Homegame. It is case sensitive. The user must have explicit rights to this database, not the mysql database.

  3. That database does not exist if you can&#039;t select it.

    Use the MySQL command line and the following command: &quot;CREATE DATABASE homegame;&quot;

  4. well, you have to set these fields first:

    $username = &quot;&quot;;

    $password = &quot;&quot;;

    $hostname = &quot;localhost&quot;;

    should be

    $username = &quot;your user name&quot;;

    $password = &quot;your password&quot;;

    $hostname = &quot;localhost&quot;;  
You're reading: PHP and MySQL question

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.