-4

I am getting the following error when trying to pull data from a MySQL database:

Warning: mysql_query() expects parameter 1 to be string, resource given

I have tried many changes to remedy the problem but I am really just guessing at what the problem is.

Here is my code:

// Create connection
$conn = mysql_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysql_connect_error());
}

$sql = "SELECT * FROM doginfo WHERE breed='$dog'";
$result = mysql_query($conn, $sql);

if (mysql_num_rows($result) > 0) {
// output data of each row
    while($row = mysql_fetch_assoc($result)) {
        echo nl2br("Breed: " . $row["breed"] . "\n" . "Size: " .     $row["size"] . "\n" . "Height: " . $row["height"] . "\n" "Weight: " . "\n" . $row["weight"] . "\n" . "Life Expectancy: " . $row["life"] . "\n");
} 
} else {
     echo "No results found!";
}
mysql_close($conn);
halfer
  • 19,824
  • 17
  • 99
  • 186
J. Coones
  • 1
  • 1
  • This question is one of the most-asked on Stack Overflow. If you can get into the habit of searching both here and the web generally before asking a question, it is much appreciated. – halfer Feb 09 '15 at 18:29

1 Answers1

-1

For mysql_query() the first parameter is a string that contains the query to execute, not the connection. The connection is used as the first parameter in the newer and highly recommended mysqli_query().

For mysql_query():

mixed mysql_query ( string $query [, resource $link_identifier = NULL ] )

For mysqli_query():

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

Deprecation Notice: It is strongly advised that you discontinue use of mysql_* functions in favor of mysqli_* or PDO functions. mysql_* functions are deprecated due in part to their vulnerability of SQL Injection. It is highly recommended that you upgrade to one of the newer extensions and make use of prepared statements to better protect your web application.

War10ck
  • 12,387
  • 7
  • 41
  • 54