views:

32

answers:

4

Hell there when i try and connect to pull thing out of the database i get the following error:

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/content/49/5548763/html/matt/download.php on line 17

None of the other answers on this site worked.

here is the script:

<?php

$con = mysql_connect("XXXX", "name", "password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("nameofdb",$con);

$musictable = "";

$sql = "GET * FROM matt";

$result = mysql_query($sql,$con);

while($row = mysql_fetch_object($result)) {

$id = $row->id;

$name = $row->name;

$update = $row->update;

$length = $row->length;

$size = $row->size;

$musictable .= "
  <tr>
    <td width=\"63%\">".$name."</td>
    <td width=\"10%\">".$length." / ".$size."</td>
    <td width=\"10%\"><a href=\"download.php?mp3=".$name."\">DOWLOAD</a></td>
    <td width=\"17%\">|||||</td>
  </tr>
  ";
}

?>
+6  A: 

That's because your query is wrong.

$sql = "GET * FROM matt";

must probably become

$sql = "SELECT * FROM matt";

the basic measure to get warned about this is

if (!$result)
 die("mySQL error: ". mysql_error());  

after issuing a query.

Pekka
+1 for Pekka: this error usually occurs when you have a SQL syntax error.
Andrew Sledge
WOW i feel like the biggest dumb-ass ever. I guess because i was working with $_GET earlier it stuck i cant believe i made that mistake and didn't even catch it. Thanks a lot.
Matthew Carter
A: 

Perhaps try using 'SELECT' instead of 'GET'?

mrjames
A: 
$sql = "GET * FROM matt";

is wrong. Correct SQL syntax is

$sql = "SELECT * FROM matt";

That error is passed into

$result = mysql_query($sql,$con);

which then is invalid and can't be used in while($row = mysql_fetch_object($result)).

You should check for mysql_error() after mysql_query() to catch these.

Fanis
A: 

Want to use SELECT *

You can also use

mysql_query($sql, $con) or die("Error in $sql:" . mysql_error($con));

If you don't want to use die() then you can use echo to see what the error was and help debug the application at least.

tandu