Page 1 of 1
PHP MYSQL connection problem
Posted: Tue Dec 28, 2010 1:45 pm
by zxkelxz
Code: Select all
<?php
$link = mysql_connect('localhost', 'kel', 'meme222');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
$db_selected = mysql_select_db('test');
if (!$db_selected) {
die(' Could not select database: ' . mysql_error());
}
$query = 'SELECT name, pswd';
?>
Output:
Connected successfully Could not select database: Access denied for user 'www-data'@'localhost' (using password: NO)
What's wrong? I don't get it it. I've even tried logging in as 'root' with the same problems.
Re: PHP MYSQL connection problem
Posted: Tue Dec 28, 2010 3:14 pm
by McInfo
Just like you can't edit a file after you close it, you can't use a database connection after you close it.
Re: PHP MYSQL connection problem
Posted: Tue Dec 28, 2010 4:08 pm
by zxkelxz
Lol, missed that one totally.
So here derives another question
Code: Select all
<?php
$username = "kel";
$password = "meme222";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("test",$dbhandle)
or die("Could not select test");
echo "Selected Test<br>";
//execute the SQL query and return records
$result = mysql_query("SELECT name, pswd");
///what do i put here to bring my query to screen
//close the connection
mysql_close($dbhandle);
?>
How do I output what I've gotten to screen.
Re: PHP MYSQL connection problem
Posted: Tue Dec 28, 2010 4:39 pm
by danwguy
I use ...
Code: Select all
while($row = mysql_fetch_array($result)) {
$name = $row['name'];
$pwd = $row['pswd'];
}
Then you can either
or ...
Code: Select all
?>
<html><body>
<p>user name is: <?php echo $name; ?><br />
password is: <?php echo $pwd; ?><br />
either one of those would work and they will loop through all the rows in the db and echo each one with a break in the middle of them. hope that helps.
Re: PHP MYSQL connection problem
Posted: Thu Dec 30, 2010 9:02 am
by zxkelxz
I use the code above, the start of the script is fine. But after adding your code and some other example code, I still can't see the user name or password output to the screen. Anyone know what I could be doing wrong? My code now looks exactly like this.
Code: Select all
<?php
$username = "kel";
$password = "meme222";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("test",$dbhandle)
or die("Could not select test");
echo "Selected Test<br>";
//execute the SQL query and return records
$result = mysql_query("SELECT name, pswd");
///what do i put here to bring my query to screen
while($row = mysql_fetch_array($result)) {
$name = $row['name'];
$pwd = $row['pswd'];
}
echo $name;
echo $pwd;
//close the connection
mysql_close($dbhandle);
?>
I'm sorry I'm asking so many questions but I'm really new at this. Also turned out the book we had is outdated. The only conclusion I can come to is I'm returning NULL, in which it wouldn't display to screen. Very confused on why this would be because my databases name is test, tables name is user, table rows are name, pswd. But I'm getting nothing back on the return.
Re: PHP MYSQL connection problem
Posted: Thu Dec 30, 2010 9:30 am
by zxkelxz
OK, I finally got it. Forgot how hard it is wondering through new territory. I kept attempting to use my database instead of table "user" in query.
Also thank you guys for the help and guidance you have offered. It is appreciated.