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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.