0

I'm trying to connect to a database that I created using this code:

<?php
function Conectarse()
{
    $host="localhost";
    $user="root";
    $password="root";
    $bda="toobebe_catalogo";

    if (!($link=mysql_connect($host,$user,$password)))
    {
        echo "Error conectando a la base de datos.<br>";
        exit();
    }
    else
    {
        echo "Éxito conectando con la base de datos.<br>";
    }

    if (!mysql_select_db($bda,$link))
    {
        echo "Error seleccionando la base de datos.<br>";
        exit();
    }
    else
    {
        echo "Éxito al encontrar la base de datos.<br>";
    }
    return $link;
}

$conn=Conectarse();

$sql="SELECT * FROM toobebe-octubre";
$db_fila = mysql_query($sql,$conn);
$ok=1;

while (($row = mysql_fetch_array($db_fila)) && $ok) 
{
    $valor=mysql_query($sql,$conn);

if(!$valor)
{
    $ok=0;
}   
}

?>

But it fires this mistake when I execute it:

 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in ...

I've been searching, and to know: - Database exists

  • Permissions are correct

  • Table exists

  • Table is not null

Any idea on why this mistake is happening?

EDIT: Added image with the mistake: Image error

3
  • The full error line would be helpful. Did you ever try to insert the value of $sql directly into your database via PHPMyAdmin or something similiar? From the Docs: mysql_query() returns a resource on success, or FALSE on error. FALSE = boolean Commented Oct 25, 2012 at 10:57
  • use PDO please instead of this Commented Oct 25, 2012 at 11:00
  • mysql_connect will be depricated soon so better use PDO or mysqli Commented Oct 25, 2012 at 11:02

1 Answer 1

2

The error isn't with connecting to the Database, but rather with your query. You have a hyphen in your table name, so you should try and enclose it as follows:

$sql = "SELECT * FROM `toobebe-octubre`";
$db_fila = mysql_query($sql, $conn);
$ok = 1;

while(($row = mysql_fetch_array($db_fila)) && $ok) 
{
    $valor=mysql_query($sql, $conn);
    if(!$valor)
    {
       $ok=0;
    }   
}

Just a couple of tips, using mysql_* is severely deprecated now. You should really be using mysqli_* at a very minimum, or PDO.

Also, SELECT * is generally considered a bad practice, because I really doubt you do need everything from the table.

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

3 Comments

I do really need to select all from my database, as I created it with articles and I need to select all of them to insert hem into a new and more complicated table. Do you mean with 'toobebe-octubre' with simple quotes? Because it doesn't look like that and it doesn't work at all...
No, they're not single quotes, but rather backticks.
Sorry, couln't at first 10 minutes and I forgot until now... You were so fast ;)

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.