For information_schema
mysql is possible to access all tables in a database:
SELECT table_name FROM information_schema.tables WHERE table_schema = 'data_base'
AND table_name = 'nome_da_tabela'
After query, use the bank driver function/method that resumes the amount of bank records:
Count() (generically)
rowCount()(PDO)
mysqli_num_rows()(mysqli)
$db = new PDO('mysql:host=localhost dbname=test','usuario', 'senha');
$sql = 'SELECT table_name FROM information_schema.tables WHERE table_schema = :database
AND table_name = :tabela';
$stmt = $db->prepare($sql);
$stmt->bindValue(':database', 'test';
$stmt->bindValue(':tabele', 'bananas';
$stmt->execute();
$tabelas = $stmt->rowCount();
if($tabelas >= 1 ){
echo 'tabela valida';
//efetuar select
}else{
echo 'tabela invalida';
//efetuar um create table?
}
about the information_schema
The person who gave the downvote could explain why? Do you have any performance problems or anything like?
– utluiz
I did not downvote the 'problem' of the code is that the mysql_* functions have already been deprecated. http://br1.php.net/manual/en/function.mysql-connect.php and has a question here: http://answall.com/questions/579/porque-nao-devemos-usar-funcoes-do-tipo-mysql
– rray
Really, @lost is right...today the best is to use PDO...
– Kenny Rafael
@utluiz Não dei downvote, but in the link you provided, in the comments has the version PDO, update your example and if it was from downvote, you’re already safe :)
– Zuul
Thanks for the personal comment, it is better to leave the example more suitable even. I was more concerned with the content of query than with how to use in PHP and let it pass.
– utluiz
@utluiz you could update your answer using
mysqli
? Thank you.– Raizant