0

My php crashes without an error because of the following code:

//print some html

$server = "localhost"; $user = "iremovedthis"; $pass = "iremovedthis";
$connection = new mysqli($server, $user, $pass);
if ($connection->connect_errno) {
    printf("Connect failed: %s\n", $connection->connect_error);
    exit(); }

$game_query = "SELECT * FROM games LIMIT 9;";
$game_query_result = $connection->query($game_query);
$row = $game_query_result->fetch_array(MYSQLI_ASSOC);

//print more html

I'm trying to put the SQL result into an array.

If i comment the last line (starting with $row), my PHP keeps nicely printing HTML, but if i include the last line, it only outputs the HTML before my code and it doesn't give me a print error.

I seem have followed the PHP manual to a tee, anyone know what could be causing this?

6
  • What do you mean by crashes? Have you checked your error logs? Is error reporting on? What is the result of $game_query_result? Commented Jun 8, 2016 at 11:46
  • You could try telling the connection what database you would like it to connect to! Commented Jun 8, 2016 at 11:49
  • did you check what $connection->connect_error says after query call? Commented Jun 8, 2016 at 11:50
  • I mean it stops outputting html, without giving me any error. I'm pretty sure error reporting was already on. Adding "error_reporting(E_ALL);" to the top of the code doesn't change anything. Commented Jun 8, 2016 at 11:50
  • @RiggsFolly isn't giving a query pointing to database "games" enough? Commented Jun 8, 2016 at 11:52

1 Answer 1

1

The mysqli_connect() needs to be told what database you want to connect to. Remember MYSQL can run 100's of databases simaltaneously.

$server = "localhost"; 
$user = "iremovedthis"; 
$pass = "iremovedthis";
$TheDatabase = 'mydb'; // of whatever you called your database

$connection = new mysqli($server, $user, $pass, $TheDatabase);

The PHP manual for mysqli_connect

RE: Your comment

isn't giving a query pointing to database "games" enough?

games is a table name, and not a database. Tables exist in databases, databases exist within a MySQL instance, A single MYSQL instance manages multiple databases. So in short, NO!

Sign up to request clarification or add additional context in comments.

3 Comments

The old mysqli method didnt use to require a database, stupid mistake on my part. It's fixed now, thanks again.
RE: your RE: ah, im a bit rusty on php so hence the confusion.
I had a feeling you were probably coming from a mysql_ background. Unfortunately the conversion to mysqli_ is not just a case of adding the i to the mysql_ api

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.