-
Sep 12th, 2013, 12:20 PM
#1
Thread Starter
Addicted Member
[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.
-
Sep 12th, 2013, 12:41 PM
#2
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
-
Sep 13th, 2013, 04:35 AM
#3
Thread Starter
Addicted Member
Re: php: An defined variable:
Originally Posted by techgnome
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.
-
Sep 13th, 2013, 07:31 AM
#4
Thread Starter
Addicted Member
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");
Originally Posted by alobi
-tg
Thank you for pointing out the errors, I am new at php and mysqli, I will continue to work on my skills.
-
Sep 13th, 2013, 07:49 AM
#5
Re: php: An defined variable:
Originally Posted by alobi
$sql=mysqli_select_db($dbname) or die ("Cannot Select Database");
This is how I connect to a database using php
php Code:
<?php //connect to db $conn = mysql_connect("host", "user", "password"); if (!$conn) { die("Connection failed: " .mysql_error()); } // Database connection variables $dbDatabase = "your database"; ?>
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
-
Sep 13th, 2013, 07:49 AM
#6
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
-
Sep 13th, 2013, 08:40 AM
#7
Thread Starter
Addicted Member
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
-
Sep 13th, 2013, 08:44 AM
#8
Thread Starter
Addicted Member
Re: php: An defined variable:
Originally Posted by Nightwalker83
This is how I connect to a database using php
php Code:
<?php //connect to db $conn = mysql_connect("host", "user", "password"); if (!$conn) { die("Connection failed: " .mysql_error()); } // Database connection variables $dbDatabase = "your database"; ?>
I tried this code and got error message on this line die("Connection failed: " .mysql_error());
-
Sep 13th, 2013, 09:45 AM
#9
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
-
Sep 14th, 2013, 06:56 PM
#10
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
-
Sep 14th, 2013, 07:23 PM
#11
Re: [RESOLVED] php: An defined variable:
I'm hoping by swapping his parameters around...
-tg
-
Sep 15th, 2013, 08:59 AM
#12
Thread Starter
Addicted Member
Re: [RESOLVED] php: An defined variable:
Originally Posted by techgnome
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
-
Sep 15th, 2013, 09:06 AM
#13
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
-
Sep 15th, 2013, 09:11 AM
#14
Thread Starter
Addicted Member
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
-
Sep 15th, 2013, 09:20 AM
#15
Thread Starter
Addicted Member
Re: php: An defined variable:
This is one of your replies.
-
Sep 15th, 2013, 09:21 AM
#16
Thread Starter
Addicted Member
Re: php: An defined variable:
Below is one of your replies.
Originally Posted by techgnome
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|