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


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:


// 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>";

// 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.';

// 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!";
echo "An error has occurred. The account was not created.";



Any solutions?

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


> $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.

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

Wampserver 3.2.2 32 bit - Apache 2.4.43 - PHP 7.4.6/7.3.18/..../5.6.40 - MySQL 5.7.30 - MariaDB 10.4.12
Wampserver 3.2.2 64 bit - Apache 2.4.43 - PHP 7.4.6/7.3.18/..../5.6.40 - MySQL 5.7.30/8.0.20 - MariaDB 10.4.12
PhPMyadmin 5.0.2 - MysqlDumper 1.24.5
on W10 and W7 Pro 64 bit
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons
« Ce n'est pas parce qu'ils sont nombreux à avoir tort, qu'ils ont forcément raison. Coluche »
« It's not because they are many to be wrong, they are necessarily right. Coluche »

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.