I need assistance with php Homework - php

I tried using this code to input data from a form into a database, but the code will not work and it keeps giving me access denied and error messages.I will submit my php code and a screen shot of the error messages in the hopes that you can find out why this dose not work.
Code & Screen Shot Below:
<?php
// Create connection
$con=mysqli_connect("<host>","<username>","<password>","<database>");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO usertb (username, password)
VALUES
('$_POST[username]','$_POST[password]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>

Access denied would point to a DB connection error. Without seeing the error message I would say its one of two possibilities, db user being denied a connection to the DB or the user not having enough rights on the DB to be able to make changes.
Confirm that you can log into the DB with the user information you have put in you code.
mysql -D 'db name' -u 'user' -p
If you can get in OK, see if your INSERT query works at the mysql command line. If it does then you know your user can enter data into that DB and you know your query is properly formed.
If the above works all OK, then you know there is something wrong with your PHP code.
Lastly I would put a positive response on your connect and query code.
$con=mysqli_connect("<host>","<username>","<password>","<database>");
// Check connection
if (!$con)
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
echo "Connected to DB successfully!<br>!";
}
Hopefully that gets you a little closer.
S

Related

frequent lost of connection to mySQL server

I instal wamp server locally on my PC.I create a user registration form using php and mySQL database for pratice.I was able to connect to my mySQl data server and if i input data on the form and submit is work successfully.I normal experience at time when i access the user registration form it lost connection to my SQLdata server even though my wamp server is on.I will troubleshoot by changing some of the parameter by using mysqli_connect() to check if i will be prompted unsuccessful connection still nothing will show.When i check some other time it will work successfully.Please what may be causing this issue?
Run wampserver as administrator by right clicking wampserver.exe file. It may help.
Edit:
With the type of english you are using to produce the problem and giving no code is making it difficult to predict what the problem actually is. But I think there is something wrong with your mysqli connection file.
Add this code bit to the top of your registration.php file like this:
<?php
$connection = mysqli_connect('localhost','root','');
if(!$connection) {
die("Failed to connect" . mysqli_error($connection));
}
else {
echo "Connection Established";
}
$select_db = mysqli_select_db($connection, 'db2');
if(!$select_db) {
die("Database selection failed" . mysqli_error($connection));
}
else {
echo "Database selected";
}
?>
<?php
////rest of ur registration code goes here

php require_once ("database connection file") is not working when running a query from an other include_once file

