Error connecting to database
Posted by: phpman (199.33.78.---)
Date: August 13, 2019 09:30AM

Hi,

Thanks again for the help getting WAMP running. I wrote a PHP script for to insert data into into Mariadb, and unfortunately it is not connecting. I have a an if statement in my code using mysqli_connect_errno() to check for errors, and when I run the script, mysqli_connect_errno() triggers the if statement and yields the value 2002. Here is the full script of the file:

<?php

// Extract HTML variable values
$fName = $_POST['fName'];
$lName = $_POST['lName'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$ccnumber = $_POST['ccnumber'];
$cardtype = $_POST['cardtype'];
$expmonth = $_POST['expmonth'];
$expyear = $_POST['expyear'];
$sc = $_POST['sc'];
$email = $_POST['email'];
$phonenumber = $_POST['phonenumber'];
$username= $_POST['username'];
$pwd1 = $_POST['pwd1'];
$pwd2 = $_POST['pwd2'];

// Verify passwords
if ($pwd1 != $pwd2)
{
echo "<h1>The passwords did not match. Please <a href=\"createaccount.htm\">try again.</a></h1>";
exit;
}

// Connect to database
@ $db = new mysqli('localhost', 'user', 'password', 'database');
$error = mysqli_connect_errno();

if ($error)
{
echo 'Error '.$error.'. Could not connect to database. Please try again later.';
exit;
}

// Insert values into database
$query = "INSERT INTO ebazaar VALUES ('".$fName."', '".$lName."', '".$address."', '".$city."', '".$state."', '".$zip."', '".$ccnumber."',
'".$cardtype."', '".$expmonth."', '".$expyear."', '".$sc."', '".$email."', '".$phonenumber."', '".$username."', '".$pwd1."', '".$pwd2."')";
$result= $db->query($query);

// Output results to user
if ($result)
{
echo "Congratulations. Account created!";
}
else
{
echo "An error has occurred. The account was not created.";
}

$db->close();

?>



Any solutions?

Options: ReplyQuote
Re: Error connecting to database
Posted by: Otomatic (Moderator)
Date: August 13, 2019 11:17AM

Hi,

> $error = mysqli_connect_errno();
RTFM. Once again, ALWAYS read the documentation!
Look at how to handle errors and display the error text and not just the error number.
That will be the only answer on this subject.
Although the links on the documentation are in RiggsFolly's and mine signatures and in Wampmanager's Documentation menu items, I'm kind enough to put the right link below, but it's really starting to annoy me.
mysqli_connect

> insert data into into Mariadb
See wamp(64)\mariadb_mysql.txt

---------------------------------------------------------------
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons



Edited 1 time(s). Last edit at 08/13/2019 03:04PM by Otomatic.

Options: ReplyQuote


Sorry, only registered users may post in this forum.