brian914 Posted July 11, 2008 Share Posted July 11, 2008 I am learning php/mySQL and am following this tutorial, which is great, until you make a mistake or something is not quite the when they have it and there is no expaination how to get back on track. Right now, I am trying to cennect from my php document to my database like this: <? $connection = mysql_connect("localhost","root","OLtlre_PHP07"); if(!$connection){ die("Database connection failed: " . mysql_error()); } ?> <html>...</html> The issue is that I think I might not be connected correctly, because when I change the password to something I know is not right, I get no error message. What could be wrong? Could it just be that my error reporting from php is turned off? I am so new to this, that I don't even know where to start. I do have my phpMyAdmin. Thanks a lot for any help! Quote Link to comment Share on other sites More sharing options...
waynewex Posted July 11, 2008 Share Posted July 11, 2008 1: Make sure it's localhost. 2: You're not trying to connect to a remote db are you? Why not: $connection = mysql_connect("localhost","root","OLtlre_PHP07") or die(mysql_error()); Shooting in the dark I know. Quote Link to comment Share on other sites More sharing options...
john-formby Posted July 11, 2008 Share Posted July 11, 2008 This is what I use to connect to my databases: <?php $dbHost = "localhost"; // Database Connection Details - host $dbUser = "root"; // Database Connection Details - username $dbPass = "blah"; // Database Connection Details - password $dbname = "testdb"; // Database Connection Details - database name $db = mysql_connect($dbHost,$dbUser,$dbPass); // Connection Code mysql_select_db($dbname,$db); // Connects to database ?> Try that and see if you have any problems Quote Link to comment Share on other sites More sharing options...
waynewex Posted July 11, 2008 Share Posted July 11, 2008 $connection = mysql_connect("localhost","root","OLtlre_PHP07"); $select_db = mysql_query("use databasename"); if(!$select_db){ echo "Could not select database"; } Try. Quote Link to comment Share on other sites More sharing options...
brian914 Posted July 11, 2008 Author Share Posted July 11, 2008 It is local, but I am wondering if it might be named different? I noticed that in a previous tutorial, when looking at cookies, in the list mine was named a bit different then in the tutorial, where it was called "localhost". How can I find that out? Quote Link to comment Share on other sites More sharing options...
brian914 Posted July 11, 2008 Author Share Posted July 11, 2008 I tried this. <? $connection = mysql_connect("localhost","root","Otl_PHP07"); $select_db = mysql_query("use databasename"); if(!$select_db){ echo "Could not select database"; } ?> <html> <head> <title>Basic</title> </head> <body> </body> </html> It loads the file, but nothing happens. Do I have something in my html? Thanks a lot!!! Quote Link to comment Share on other sites More sharing options...
waynewex Posted July 11, 2008 Share Posted July 11, 2008 If you're on a webhost, check because sometimes they don't have it on your localhost. Sometimes it can be localhost:8080 Quote Link to comment Share on other sites More sharing options...
brian914 Posted July 11, 2008 Author Share Posted July 11, 2008 this is on my computer. Quote Link to comment Share on other sites More sharing options...
waynewex Posted July 11, 2008 Share Posted July 11, 2008 You don't have anything in your HTML? Does the title Basic turn up? Quote Link to comment Share on other sites More sharing options...
waynewex Posted July 11, 2008 Share Posted July 11, 2008 You didn't specify the database name that you're trying to connect to. look at the mysql_query("use databasename") part. Change databasename to the name of your database. Quote Link to comment Share on other sites More sharing options...
brian914 Posted July 11, 2008 Author Share Posted July 11, 2008 Yeah, the title changes to Basic, but that is all that changes. <? $connection = mysql_connect("localhost","root","Otl_PHP07"); $select_db = mysql_query("widget_corp"); if(!$select_db){ echo "Could not select database"; } ?> <html> <head> <title>Basic</title> </head> <body> </body> </html> Quote Link to comment Share on other sites More sharing options...
waynewex Posted July 11, 2008 Share Posted July 11, 2008 It needs to be: <? $connection = mysql_connect("localhost","root","Otl_PHP07"); $select_db = mysql_query("use widget_corp"); if(!$select_db){ echo "Could not select database"; } else{ $success = true; } ?> <html> <head> <title>Basic</title> </head> <body> <?php if(isset($success)){ echo "connected!"; } ?> </body> </html> Now. You're connected but you're not displaying any data from the database. That's why it's blank? Connecting isn't enough. You need to connect and then select data from the database. Try my above code to see if it's a success. Quote Link to comment Share on other sites More sharing options...
brian914 Posted July 11, 2008 Author Share Posted July 11, 2008 Still nothing. So what does that mean? I am not connecting? Thanks so much for your help! Brian Quote Link to comment Share on other sites More sharing options...
waynewex Posted July 11, 2008 Share Posted July 11, 2008 Okay. Some very basic questions so forgive me if they offend you: 1: Have you created a database? 2: If you have. Make sure that you're connecting to the database and not the table. 3: Usually mysql on your computer doesn't have a password, unless you set one yourself. Did you set one? Quote Link to comment Share on other sites More sharing options...
waynewex Posted July 11, 2008 Share Posted July 11, 2008 Also, use <?php instead of <? Quote Link to comment Share on other sites More sharing options...
brian914 Posted July 11, 2008 Author Share Posted July 11, 2008 Haha, I need very basic, so offense taken. 1: Have you created a database? Yes, it is called "widget_corp" 2: If you have. Make sure that you're connecting to the database and not the table. How do I do this? I thought that is what we were doing above? 3: Usually mysql on your computer doesn't have a password, unless you set one yourself. Did you set one? Part of the tutorial I am doing was setting up a password via the commend line. So, yes, I do have a password. Thank you! Quote Link to comment Share on other sites More sharing options...
brian914 Posted July 11, 2008 Author Share Posted July 11, 2008 OMG, that was it!!!! <?php instead of <? Sorry, I am so lame. I just could not find that and don't know enough to figure it out! Thank you so much for your help!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.