Jump to content

Receiving Warning but can't locate error.


jcoones
Go to solution Solved by 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

Edited by jcoones
Link to comment
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)

Link to comment
Share on other sites

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

Edited by CroNiX
Link to comment
Share on other sites

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.

Edited by jcoones
Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.