a weird error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

yes...i corrected it...sorry bout that...now it says

Debug: mysqli_connect(localhost,****,****)
Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user '*****'@'localhost' (using password: YES) in that\anonymous\directory on line 11
Couldn't connect to server.

line 11 is

Code: Select all

$cxn = mysqli_connect($host,$user,$password)
im guessing couldent connect to server and the fact that it was showing the user name and the password is a bad thing.....lol...how do i fix this
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Obadiah wrote:it was showing the user name and the password is a bad thing
yes it is. Was only meant for debugging purposes, but still a bad idea.
Obadiah wrote:.....lol...how do i fix this
You're kidding, yes?
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

@volka= no...i wish i were but im just following a tutorial....theoretically it should have worked and i shouldent be having these errors
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

echo "Debug: mysqli_connect($host,$user,$password)";
This line prints the parameters/credentials for the database connection. Just remove it.
Obadiah wrote:..but what password is it looking for is it looking for the mySQLadmin, the phpMYAdmin() bc thats where i created the database in....or am i still miles away from the answer...anyone?
You need valid mysql credentials. http://dev.mysql.com/doc/refman/5.1/en/privileges.html explains it in detail.
Who installed the mysql server and/or phpmyadmin?
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

i installed it with xampp as a pakage at the time i was looking for a way to set up php and mySQL to work together and you told me about Xampp...it is a really nice setup...im just having the hardest time getting this program to work...i read that manual by the way it said that
As a user, when you connect to a MySQL server, your identity is determined by the host from which you connect and the username you specify. When you issue requests after connecting, the system grants privileges according to your identity and what you want to do.
so i took it as...if im the administrator of the machine im using....and i put in the user name and the password for mySQL it should work but if i installed php, apache and mySQL as a pakage then the username and password for mySQL admin was defined for me...how do i connect?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I've never used xampp but is the username "root" and the password "secret" ?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ditch that comment:
* MySQL and PHP
MySQL starts without a password for "root". So in PHP you can connect the MySQL-Server with: mysql_connect("localhost","root","");
If you want to set a password for "root" in MySQL, please use "mysqladmin" under Console. For example:

\...\xampp\mysql\bin\mysqladmin -u root password secret

Attention. After changing the password for root, don't forget to inform PHPMyAdmin. Search the "config.inc.php" under \...\xampp\phpmyadmin\ and edit the following lines:

$cfg['Servers'][$i]['user'] = 'root'; // MySQL SuperUser
$cfg['Servers'][$i]['auth_type'] = 'http'; // HTTP MySQL authentification

Now the correct password for "root" is required, before PHPMyAdmin starts.

Please see also the three methods in the Windows FAQ:
http://www.apachefriends.org/en/faq-xam ... #password0
The username is "root" and the password is "".
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Im sure the username is "root" and there is no password

EDIT| d11wtq beat me to it
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

k....i figured that out guys...sorry about stressin u out but finally im connected :D

Warning: mysqli_query() expects parameter 1 to be mysqli, null given
Coulden't execute query 1


line 12 of that file is from the block

Code: Select all

case "Login": 
    $cxn = Connect_to_db("Vars.php"); 
    $sql = "SELECT user_name FROM $table_name 
                        WHERE user_name='$_POST[fusername]'"; 
    $result = mysqli_query($cxn,$sql) or die("Coulden't execute query 1"); 
    $num = mysqli_num_rows($result);
more specifically line 12 is just a debug i guess that says

Code: Select all

$result = mysqli_query($cxn,$sql) or die("Coulden't execute query 1");
what does it mean?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
function Connect_to_db($filename)
{
        include($filename);
        $cxn = mysqli_connect($host,$user,$password)
                or die ("Couldn't connect to server.");
        $db = mysqli_select_db($cxn,$database)
                or die ("Coulden't select database.");
}
?>
This function does not return anything.
Therefore $cnx = Connect_to_db(...) set $cnx tu null.

see http://de2.php.net/return and http://de2.php.net/language.variables.scope
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
case "Login":
// This var $cxn needs to be a connection resource. Is it one?
    $cxn = Connect_to_db("Vars.php");
    $sql = "SELECT user_name FROM $table_name
                        WHERE user_name='$_POST[fusername]'";
    $result = mysqli_query($cxn,$sql) or die("Coulden't execute query 1");
    $num = mysqli_num_rows($result);
?>
EDIT | Try this in your function...

Code: Select all

<?php
function Connect_to_db($filename)
{
        include($filename);
        $cxn = mysqli_connect($host,$user,$password)
                or die ("Couldn't connect to server.");
        $db = mysqli_select_db($cxn,$database)
                or die ("Coulden't select database.");

        return $cxn;
}
?>
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

i should have included the entire case statement in my last post which may have been helpful

Code: Select all

<?php 
session_start(); 
include("functions_main.php"); 
$table_name = "Customer"; 
$next_program = "SecretPage.php"; 