on my main page I have my
//connection to database
require_once('inc/conn_db.in');
I also have a file that
// Device Search::
require_once('inc/ProductSearch.php');
queries made from the main page work fine.
The problem lies when I run queries from the second require_once file ('inc/ProductSearch.php')
I could be completely wrong but I would think that the database connection is open, and that I should be able to query while the main page is open.
the only way I am able to run the query is when I include the require_once('inc/conn_db.in');
in the second file.
Is this a normal behavior or am I doing something wrong
content of the conn_db.in::::
############################################################
// Create Script to Connection to the Database
$conn = mysql_connect("localhost", "$$$$$", "$$$$$$");
// Check if the connection to the database Server can be created
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
// Check if it can find the Database on the Database Server
if (!mysql_select_db("$$$$$$")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
#
any help or directions are appreciated

Connection to database seems successful but INSERT INTO sends no data

I'm trying to insert some data into a DB on my local machine.
I've opened up the connection, and performed the INSERT INTO for the fields I require. Nothing is showing up in the database but I'm really lost as I'm not getting any errors either, so no guidance as to where it is failing. I have tried scaling it back and adding in echo variables to check how far the script is running but it runs all the way to the end.
To start I set the password and username which is just my local stuff:
// configuration
$dbuser = "root";
$dbpass = "";
Then I create the connection
// Create connection
$con=mysqli_connect("localhost",$dbuser,$dbpass,"survey");
Because I was struggling to find out where it was falling over, I have included an error statement. At present it outputs 'successfully connected' so it's not failing to connect...
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo 'successfully connected';
}
And then I'm preparing the SQL INSERT INTO
// the SQL insert
mysqli_query($con,"INSERT INTO mytablename (MostImportantFeatures,ImportanceOfFreeDelivery)
VALUES ($features,$importance_free_delivery)");
mysqli_close($con);
And I am getting nothing back. No errors, but no submission to the database. I cannot work out where I've gone wrong or how I can debug this as the script runs through. I can log into the database (using Sequel Pro) with the details above just fine.
How would you start to debug this?
To start debugging you probably want to check if you get TRUE or FALSE back from mysqli_query, and check the last error on false.
if(!mysqli_query($con,"INSERT INTO mytablename (MostImportantFeatures,ImportanceOfFreeDelivery) VALUES ($features,$importance_free_delivery)")) {
echo mysqli_error($con);
}
mysqli_query docs - php.net
mysqli_query docs - php.net

Can't connect to database "Fatal error: Call to undefind function mysqli_connect()"

I've been scouring this site for 5 hours now trying to get this sorted, I rarely ask for help but this is one of the weirdest and most annoying things I've encountered.
First of all I'd like to say that this DID work fine, I have limited examples of what the cause is but I'll list them anyway.
Here's the full error message:
Fatal error: Call to undefind function mysqli_connect() in C:\wamp\www\game\connect.php on line 3
And here's the code
<?php
// Create connection
$con=mysqli_connect("localhost","root","","game");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$select_db = mysqli_select_db($con, 'game');
if (!$select_db){
die("Database Selection Failed" . mysql_error());
}
?>
Weird thing is, it was working completely fine and just suddenly stopped, this has happened more than once.
Here's what I've tried:
Checking the extensions are enabled -
Rebooting various times -
Setting the correct php path -
Using many example codes that "work" -
I also had some code that inputted data straight from phpdesigner into the database, which successfully worked, but that no longer works and I've made literally 0 changes.
The last time it stopped working, I filled out a registration form on my site as a test (that doesn't work either) and it suddenly went off. When filling in the form I click register and nothing happens besides a refresh.
Bit extra: In my httpd file the pfp and pfpinidir are as follows
php5_module"c:/wamp/bin/php/php5.5.12/php5apache2_4.dll"
PHPIniDir "C:\wamp\bin\apache\apache2.4.9\bin\php.ini"
Your mysqli extension might not be enabled. so u need to enable that.
You have two PHP binaries on your machine. One is connected to the Apache, and is the one u use when you surf to a web page.
The phpinfo() shows you which php.ini file is used by the web php binary. u need to edit this file and in it activate the mysqli extension
You're trying to connect to DB twice, plus you're mixing MySQL APIs with mysql_error(), they do not mix together.
The 4th parameter is the DB name which is what you've done in the first example.
Use either:
<?php
// Create connection
$con=mysqli_connect("localhost","root","","game");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
or omit ,"game" from mysqli_connect() - while mysqli_error() requires DB connection parameter.
<?php
// Create connection
$con=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$select_db = mysqli_select_db($con, 'game');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($con));
}
?>

php mysqli_connect weird response

I'm trying to create a very simple create account page. This is the part of the MySQL Database connection:
<?php
function dbconnect()
{
$connection = mysqli_connect("localhost","maistral_quizonline","maistral_kalkas","abcdefghi");
if (mysqli_connect_errno($connection))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return 0;
}
return $connection;
}?>
When I try to run dbconnect this is what I get as output:
"Failed to connect to MySQL: Access denied for user 'maistral_quizonl'#'localhost' (using password: YES)Access denied for user 'maistral'#'localhost' (using password: NO)"
Now, I'm new to php and maybe i do something really silly, can anyone explain to me this output? Why it seems to try for connection twice and why the connection information is corrupted? What is this: maistral_quizonl? maistral? Why is it cutting the words? And why the first time says that I'm using password and the second time says no? I hope my mistake is not so stupid.
And one other thing. I saw in another post someone suggesting to check if the mysqli_connect function exists so I change this:
echo "Failed to connect to MySQL: " . mysqli_connect_error();
to this:
echo "Failed to connect to MySQL: " . mysqli_connect_error() . var_dump(function_exists('mysqli_connect'));
and the output was this:
"bool(true) Failed to connect to MySQL: Access denied for user 'maistral_quizonl'#'localhost' (using password: YES)Access denied for user 'maistral'#'localhost' (using password: NO)"
lol?
I know that it's pointless to check if mysqli_connect exists after you use it but am I mistaken or isn't the "bool(true)" suppose to print at the and of the line and not the beggining?
What is going on?
Thank you
PS: I have double check that the database exists as well as the user and the priviliges and the password
mysqli_connect accepts parameters in this order 'host', 'username', 'password', and 'dbname', is this the order in which you have provided your details? It does not seem so.
http://us2.php.net/manual/en/mysqli.construct.php

Categories