Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I use this php code to connect to my databse and retrieve the table food. I am able to connect to the database but no retrieve the table. I get this as a result: Host information: site.ipagemysql.com via TCP/IP Default database is database. Is there something wrong with my code?

<?php

    $mysqli = new mysqli("site.ipagemysql.com", "user", "pass", "database");

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

    printf("Host information: %s\n", $mysqli->host_info);



$result = mysqli_query($mysqli, "SELECT DATABASE()");
    $row = mysqli_fetch_row($result);
    echo "<br />";
    printf("Default database is %s.\n", $row[0]);


$query = "SELECT * FROM example"; 

$result2 = mysqli_query($mysqli,$query) or die(mysql_error());


while($row2 = mysqli_fetch_array($result2, MYSQLI_BOTH)){
    echo $row2['food']. " - ". $row2['calories'];
    echo "<br />";
}


?>
share|improve this question
add comment

closed as off topic by Jeff Vanzella, Flambino, Corbin, Glenn Rogers, Brian Reichle Nov 23 '12 at 22:29

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 1 down vote accepted

This is one of the most common ways to connect & return data from a mysqli database

// Database parameters
    $db_host = 'database_host';localhost??
    $db_user = 'database_user';
    $db_pass = 'database_password';
    $db_name = 'database_name';
// Connect to the database  
    $mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
    if (mysqli_connect_errno()) {
        printf("connect failed: %s\n", mysqli_connect_error());
        exit();
    }
// Query database
    $query = "SELECT * FROM example";
    $result = $mysqli->query($query) or die($mysqli->error.__line__);
// Loop through results
    if($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo $row['food']. " - ".$row['calories'];
            echo "<br">;    
        }
    }
    else {
        echo 'No results';  
    }
// Close connection
    mysqli_close($mysqli);
share|improve this answer
    
Thank you, All online tutorials use mysql_connect(), How where I supposed to know that mysqli_connect() is better? –  Conrad C Nov 24 '12 at 19:50
    
Glad to help.But in future you should post questions to Stack Overflow. –  david strachan Nov 24 '12 at 19:57
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.