mysql_query() expects parameter 2 to be resource, string given in /home/u921305435/public_html/phpinfo.php on line 9

I know this has been asked many times already, but me being thick, havent been able to solve it

Ive got this code and its giving me an error

<?
$username="username";
$password="password";
$database="databse";

mysql_connect("host",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$result = mysql_query($con, "SELECT ProductName FROM ProductTrans");

echo "<table border='1'>
<tr>
<th>ProductName</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['ProductName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
?>

What is the problem with it?

Answers


I think it should be like :

$con = mysqli_connect("localhost", "my_user", "my_password", "world");
mysqli_query($con, "SELECT ProductName FROM ProductTrans");

For more info on mysqli_query: http://in2.php.net/manual/en/mysqli.query.php OR if you want to use mysql_query() function which is deprecated you should do like this:

$result = mysql_query("SELECT ProductName FROM ProductTrans");

for mysql_query() :http://in2.php.net/mysql_query


From the documentation on php.net:

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

You have the parameters swapped in your code in the call to mysql_query


It should be query followed by the connection. Interchange the parameters !
$result = mysql_query("SELECT ProductName FROM ProductTrans",$con);

Sidenote : Also, stop using mysql_* functions as it deprecated. Switch to PreparedStatements !


you're using the syntax of mysqli_query with mysql_query. My advice is replace all your mysql with mysqli, as mysql_* is deprecated


It should be like this.

$con = mysql_connect("host",$username,$password); // SEE HERE 
@mysql_select_db($database) or die( "Unable to select database");

$result = mysql_query($con, "SELECT ProductName FROM ProductTrans");

Need Your Help

Return lvalue reference from temporary object

c++ reference lifetime lvalue rvalue

Is, returning an lvalue reference to *this, allowed when *this is an rvalue?

How to read a XML file using getResourceAsStream

java xml parsing jframe executable-jar

I am trying to read an xml configuration in the below class.It is Working when Running It .But When i Export it into a Runnable Jar.The file is not being read ..??