Results 1 to 16 of 16

Thread: [RESOLVED] php: An defined variable:

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    204

    Resolved [RESOLVED] php: An defined variable:

    Hi I have created a login page but each time I run the code I will get the following error message: below is the error message and the and code
    line 135 is highlighted below, Thanks for your time

    Notice: Undefined variable: mysql_connect in C:\xampp\htdocs\tivoliHotel\main_login.php on line 135

    Fatal error: Function name must be a string in C:\xampp\htdocs\tivoliHotel\main_login.php on line 135
    HTML Code:
    <?php
      //server = local testing
      $host= "localhost"; // host Name 
      $username="root"; //Mysql user name
      $password="xyz"; // Mysql Pass word
      $dbname="adminlogindb"; //database Name
      $tbl_Name="stafftbl"; //Table name
       //Connect to the server and select the database
     [U] $mysql_connect("$host", "$username", "$password") or die ("Cannot connect"); [/U] $mysql_select_db("adminlogindb") or die ("Cannot Select Database");
      
      //user name and password sent from the form
      $username =$_POST['myusernamd'];
      $password =$_POST['mypassword'];
      
      // To protect Mysql Injection
      $myusername = stripslashes(myusername);
      $mypassword= stripslashed(mypassword);
      $myusername = mysql_real_excape_string(myusername);
      $mypassword = mysql_real_excape_string(password);
      $sql = "select * from $stafftbl where username = '$myusername' and 
      password = '$mypassword'";
      $result = mysql_query($sql);
      
      //mysql_num_row is counting table row
        $count= $mysql_num_row($result);
    	if($count== 1){
      // register $myusername and $mypassword and redirect to login success.php
        section_register("$myusername");
        section_register("$mypassword");
    	header("location:login_success.php");
    	}
    	else {
    	 echo "Wrong Username or Password";
    	}
       ?>
    Last edited by alobi; Sep 12th, 2013 at 12:24 PM.

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: php: An defined variable:

    mysql_connect isn't a variable... it's a function... so is mysql_select_db...

    1) mysql_connect has been deprecated... should be using mysqli_connect() instead...
    2) As I mentioned, it's a function, so that means it's going to return something... which means you'll proabably want to capture that thing in a variable...
    3) See the documentation; http://php.net/manual/en/function.mysql-connect.php

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    204

    Re: php: An defined variable:

    Quote Originally Posted by techgnome View Post
    mysql_connect isn't a variable... it's a function... so is mysql_select_db...

    1) mysql_connect has been deprecated... should be using mysqli_connect() instead...
    2) As I mentioned, it's a function, so that means it's going to return something... which means you'll proabably want to capture that thing in a variable...
    3) See the documentation; http://php.net/manual/en/function.mysql-connect.php

    -tg
    -tg
    Thank you for pointing out the errors, I am new at php and mysqli, I will continue to work on my skills.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    204

    Re: php: An defined variable:

    I changed the my code but I am still getting the following error message could you please look at Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\tivoliHotel\main_login.php on line 136
    Cannot Select Database my changes are shown below
    <?php
    //server = local testing
    $host= "localhost"; // host Name
    $username="root"; //Mysql user name
    $password="password"; // Mysql Pass word
    $dbname="adminlogindb"; //database Name
    $tbl_Name="stafftbl"; //Table name
    //Connect to the server and select the database
    $conn= mysqli_connect("$host", "$username", "$password") or die ("Error connecting to db");
    $sql=mysqli_select_db($dbname) or die ("Cannot Select Database");


    Quote Originally Posted by alobi View Post
    -tg
    Thank you for pointing out the errors, I am new at php and mysqli, I will continue to work on my skills.

  5. #5
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: php: An defined variable:

    Quote Originally Posted by alobi View Post
    $sql=mysqli_select_db($dbname) or die ("Cannot Select Database");
    This is how I connect to a database using php
    php Code:
    1. <?php
    2. //connect to db
    3. $conn = mysql_connect("host", "user", "password");
    4. if (!$conn) {
    5. die("Connection failed: " .mysql_error());
    6. }
    7. // Database connection variables
    8. $dbDatabase = "your database";
    9. ?>
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: php: An defined variable:

    It tells you exactly what the issue is... you gave it only one parameter and it's looking for two.... it's like driving a car... it requires BOTH gas AND a key... you can't just stick gas in the car and expect it to run unless you also insert the key (the correct one I might add) and turn it...

    samething here... you're calling the sub and it's looking for two pieces of information to be handed to it... that link I posted, it's to the php online documentation... use it... take the name of the function, enter it into the search field... and it'll give you the results... the first link should be to the proper page where it will explain what the parameters are, and even have some examples on how to use it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    204

    Re: php: An defined variable:

    I gave you the wrong code and wrong error message: Here is my latest error message and code.
    $dbname="adminlogindb"; //database Name
    $tbl_Name="stafftbl"; //Table name
    //Connect to the server and select the database
    $conn= mysqli_connect("$host", "$username", "$password") or die ("Error connecting to db");
    $sql=mysqli_select_db("adminlogindb", $conn) or die ("unable to Select Db");


    Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\tivoliHotel\main_login.php on line 136
    unable to Select Db
    Thanks

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    204

    Re: php: An defined variable:

    Quote Originally Posted by Nightwalker83 View Post
    This is how I connect to a database using php
    php Code:
    1. <?php
    2. //connect to db
    3. $conn = mysql_connect("host", "user", "password");
    4. if (!$conn) {
    5. die("Connection failed: " .mysql_error());
    6. }
    7. // Database connection variables
    8. $dbDatabase = "your database";
    9. ?>
    I tried this code and got error message on this line die("Connection failed: " .mysql_error());

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: php: An defined variable:

    sigh.... Look at the documentation...

    the parameters are mysqli $link ... which means it's expecting a mysqli typed variable.... the second parameter is string $dbname ... which means it's expecting a string...

    you may be new to PHP, but with 155 posts, you're not all that new to programming... which is why I'm coming off as a bit of a *****... you can't just throw parameters in any old order you want... if the first parameter is supposed to be a sqlcli parameter... pass one... NOT a string...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] php: An defined variable:

    How did you solve the problem?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: [RESOLVED] php: An defined variable:

    I'm hoping by swapping his parameters around...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    204

    Re: [RESOLVED] php: An defined variable:

    Quote Originally Posted by techgnome View Post
    I'm hoping by swapping his parameters around...

    -tg
    My understanding about the forum is that you came and ask question from people who understands the subject matter better. I understand too that you do not have to subscribe to a thread if you do not feel like it. . I think people should be respectful.
    Thanks
    Al

  13. #13
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: [RESOLVED] php: An defined variable:

    Sure, but it's also common courtesy to let people know how you solved... jsut marking the thread as resolved isn't helpful either. So when you mark a thread as resolved with no indication of what you did is equally rude. So I posted a response such if anyone one else found this thread with the same problem, they would at least know what the solution was. And yes, you're right, I don't have to help if I don't feel like it. Since you didn't seem to be all that appreciative of my replies, I'll simply add you to my list. Doesn't bother me one bit.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    204

    Re: [RESOLVED] php: An defined variable:

    @Nightwalker83 Actually I just decided to leave it alone to for now. It is just too much going on
    AL

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    204

    Re: php: An defined variable:

    This is one of your replies.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    204

    Re: php: An defined variable:

    Below is one of your replies.
    Quote Originally Posted by techgnome View Post
    sigh.... Look at the documentation...

    the parameters are mysqli $link ... which means it's expecting a mysqli typed variable.... the second parameter is string $dbname ... which means it's expecting a string...

    you may be new to PHP, but with 155 posts, you're not all that new to programming... which is why I'm coming off as a bit of a *****... you can't just throw parameters in any old order you want... if the first parameter is supposed to be a sqlcli parameter... pass one... NOT a string...

    -tg

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width