mysql_query() does not execute

Asked

Viewed 78 times

-1

I am trying to test if my database has connected correctly from a query, but apparently mysql_query() does not execute and changes my table. This is my code:

<?php
//conecta a base de dados
$dbc = mysql_connect('localhost', 'dbweb', 'notime');
if(!$dbc)
{
    die("Not connected : " . mysql_error());
}

//seleciona a base de dados
$db_select = mysql_select_db('goaheadmakemyday', $dbc);
if($db_select)
{
    die("Can't connect : " . mysql_error());
}

//teste
$query = "UPDATE Colaborador SET Nome='Ola' WHERE ColNum='0'";
$resultado = mysql_query($query);

//Pequena parte da minha tabela:
//ColNum  Nome
//0       Simeão Lopes
//1       Olavo Bettencourt
//2       Sandro Vidal

1 answer

0


In your code the problem is when the connection is successfully made $db_select receives true, enters the if and fatally falls in the die() and kills your script. The solution to this is to deny the condition or do an inline check.

Mysql functions have already been removed from php7, the most suitable is to use Mysqli or PDO for database queries.

$db_select = mysql_select_db('goaheadmakemyday', $dbc);
if(!$db_select)
{
    die("Can't connect : " . mysql_error());
}

Inline

mysql_select_db('goaheadmakemyday', $dbc) or die(mysql_error());
  • Thank you very much!

Browser other questions tagged

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