Jump to content

Receiving Warning but can't locate error.


jcoones

Recommended Posts

Hi,  I'm fairly new to php and MySQL and 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 a few things in an attempt to correct the issue but I am really only guessing.

 

Here is my code:

// Create connection
$conn = mysql_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysql_connect_error());
}
$query = "SELECT * FROM doginfo WHERE breed='$dog'";
$result = mysql_query($conn, $query);

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);
echo "</font>";

Any help appreciated.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/294487-receiving-warning-but-cant-locate-error/
Share on other sites

Are you still having the problem? I see you edited and added the ) to mysql_query() in your original post. It's also never wise to just assume your queries run and continuing on, without first checking for an error. See example 1 in the docs for mysql_query(). Most likely that will tell you where you are going wrong.

 

Also pay attention to the deprecation warning at the top of the page for mysql_query(). If you want your code to run on future versions of PHP you should be using the PDO or MySQLi extension, not MySQL (without the i at the end)

Ah, yes, you have the parameters in the wrong order

 

Hi,  I'm fairly new to php and MySQL and 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 a few things in an attempt to correct the issue but I am really only guessing.

 

Here is my code:

$result = mysql_query($conn, $query);

Any help appreciated.

 

Thanks

Thanks for the response.  I changed all references of mysql to mysqli and changed the parameters from $result = mysqli_query($conn, $query); to $result = mysqli_query($query, $conn);  and now I am getting two errors.

 

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in /home/…. : eval()'d code on line 15

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/…. : eval()'d code on line 16

 

There must be something else amiss in my code.

I added the error checking from the example 1 that you suggested and I am getting just one error that says:

 

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in /home/…. : eval()'d code on line 15
Invalid query:

 

Thanks

Thanks for all the help.  Very much appreciated.  I finally got it working as it should.

 

Here's the code I changed to:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM doginfo WHERE breed='$dog'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        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 "0 results";
}
$conn->close();

Thanks again for the help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.