DeveloperSide.NET Forums
July 09, 2020, 10:12:16 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: PHP - Accessing a Database & Redirecting To a Different  (Read 5972 times)
0 Members and 1 Guest are viewing this topic.
dzmay
Member
*
Posts: 3


View Profile
« on: February 06, 2006, 07:27:48 PM »

I haven't written in php for years and the code i do know is limited. I'm trying to access a database using php and mysql. All i get is a blank page and nothing happen's! From what i have read i need to change some settings in the php.ini file but i'm unsure where this is or what i should change.

Below is some of the code i'm using could somone please help so i can do the following:

1. Connect to and use the data within a database

2. Redirect to another page according to some condition, does this work? header("location: existinglogon.php" ); //redirect to login page

3. See any errors caused by my code (instead of a blank screen)

4. Find and change the appropriate php.ini file

My Code:

Code in Connection.php
<?
   define('HOST','localhost');
   define('USER','root');
   define('PASSWORD','password');
   define('DATABASE','freefall');
?>

Other Code Using Connection.php
<?
   include_once('connection.php');
   
   $username = $txtusername;
   $password = $txtpassword;
   $password2 = $txtpassword2;
   
   $link = mysql_connect(HOST, USER, PASSWORD) or die ('Cannot connect to database');
   mysql_select_db(DATABASE,$link);
   
   $qry = ("SELECT * FROM users WHERE (Username = '$username') ");
   $result = mysql_query($qry,$link);
   
   if (!$result)
   {
      echo "ERROR6: ", mysql_error();
   }
   
   $row = mysql_fetch_array($result);
   
   if ($row['Id'] < 1)
   {
   
      if ($password == $password2)
      {
         $qry = "INSERT INTO users(Username,Password)"."VALUES ('$username','$password')";
         $result = mysql_query($qry,$link);
      
         header("location: existinglogon.php" ); //redirect to login page
      }
      else
      {
         header("location: newlogon.php"); //redirect to current page
      }
   }
   else
   {
      header("location: newlogon.php"); // redirect to current page
   }
?>

Any help would be greatly appreciated
Logged
admin
Administrator
Master of All Subjects
*****
Posts: 3272


View Profile WWW Email
« Reply #1 on: February 06, 2006, 08:32:03 PM »

http://www.php.net/mysqli_select_db
Code:

<?php
$link 
mysqli_connect&#40;"localhost", "my_user", "my_password", "test"&#41;;

/* check connection */
if &#40;mysqli_connect_errno&#40;&#41;&#41; &#123;
   
printf&#40;"Connect failed&#58; %s\n", mysqli_connect_error&#40;&#41;&#41;;
   
exit&#40;&#41;;
&#125;

/* return name of current default database */
if &#40;$result = mysqli_query&#40;$link, "SELECT DATABASE&#40;&#41;"&#41;&#41; &#123;
   
$row mysqli_fetch_row&#40;$result&#41;;
   
printf&#40;"Default database is %s.\n", $row[0&#93;&#41;;
   
mysqli_free_result&#40;$result&#41;;
&#125;

/* change db to world db */
mysqli_select_db&#40;$link, "world"&#41;;

/* return name of current default database */
if &#40;$result = mysqli_query&#40;$link, "SELECT DATABASE&#40;&#41;"&#41;&#41; &#123;
   
$row mysqli_fetch_row&#40;$result&#41;;
   
printf&#40;"Default database is %s.\n", $row[0&#93;&#41;;
   
mysqli_free_result&#40;$result&#41;;
&#125;

mysqli_close&#40;$link&#41;;
?>


You need to already have a database created under MySQL and use the proper MySQL user/password combo -- default is root with no password on localhost.
Logged

DeveloperSide.NET
Advanced PHP and MySQL Solutions for your Web Design and Development needs with Web.Developer Server Suite.
dzmay
Member
*
Posts: 3


View Profile
« Reply #2 on: February 06, 2006, 11:35:18 PM »

Thanks

Tha code works but i would like to know how to do the following aswell:

1. declare my username, password etc in a seperate file so it cant be seen by clicking view source! Possibly using global variables.

2. Redirect someone to another page according to some conditions for example if you enter a password for something wrong it will redirect you to one page otherwise it takes you to another.

for example can i use include_once('mydeclarations.php');

and mydeclarations will contain defines for username, password, database etc?

Many thanks again
Daz
Logged
admin
Administrator
Master of All Subjects
*****
Posts: 3272


View Profile WWW Email
« Reply #3 on: February 07, 2006, 12:30:33 AM »

There is no click on source for php files.  They are parsed before leaving the server. But yes, you could do the include(file.php).
Logged

DeveloperSide.NET
Advanced PHP and MySQL Solutions for your Web Design and Development needs with Web.Developer Server Suite.
dzmay
Member
*
Posts: 3


View Profile
« Reply #4 on: February 07, 2006, 12:41:31 AM »

Cheers i guess i should have tried clicking view source, maybe i would have realised then.

Not sure why but the original code i was using is now working!

Owell thanks for your help

Daz
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.9 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!