switch(@$_POST['Button']) { 
  case "Login": // error causing case begins here
    $cxn = Connect_to_db("Vars.php"); 
    $sql = "SELECT user_name FROM $table_name 
                        WHERE user_name='$_POST[fusername]'"; 
    $result = mysqli_query($cxn,$sql) or die("Coulden't execute query 1"); 
    $num = mysqli_num_rows($result); 
    if($num == 1) { 
      $sql = "SELECT user_name FROM $table_name 
                                WHERE user_name='$_POST[fusername]' 
                                AND password=md5('$_POST[fpassword]')"; 
      $result2 = mysqli_query($cxn,$sql) 
      or die("Coulden't execute query 2"); 
      $row = mysqli_fetch_assoc($result2); 
      if($row) { 
        $_SESSION['auth']="yes"; 
        $_SESSION['logname']=$_POST['fusername']; 
        header("Location: $next_program"); 
      } else { 
        $message_1="The Login Name, '$_POST[fusername]' 
                                        exist, but you have not entered the 
                                        correct password! Please try again.<br>"; 
        extract($_POST); 
        include("fields_login.php"); 
        include("double_form.php"); 
      } 
    } elseif($num == 0) { 
       //login name not found 
      $message_1 = "The User Name you entered does not 
                                        exist! Pease try again.<br>"; 
      include("fields_login.php"); 
      include("double_form.php"); 
    }  // error causing case ends here
break; 

  case "Register": 
    /* Check for blanks */ 
    foreach($_POST as $field => $value) { 
      if($field != "fax") { 
        if($value == "") { 
          $blanks[] = $field; 
        } 
      } 
    } 
    if(isset($blanks)) { 
      $message_2 = "The Following fields are blank. 
                                        Please enter the required information: "; 
      foreach($blanks as $value) { 
        $message_2 .="$value, "; 
      } 
      extract($_POST); 
      include("fields_login.php"); 
      include("double_form.php"); 
      exit(); 
    } 
    /* validate data */ 
    foreach($_POST as $field => $value) { 
      if(!empty($value)) { 
        if(eregi("name",$field) and !eregi("user",$field) and !eregi("log",$field)) { 
          if(!ereg("^[A-Za-z' -]{1,50}$",$value)) { 
            $errors[] = "$value is not a valid name."; 
          } 
        } 
        if(eregi("street",$field) or eregi("addr",$field) or eregi("city",$field)) { 
          if(!ereg("^[A-Za-z0-9.,' -]{1,50}$",$value)) { 
            $errors[] = "$value is not a valid address or city."; 
          } 
        } 
        if(eregi("state",$field)) { 
          if(!ereg("[A-Za-z]",$value)) { 
            $errors[] = "$value is not a valid state."; 
          } 
        } 
        if(eregi("email",$field)) { 
          if(!ereg("^.+@.+\\..+$",$value)) { 
            $errors[] = "$value is not a valid email address."; 
          } 
        } 
        if(eregi("zip",$field)) { 
          if(!ereg("^[0-9]{5,5}(\-[0-9]{4,4})?$",$value)) { 
            $errors[] = "$value is not a valid zipcode."; 
          } 
        } 
        if(eregi("phone",$field) or eregi("fax",$field)) { 
          if(!ereg("^[0-9)(xX -]{7,20}$",$value)) { 
            $errors[] = "$value is not a valid phone number."; 
          } 
        } 
      } 
    } 
    foreach($_POST as $field => $value) { 
      if(field != "password") { 
        $password = strip_tags(trim($value)); 
      } else { 
        $fields[]=$field; 
        $value = strip_tags(trim($value)); 
        $values[] = addslashes($value); 
        $$field = $value; 
      } 
    } 

    if(@is_array($errors)) { 
      $message_2 = ""; 
      foreach($errors as $value) { 
        $message_2 .= $value." Please try again<br />"; 
      } 
      include("fields_login.php"); 
      include("double_form.php"); 
      exit(); 
    } 
    $user_name = $_POST['user_name']; 

    /* check to see if user name already exist */ 
    $cxn = Connect_to_db("Vars.php"); 
    $sql = "SELECT user_name FROM $table_name WHERE user_name='$user_name'"; 
    $result = mysqli_query($cxn,$sql) or die("Coulden't execute query."); 
    $num = mysqli_num_rows($result); 
    if($num > 0) { 
      $message_2 = "$user_name already used. Select another 
                                                User Name."; 
      include("fields_login.php"); 
      include("double_form.php"); 
      exit(); 
    } else { 
      $today = date("Y-m-d"); 
      $fields_str = implode(",",$fields); 
      $values_str = implode('","',$values); 
      $fields_str .=",create_date"; 
      $values_str .='"'.",".'"'.$today; 
      $fields_str .=",password"; 
      $values_str .= '"'.","."md5"."('".$password."')"; 
      $sql = "INSERT INTO $table_name "; 
      $sql .= "(".$fields_str.")"; 
      $sql .= " VALUES "; 
      $sql .= "(".'"'.$values_str.")"; 
      mysqli_query($cxn,$sql) or die(mysqli_error($cxn)); 
      $_SESSION['auth']="yes"; 
      $_SESSION['logname']=$user_name; 

      /* send email to new Customer */ 

      $emess = "You have successfully registered."; 
      $emess .= "Your new user name and password are:"; 
      $emess .= "\n\n\t$user_name\n\t"; 
      $emess .= "password\n\n"; 
      $emess .= "We apprieciate your interest. \n\n"; 
      $emess .= "If you have any questions or problems,"; 
      $emess .= " email obadiah_00@hotmil.com"; 
      $subj .= "Your new customer registration"; 
      $mailsend=mail("$email","$subj","$emess"); 
      header("Location: $next_program"); 
    } 
  break; 

  default: 
    include("fields_login.php"); 
    include("double_form.php"); 
} 
?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

What does the function Connect_to_db() look like? It looks like it's not returning anything, or at least returning NULL as has already been suggested.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

this is the connect_to_ DB function()

Code: Select all

<?php 
function Connect_to_db($filename) 
{ 
        require $filename; 
        
        if ( !isset($host,$user,$password,$database) ) { 
                die('one or more required connection parameters not set'); 
        } 
        $cxn = mysqli_connect($host,$user,$password) 
                or die ("Couldn't connect to server."); 
        $db = mysqli_select_db($cxn,$database) 
                or die ("Coulden't select database."); 
} 
?>
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

ahaa.....that did it
Post Reply