Friday, 31 August 2018

The PHP code does not work, does not connect to the mysql database

I'm trying to get simple form data added to mysql database via php.

This is my php code..
$dbc = mysqli_connect('localhost','root','sahunihal123321','aliendatabase')
or die('Error connecting to MySQL server.');

$query="INSERT INTO aliens_abduction (first_name,last_name,when_it_happened,how_long, " .
"how_many,alien_description,what_they_did,fang_spotted,other,email)" .
"VALUES ('$first_name','$last_name','$when_it_happened','$how_long','$how_many', " .
"'$alien_description','$what_they_did','$fang_spotted','$other','$email')";

$result=mysqli_query($dbc,$query)
or die('Error querying database.');
mysqli_close($dbc);

  echo 'Thanks for submitting the form.<br />';
  echo 'You were abducted ' . $when_it_happened;
  echo ' and were gone for ' . $how_long . '<br />';
  echo 'Number of aliens: ' . $how_many . '<br />';
  echo 'Describe them: ' . $alien_description . '<br />';
  echo 'The aliens did this: ' . $what_they_did . '<br />';
  echo 'Was Fang there? ' . $fang_spotted . '<br />';
  echo 'Other comments: ' . $other . '<br />';
  echo 'Your email address is ' . $email;

?>

</body>
</html>

Nothing is being displayed.
So ,Something going wrong in the connect stage, Im working on a local system not a server , and no problem with the form
I go to access my mysql database ,this is what i see
nsnihalsahu@nsnihalsahu-home:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 5.5.34-0ubuntu0.13.10.1 (Ubuntu)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW DATABASES
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aliendatabase      |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> USE aliendatabase
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> SHOW TABLES;
+-------------------------+
| Tables_in_aliendatabase |
+-------------------------+
| aliens_abduction        |
+-------------------------+
1 row in set (0.00 sec)

mysql> SELECT * FROM aliens_abduction;
Empty set (0.00 sec)

How do I resolve this?

I copied this from php.net, try to use this instead to connect to your db:
For remote:
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";

For localhost:
$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

echo $mysqli->host_info . "\n";

0 comments:

Post a Comment