4

As a complete php/sql noob, I've been trying to set up a very basic thingy on a server. I have a db up with a table, and I'm trying to read it on a php page. Here is the relevant part of my code:

<?php
    $con = mysql_connect("localhost", "----", "----") or die(mysql_error());

    mysql_select_db("my_db") or die ("Select db fail!");

    $ings = mysql_query($con, "SELECT * FROM Table") or die("Query fail: " . mysql_error());

    mysql_close($con);
?>

The output is:

Query fail: 

I can't wrap my head around it. mysql_error does not return anything. I've tried mysql_errno and it returns 0, I've tried mysql_num_rows($ings) and it returns nothing.

Any insight?

Jim
  • 1,056
  • 1
  • 13
  • 19
user1846231
  • 315
  • 3
  • 12
  • 1
    [mySQL is depricated use mySQLi](http://stackoverflow.com/questions/548986/mysql-vs-mysqli-in-php) also "Table" is a reserved word if you really have a "table" named "TABLE" you have to escape it using back tick ` – xQbert May 22 '13 at 19:19
  • yes, it does. On phpmyadmin I run the exact same query (copy paste) and it succeeds and returns the correct rows. – user1846231 May 22 '13 at 19:20

1 Answers1

5

The signature for mysql_query only takes the query string... you don't need to pass the connection because that is set in the background.

The die is being called because the function call is not valid, which is why the error is empty because the query is never ran.

And yes, mysql_query is deprecated in favor of mysqli. You might as well learn it from the beginning so you don't have to learn it twice.

ajon
  • 7,868
  • 11
  • 48
  • 86