Jump to content

Connection Error - Newbie question


brian914

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/114284-connection-error-newbie-question/
Share on other sites

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

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!!!

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>

 

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.

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?

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!

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.