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!
tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hello,
I've gotten PHP to connect to a mySQL database successfully. But now I'm running into trouble. I'm trying to display the results of a query on a webpage. In the db, I've got a table named member. I've tried the following code inside my php page, but it doesn't work:
tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
only quick but it should be something like this assuming it is just for one row:
tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I'm not sure which problem is easier to debug, but I'll go with the Rubberjohn version, because no database selected sounds like a big problem : ) Below is the code (with the username and password changed):
How about the mysql close in the middle of all of it.
You make a connection.
than you make the query.
than you close the connection, and than you are trying to retrieve the rows.
#$num=mysql_num_rows($result);
#mysql_close();
#echo "<b><center>Database Output</center></b><br/><br/>";
#if($num > 0){
// however try this.
mysql_connect($host,$user,$password) or die (mysql_error());
mysql_select_db($database) or die (mysql_error());
# the following will/should show you all results. use WHERE if you'd like just one row
$query=mysql_query("SELECT * FROM member") or die (mysql_error());
$result = mysql_fetch_array($query);
if ($result) {
while($row = mysql_fetch_array($result)) {
echo $row['mnum']; # and all other rows
}
}
Thanks. That results in an error message showing the access is denied for the user. I think this means I have the wrong username and password. If so, this may be getting beyond the scope of this particular forum. But I have three questions:
(1) Is there anything else that error message can mean besides I've got the username and/or password incorrect? (the one that says
Access denied for user 'dagoodga_dagoodg'@'localhost' to database 'database'
)
(2) Does it matter where the PHP is located on the server? Like, would it cause problems or not work correctly if it was in the wrong folder (oops, I mean Directory ... I think I just blew my cover and gave away that I'm a window's user - but I don't use windows for a server!)
or (3) Is there anything else you could/should use instead of localhost? Are there some circumstances where that doesn't work and you need something else (or does that always work as long as the code is on the same server as the database?)
Thanks a lot for putting up with all my neebie questions,
That means your user doesn't have access rights for that database. You will need to either adjust the rights for the user in your control panel, or ask your host for help on that front.
php itself? as long as the server knows where it is, it rarely makes a difference where php itself is located. If you're talking about the file, it only matters when you are making reference to file system paths or certain other things that your code, as yet shown, does not use.
Although rare, I have seen using 127.0.0.1 (the IP version of localhost, basically) can work better for various weird reasons. The circumstances that it fails are very specific as far as I know...
Thanks so much! I figured out the problem with the username and password - I had created the username, but I hadn't assigned it to the database. After that, I went back over the suggestions for displaying query results, and I got a code that worked! I'm so excited! Thanks so much! In case anyone else is have the same problems, here's the complete code (with situation-specific info changed, of course) that worked for me:
<?php // Script 2.3 - variables.php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
$dbh=mysql_connect ("localhost", "USERNAME", "PASSWORD") or die ('I cannot connect to the database because: ' . mysql_error());
if (!$dbh) {
echo "could not connect to database";
} else {
mysql_select_db('DATABASE_NAME', $dbh) or die(mysql_error());
$query="SELECT * FROM someTable"; //or any mySQL query
$result=mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo '<td style="width:50%;">';
echo $row['mnum']; //or any field name instead of mnum
echo '</td>';
echo '<td style="width:50%;">';
echo $row['mname']; //any other field name instead of mname
echo '</td>';
}
mysql_close($dbh);
}
?>
DaGoodGames
(if I knew a way to close this thread, or mark it as "solved" I would, but I don't see any way ...)