ARTICLES

mySQL connect function in PHP

webpoet June 6, 2004 at 20:24:32 Pacific
xp pro, 2400+ / 768 DDR

In the WinMySQLAdmin under the my .ini Setup tab it says:

user=Brad
password=password

my PHP reads:

$conn = mysql_connect("localhost", "Brad", "password");
echo $conn;

I get this error message:

Warning: mysql_connect(): Access denied for user: 'Brad@localhost' (Using password: YES) in .../mysql_test.php on line 9

If anyone knows why I can't log into my own database, I would greatly appreciate any help!! (I'm able to create and edit my databases in mysql.exe)

Thanks!



Google Ads

#1
+1
ksg June 7, 2004 at 08:05:58 Pacific

Hi,

Use the command line mysql program to do these steps.

Create your database:
mysql> CREATE DATABASE test_database;
mysql> USE test_database;
mysql> CREATE TABLE test_table (firstname VARCHAR(20), lastname VARCHAR(20));
mysql> INSERT INTO test_table VALUES ('John', 'Doe');
mysql> GRANT ALL PRIVILEGES ON test_table.* TO [insert_username_here]@localhost IDENTIFIED BY '[insert_password_here]';
mysql> FLUSH PRIVILEGES;

Ok, now you'll need the PHP code to read from the database...

<?

// Define variables
$host="localhost"; // location of MySQL server
$user="john"; // username you set in the GRANT ALL PRIVILEGES... query
$pass="secret"; // password you set in the GRANT ALL PRIVILEGES... query
$db="test_database"; // database to connect to
$query="select * from test_table;"; // query to run on database

// Connect to the database
$link = mysql_connect($host, $user, $pass) or die("Could not connect to database: " . mysql_error());

// Select the correct database
mysql_select_db($db) or die("Could not select database: " . mysql_error());

// Run a query on the database
$result=mysql_query($query) or die("Could not run query: " . mysql_error());

// Show results
$rows=mysql_num_rows($result);
$fields=mysql_num_fields($result);

print("<table>"); // Start the whole new table in the HTML page

// Show the result
for($i=0; $i<$rows; $i++) { // number of rows

print("<tr>"); // Start a new row in the HTML page
$row=mysql_fetch_row($result); // Get a row of data

for($k=0; $k<$fields; $k++) { // number of fields
print("<td>"); // Start a new cell in the HTML page
print("$row[$k]"); // Print the field data
print("</td>"); // End the cell in the HTML page
}

print("</tr>"); // End the row in the HTML page
}

print("</table>"); // End the whole table in the HTML page

// Free result in memory
mysql_free_result($result);

// Close the connection
mysql_close($link);

?>

Hope this helps!!


Start New Discussion Reply to Message Icon
Related Posts

« C++: How to inherit a tem... Keyboard Capturing »


This post is quite old and has been locked from receiving new replies. Please create a new posting instead.



Ask the Community!
Describe your Problem
Example: Hard Drive Not Detected on My PC


Google Ads



Results for: mySQL connect function in PHP

Multiple Function in PHP www.computing.net/answers/programming/multiple-function-in-php/14325.html

MySQL Connection problem form PHP script www.computing.net/answers/programming/mysql-connection-problem-form-php-script-/20666.html

problem with date() function in PHP www.computing.net/answers/programming/problem-with-date-function-in-php/8855.html