PHP and Mysqli. Table link problem

Asked

Viewed 327 times

3

I’m following a toturial, it’s all the same. My problem is I can’t connect to the table.

connect.php:

<?php

    $db = new mysqli('127.0.0.1', 'user', 'pass', 'app');

    if ($db -> connect_errno) {
        die ('Sorry, we are having some problems.');
    }

?>

This works correctly, the connection to DB is made without probs.

The problem is here:

index php.

<?php
    require_once('db/connect.php');

    $result = $db->query("SELECT * FROM people") or die ($db->error);

    if ($result->num_rows) {
        echo 'yay';
    }
?>

The query gives this error message being this:

Table 'app.people' doesn’t exist

Being that, only people is the table name

inserir a descrição da imagem aqui

  • In your php instead of select ... people change to show tables and see if the table will appear people. When in doubt try to recreate her.

  • "Yay, "I think that’s a good sign.... Although I don’t understand what’s going on in this case...

  • you need a forech on $result or a print_r() to see the result of the consultation.

  • Miguel, but it comes to "Yay" or not?

  • Yes, with 'show Tables' arrived.

  • 'print_r($result)' = "mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 1 [type] => 0 ) ".... is wrong

  • Could be permission issue, tried to access with the root user of your mysql?

  • I’ve also thought about it, but I don’t think so... Permission is on the www-data side (linux)

Show 3 more comments

2 answers

1

That way it worked for me:

connect.php

<?php
// Mantenha o @, ele esconde o echo automatico do erro
$mysqli = @new mysqli("127.0.0.1", "user", "pass", "app");

/* checar conexão */
if (mysqli_connect_errno()) {
    printf("Erro ao contectar:: %s\n", mysqli_connect_error());
    exit();
}


index php.

<?php
require_once('db/connect.php');

/* Executar query */
$result = $db->query("SELECT * FROM people");

/* Checar se a query teve sucesso */
if (!$result) {
    printf("Erro ao executar query: %s\n", $db->error);
}

/* Pegar o numero de linhas */
$rows=mysqli_num_rows($result);

/* Checar se o numero de linhas selecionadas é maior que 0 */
if ($rows>0) {
    echo 'Yay';
}

If it still doesn’t work check the db permissions or try an online server...

1

The connection syntax is:

$mysqli = new mysqli("meu_host", "meu_usuário", "minha_senha", "meu_banco");

So in your case, the error is stating that there is no table people in the database app, make sure it exists in your bank app this table. See more...

  • Thank you very much, but the table does exist

Browser other questions tagged

You are not signed in. Login or sign up in order to post.