cannot connect to MySQL from webpage

eljay

Registered
Sep 10, 2021
1
0
1
Toronto
cPanel Access Level
Website Owner
I began hosting my website through a service that uses cpanel... I found creating a database, tables etc a headache, and all the worse using phpAdmin.
Coding I used when hosting my site on my own machine (Apache2.4, MySQL5.7, PHP7) that worked fine will not work now and I have spent MANY hours trying to figure this out... 2 scripts:
someform.html:
<html>
<form action="someform.php" method="post">
<p>A: <input type="text" name="A" autofocus /></p>
<p>B: <input type="text" name="B" autofocus /></p>
<p>C: <input type="text" name="C" autofocus /></p>
<p>D: <input type="text" name="D" autofocus /></p>
<p>E: <input type="text" name="E" autofocus /></p>
<p>F: <input type="text" name="F" autofocus /></p>
<p>G: <input type="text" name="G" autofocus /></p>
<p><input type="submit"></p>
</form>
</html>

someform.php:
<html>
<style>
table {
border-collapse: collapse;
width: 13.5%;
color: #588c7e;
font-family: monospace;
font-size: 25px;
text-align: center;
}
th {
background-color: #588c7e;
color: white;
}
tr:nth-child(even) {background-color: #f0f0e0}
</style>
</head>
<body>
<table>
<tr>
<th>A</th>
</tr>
</style></html>

<?php

$mysqli = new mysqli("localhost:3306","usrname","somepword","dbname");

if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
}
else {
echo "<tr align=center><font size=5 color=blue>Here are the results for:<br></tr>";
}

echo $_POST['A'] . ", ", $_POST['B'] . ", ", $_POST['C'] . ", ", $_POST['D'] . ", ", $_POST['E'] . ", ", $_POST['F'] . ", ", $_POST['G'] . ".";


$A = $_POST['A'];
$B = $_POST['B'];
$C = $_POST['C'];
$D = $_POST['D'];
$E = $_POST['E'];
$F = $_POST['F'];
$G = $_POST['G'];



/*This is important because... what if there is no combination of AB pairs that exists,
your loop will execute infinite*/
$limitNumber = 800;
$notFound = true;


for($i = 0; ($i < $limitNumber && $notFound); $i++){

for($j = 0; ($j < $limitNumber && $notFound); $j++){

for($k = 0; ($k < $limitNumber && $notFound); $k++){

$sql = "SELECT * FROM numbers where (A=$A AND B=$B AND C=$C) OR (
A=$A AND B=$B AND D=$D) OR (
A=$A AND B=$B AND E=$E) OR (
A=$A AND B=$B AND F=$F) OR (
A=$A AND B=$B AND G=$G) OR (
A=$A AND C=$C AND D=$D) OR (
A=$A AND C=$C AND E=$E) OR (
A=$A AND C=$C AND F=$F) OR (
A=$A AND C=$C AND G=$G) OR (
A=$A AND D=$D AND E=$E) OR (
A=$A AND D=$D AND F=$F) OR (
A=$A AND D=$D AND G=$G) OR (
A=$A AND E=$E AND F=$F) OR (
A=$A AND E=$E AND G=$G) OR (
A=$A AND F=$F AND G=$G) OR (
B=$B AND C=$C AND D=$D) OR (
B=$B AND C=$C AND E=$E) OR (
B=$B AND C=$C AND F=$F) OR (
B=$B AND C=$C AND G=$G) OR (
B=$B AND D=$D AND E=$E) OR (
B=$B AND D=$D AND F=$F) OR (
B=$B AND D=$D AND G=$G) OR (
B=$B AND E=$E AND F=$F) OR (
B=$B AND E=$E AND G=$G) OR (
B=$B AND F=$F AND G=$G) OR (
C=$C AND D=$D AND E=$E) OR (
C=$C AND D=$D AND F=$F) OR (
C=$C AND D=$D AND G=$G) OR (
C=$C AND E=$E AND F=$F) OR (
C=$C AND E=$E AND G=$G) OR (
C=$C AND F=$F AND G=$G) OR (
D=$D AND E=$E AND F=$F) OR (
D=$D AND E=$E AND G=$G) OR (
D=$D AND F=$F AND G=$G) OR (
E=$E AND F=$F AND G=$G)";

if ($result = $mysqli -> query($sql)) {
while ($row = $result -> fetch_row()) {
echo "<td align=centre><b>($row[0])</td>";
$notFound = false;
break;
}

}
$C++;
}
$B++;
}
$A++;
}

$mysqli -> close();
?>

</body>
</html>

Would appreciate some help while some hair remains...
 
Last edited by a moderator:

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
7,496
1,009
313
cPanel Access Level
Root Administrator
Hey there! While our support for the actual code is going to be very limited, nothing looks wrong in your mysqli connection string. Have you tried removing the :3306 from the string? I would try that, and also try using 127.0.0.1 instead of localhost, and see if that gets things working.

If not, it would be best to reach out to the hosting provider or datacenter to see if they have any special requirements for the connection string.