2 Replies - 877 Views - Last Post: 31 August 2011 - 08:48 AM Rate Topic: -----

#1 D_MeNtEd  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 27-August 10

Website linked to database php/html

Posted 30 August 2011 - 01:15 PM

Okay so here's the deal;

Website built [check]
database set up using phpmyadmin and verified to work with below code [check]

<?php

//Sql Connection.php

require_once("database.php");
 
 $dbRecords = mysql_query("SELECT * FROM pwd", $dbLocalhost)
 or die ("Problem reading table:" . mysql_error());
 echo "<h1> Connected to Database yay! </h1>";
 
 
 ?>




database.php file
<?php

//database.php

$dbLocalhost = mysql_connect("localhost","root")
 or die("Could not Connect: " . mysql_error());
 
 mysql_select_db("users",$dbLocalhost)
 or die ("Could not find database:" . mysql_error());
 echo "<h1> Connected to Database</h1>";
 
 ?>



Okay, so now I need to have the website update the database. Simple concept - just adding new users so it's two fields. Username and password.

I'm pretty new to this (school assignment) so I've been looking around online the last couple of days.

I've created a config.php file (not sure if I need this?) But I'm still not getting any luck having the site update to the database.

My questions :

Does the config.php file need to be referenced on all of the html pages on the website?

What other steps are missing? (it seems like there's something else that needs to be running between the site and db).

I am running WAMP5 for a local server.

Any thoughts or a push in the right direction would be awesome!

Thanks,
D

Is This A Good Question/Topic? 0
  • +

Replies To: Website linked to database php/html

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: Website linked to database php/html

Posted 30 August 2011 - 02:15 PM

Well you have your connections established, so now you need to include the database.php anywhere you need to establish a connection. If you put this into your config.php, then you would add your config.php to any page that needs to open a database connection and the configuration options.

Example 1... I need just a database connection.

include_once("database.php");

// Use mysql_query to ask database for info
$results = mysql_query("select * from table");



Example 2... I need to have a connection but also configuration data. Inside config.php I would have included database.php. Then in any document I want to have config data (and the database) I would do something like the following...

include_once("config.php");

// Use config data here (perhaps get_table() is a function in config.php)
$table = get_table();

// Use mysql_query to ask database for info
$results = mysql_query("select * from $table");



Hopefully you get the idea. You include database.php anywhere you need the database connection and include config.php anywhere you want your configuration data to be used.

You don't HAVE TO have any config.php. That is a design choice. Some scripts like to have all configuration variables/functions in one config.php file to make including it into any file that needs configuration data easier.

:)

This post has been edited by Martyr2: 30 August 2011 - 02:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 D_MeNtEd  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 27-August 10

Re: Website linked to database php/html

Posted 31 August 2011 - 08:48 AM

Okay...

So I've got a database.php file that I can get to work fine (shown below) on a localhost connection. But when I go to connect through the server I'm getting the following error;

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in /home/webspace/public_html/name/database.php on line 5
Could not Connect: Access denied for user 'root'@'localhost' (using password: YES)


I've tried going into the server and changing the password... Also tried setting it with no password and leaving the pwd part as "".. But it's not working.

database.php file.
<?php

//database.php

$dbLocalhost = mysql_connect("localhost","root", "pwd")
 or die("Could not Connect: " . mysql_error());
 
 mysql_select_db("users",$dbLocalhost)
 or die ("Could not find database:" . mysql_error());
 
 
 ?>



I've also used the below code to establish that the link is working on a local host.

<?php

//Sql Connection.php

require_once("database.php");
 
 $dbRecords = mysql_query("SELECT * FROM pwd", $dbLocalhost)
 or die ("Problem reading table:" . mysql_error());
 echo "<h1> Connected to Database yay! </h1>";

?>




I haven't yet tried out the config.php file to update/alter the database, since I can't seem to connect to the database.

Is there a work around for the pwd part on this?

Thanks,

D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1