PHP MYSQL connection problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zxkelxz
Forum Newbie
Posts: 11
Joined: Wed Dec 22, 2010 9:17 am

PHP MYSQL connection problem

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP MYSQL connection problem

Post by McInfo »

Code: Select all

mysql_close($link);
Just like you can't edit a file after you close it, you can't use a database connection after you close it.
zxkelxz
Forum Newbie
Posts: 11
Joined: Wed Dec 22, 2010 9:17 am

Re: PHP MYSQL connection problem

Post 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.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: PHP MYSQL connection problem

Post by danwguy »

I use ...

Code: Select all

while($row = mysql_fetch_array($result)) {
$name = $row['name'];
$pwd = $row['pswd'];
}
Then you can either

Code: Select all

echo $name;
echo $pwd;
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.
zxkelxz
Forum Newbie
Posts: 11
Joined: Wed Dec 22, 2010 9:17 am

Re: PHP MYSQL connection problem

Post 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.
zxkelxz
Forum Newbie
Posts: 11
Joined: Wed Dec 22, 2010 9:17 am

Re: PHP MYSQL connection problem

Post 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.
Post Reply