1

I've been scouring this site for 5 hours now trying to get this sorted, I rarely ask for help but this is one of the weirdest and most annoying things I've encountered.

First of all I'd like to say that this DID work fine, I have limited examples of what the cause is but I'll list them anyway.

Here's the full error message:

Fatal error: Call to undefind function mysqli_connect() in C:\wamp\www\game\connect.php on line 3

And here's the code

<?php
// Create connection
$con=mysqli_connect("localhost","root","","game");

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


$select_db = mysqli_select_db($con, 'game');
if (!$select_db){
die("Database Selection Failed" . mysql_error());
}
?>

Weird thing is, it was working completely fine and just suddenly stopped, this has happened more than once.

Here's what I've tried:

Checking the extensions are enabled - Rebooting various times - Setting the correct php path - Using many example codes that "work" -

I also had some code that inputted data straight from phpdesigner into the database, which successfully worked, but that no longer works and I've made literally 0 changes.

The last time it stopped working, I filled out a registration form on my site as a test (that doesn't work either) and it suddenly went off. When filling in the form I click register and nothing happens besides a refresh.

Bit extra: In my httpd file the pfp and pfpinidir are as follows

php5_module"c:/wamp/bin/php/php5.5.12/php5apache2_4.dll"

PHPIniDir "C:\wamp\bin\apache\apache2.4.9\bin\php.ini"

Community
  • 1
  • 1
  • Can you tell us more about your server? Is this something local, like XAMPP or EasyPHP? What version of PHP? – Machavity Sep 16 '14 at 12:07
  • Also, if it is an EasyPHP / XAMPP like, do you have anything in the log files ? – NaeiKinDus Sep 16 '14 at 12:08
  • You will need to check if mysqli extension is enabled – nitigyan Sep 16 '14 at 12:13
  • Server is locally hosted using wamp. MySQL version is 5.6.16. php is 5.5.12 and Apache is 2.4.9. The extensions are enabled, mysql doesn't work as well as mysqli :( – Ryan McKenna Sep 16 '14 at 12:19
  • You could create a dummy php file calling phpinfo(); and ensure that the .ini is correctly loaded (under the "Loaded configuration" part) and that the mysqli extension is really loaded and enabled. – NaeiKinDus Sep 16 '14 at 12:20
  • Makes sense that the mysqli extension wouldn't be loaded or may be having troubles. How do I check if it loaded properly? it's definitely enabled – Ryan McKenna Sep 16 '14 at 12:29
  • Here's a pic of my extensions list, http://imgur.com/FpuS1Ox, look at the top one, when I click that I get this error, http://imgur.com/XLkqm27 – Ryan McKenna Sep 16 '14 at 12:33

2 Answers2

2

Your mysqli extension might not be enabled. so u need to enable that.

You have two PHP binaries on your machine. One is connected to the Apache, and is the one u use when you surf to a web page. The phpinfo() shows you which php.ini file is used by the web php binary. u need to edit this file and in it activate the mysqli extension

Digant Shah
  • 163
  • 1
  • 1
  • 8
0

You're trying to connect to DB twice, plus you're mixing MySQL APIs with mysql_error(), they do not mix together.

The 4th parameter is the DB name which is what you've done in the first example.

Use either:

<?php
// Create connection
$con=mysqli_connect("localhost","root","","game");

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

?>

or omit ,"game" from mysqli_connect() - while mysqli_error() requires DB connection parameter.

<?php
// Create connection
$con=mysqli_connect("localhost","root","");

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


$select_db = mysqli_select_db($con, 'game');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($con));
}
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • I think it is connecting anyway, even though it is telling me that it is not. As when I browse to localhost/game/connect.php I get a "success" message which I've inputted at the end of the code, if it wasn't connecting then I wouldn't get that success message, right? – Ryan McKenna Sep 16 '14 at 13:41
  • @RyanMcKenna I tend to think so. Yet, and as mentioned in my answer, you were mixing MySQL APIs with `mysql_error()` - did you try my code? Not both together at once though. – Funk Forty Niner Sep 16 '14 at 13:43
  • Great, I'll try to continue working on my project, for a hobby it sure is frustrating! EDIT - yes I tried your code, but mine seemed to be working anyway, but I will use yours as it is generally better – Ryan McKenna Sep 16 '14 at 13:43
  • @RyanMcKenna If you were to post your entire INSERT code in your question, I could have a look at it, maybe there's something in there that is causing the problem(s). – Funk Forty Niner Sep 16 '14 at 13:44
  • I'll add it now, also my registration doesn't seem to be working so that's kinda disproving the fact that I can connect, here's my code http://codeviewer.org/view/code:442b – Ryan McKenna Sep 16 '14 at 13:53
  • @RyanMcKenna *Hm...* that should be working. Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Sep 16 '14 at 13:58
  • Parse error: syntax error, unexpected 'error_reporting' (T_STRING) lol :P – Ryan McKenna Sep 16 '14 at 14:04
  • @RyanMcKenna That is very very bizarre. You did copy it correctly right? Try just a quick "hello world" example using `` see what that gives. – Funk Forty Niner Sep 16 '14 at 14:08
  • Works, when I remove the echo I get no errors messages, so I assume I am actually connecting to my database? - registration still doesn't work and you said it looks correct? I've tried other codes and they don't work either. Can't tell if I'm connecting or not! – Ryan McKenna Sep 16 '14 at 14:12
  • @RyanMcKenna You mean it's not echoing "Hello world" on screen? If it isn't, then PHP isn't properly configured, or you may need to do a new install. I've never seen that error message before. Google yielded about 6 results for that error message. – Funk Forty Niner Sep 16 '14 at 14:15
  • It DID echo Hello World, but I removed the echo code – Ryan McKenna Sep 16 '14 at 14:16
  • @RyanMcKenna Then follow the same convention in your DB code `` – Funk Forty Niner Sep 16 '14 at 14:17
  • I already did it with my DB codes lol unless you're talking about something else? put it above the two code samples I gave you and neither give error messages – Ryan McKenna Sep 16 '14 at 14:21
  • @RyanMcKenna See this answer on Stack http://stackoverflow.com/a/5179578/ there might be something in there you may not have done. Plus, MySQL is in fact installed and running, correct? See the other answers in that page also. See http://stackoverflow.com/a/18577094/ also - Enable 2 extensions in your php.ini extension=php_mysql.dll extension=php_mysqli.dll – Funk Forty Niner Sep 16 '14 at 14:23
  • What file should these 3 be directing to?Located in the php.ini file, extension_dir, user_dir and doc_root. The last two are currently empty EDIT MySQL looks to be running happily via the wamp UI and those extensions are enabled – Ryan McKenna Sep 16 '14 at 14:29
  • @RyanMcKenna That I can't be certain, I've never installed PHP/webserver before. I Google'd what I could and that's what I found; thought it would point you in the right direction. – Funk Forty Niner Sep 16 '14 at 14:30
  • Ah okay, well the error message doesn't go away (it's only when I click "Run" from phpdesigner 8 lol) but I think it's connecting – Ryan McKenna Sep 16 '14 at 14:33
  • @RyanMcKenna I'm sure if you upload it on a hosted site, it will work. There's probably some silly little setting somewhere that needs to be changed. – Funk Forty Niner Sep 16 '14 at 14:35
  • 1
    Omg I hate myself. Had an entry to add the "name" but I had the table set to "username", changed it and it's now working! – Ryan McKenna Sep 16 '14 at 14:37