0

I have a Raspberry Pi web server set up at home and I have phpMyAdmin set up to administer databases.

I have created a database to hold bits of information which will be used to place markers on a Google map (multiple users will be adding markers of different types for a role play event).

My problem is that I don't seem to be able to connect to the database. Though I've tried example code from many different sources I get strange results. E.g.

Using this code:

<?php
// Create connection
$con=mysqli_connect("example.com","peter","abc123","my_db");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if($con)
{
echo "Connection OK";
}
if(!$con)
{
echo "No Connection";
}
?>

I get the following print out on the web page:

Connection OK
"; } if(!$con) { echo "
No Connection
"; } ?>

The localhost I've tried "localhost", a domain name which points to the server, a combination of them both, etc. The fact is I feel like I don't understand the printed results on screen and feel like maybe I have been able to connect but I'm being mislead by the echo statements which don't seem to be logical.

I was under the impression that phpMyAdmin would also be able to generate connection code but I don't seem to be able to find anything of the sort.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
user3535074
  • 1,268
  • 8
  • 26
  • 48
  • 3
    Try viewing the source to that page. I wonder if the PHP interpreter is not kicking in, and it is just rendering the script as text? If you can see some PHP it means it is not executing at all. No, phpMyAdmin won't generate PHP code as far as I know. – halfer May 15 '14 at 11:05
  • 2
    The above printout does not corrolate with the provided code. Please edit your question with the COMPLETE database connection code and complete output – Steve May 15 '14 at 11:05
  • when running phpinfo.php, I get a lot of tables with a lot of info returned so I imagine that I do have php instanced and running. – user3535074 May 15 '14 at 11:27

4 Answers4

1

try this, replace this

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

with this

if (mysqli_connect_errno()) {       
    printf("Connect failed: %s\n", mysqli_connect_error());         
    exit();     
}
James Waring
  • 356
  • 2
  • 9
  • Thanks a lot for your help! OK so when I replace: if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } With: if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } I don't get any errors printed onscreen. Does that 100% mean I'm connected? I guess my whole fear here is that I don't actually know if I'm truly connected as if I change the password to something incorrect it also doesn't display an error... – user3535074 May 15 '14 at 11:07
1

This is a strange printout. Try to use

if ($con->connect_error) {
   echo "Failed to connect to MySQL: " . $con->connect_error;
}

or another way

if (!$con) {
    die('Failed to connect to MySQL: ' . mysqli_connect_error());
}
John Priestakos
  • 415
  • 4
  • 19
  • My current code is like this: connect_error) { echo "Failed to connect to MySQL: " . $con->connect_error; } ?> And my readout is like this: connect_error) { echo "Failed to connect to MySQL: " . $con->connect_error; } ?> Any ideas? – user3535074 May 15 '14 at 11:22
  • Im not getting any errors. Only chech if you `//Create connection` comment is not messing with your other code. Leave proper breaks and spaces (reformat codes). – John Priestakos May 15 '14 at 11:26
  • I tried taking the code out of the html and placing it directly in a php file. The php file reads: – user3535074 May 15 '14 at 11:37
  • connect_error) { echo "Failed to connect to MySQL: " . $con->connect_error; } if($con) { echo "Seems ok!"; } ?> Obviously the parameters are incorrect here. I get no error message. However if I add the correct parameters, I do get the message "Seems OK". – user3535074 May 15 '14 at 11:40
  • 1
    Seems OK, then. This code if for `PHP` not `HTML` files – John Priestakos May 15 '14 at 11:41
  • Apologies if this is a really dumb question but does that mean to connect to and read from/modify the database, the pages which have those DB interactions would have to be PHP based? I'm using a framework called go ratchet and I'm not sure if using PHP files would cause problems. – user3535074 May 15 '14 at 11:46
  • I mean, I understood that in an HTML file you could insert PHP code within the brackets and that was ok... Is that really the answer to his post? That this code doesn't belong in an HTML file and should be in a php file? If you confirm that then I'll answer the post. – user3535074 May 15 '14 at 11:50
  • U need to tell Apache to process files with a `.htm` or `.html` file extension as PHP files. To do this you need to create a `.htaccess` file in your root web directory and add this line to it: `AddType application/x-httpd-php .htm .html` – John Priestakos May 15 '14 at 11:56
  • That would have been the greatest answer of all time if only it had worked! I added a .htaccess file in the root web and added the single line of code you posted above (also tried removing the application/ as some blogs said that occasionally caused problems. However I get the following print out when the same code from the php file is copied in the the HTML file: connect_error) { echo "Failed to connect to MySQL: " . $con->connect_error; } if($con) { echo "Seems ok!"; } ?> – user3535074 May 15 '14 at 12:11
  • So Apache is not getting the .htaccess setup. u can see the config and modify it – John Priestakos May 15 '14 at 12:39
1

Please check this is okay

$conn = mysqli_connect("localhost", "root", "password", "dbname");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

instead of, $con->connect_error use mysqli_connect_errno()

fortune
  • 3,361
  • 1
  • 20
  • 30
0

The @mysql_connect code to connect to the database was housed in an html file without Apache being configured to run html files as php files. I am managing to connect to the DB now only only with a .php format file. The final working code was:

$conn = mysqli_connect("localhost", "root", "password", "dbname");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

And it was saved in the web server as databaseConnection.php.

At that point, adding the correct parameters lead to a successful connection and adding incorrect parameters lead to an error message being displayed.

user3535074
  • 1,268
  • 8
  • 26
  • 48