jcoones Posted February 9, 2015 Share Posted February 9, 2015 (edited) 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 February 9, 2015 by jcoones Quote Link to comment Share on other sites More sharing options...
CroNiX Posted February 9, 2015 Share Posted February 9, 2015 You are missing the closing parentheses around your mysql_query() function call Quote Link to comment Share on other sites More sharing options...
CroNiX Posted February 9, 2015 Share Posted February 9, 2015 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) Quote Link to comment Share on other sites More sharing options...
CroNiX Posted February 9, 2015 Share Posted February 9, 2015 (edited) 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 February 9, 2015 by CroNiX Quote Link to comment Share on other sites More sharing options...
jcoones Posted February 10, 2015 Author Share Posted February 10, 2015 (edited) 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 15Warning: 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 February 10, 2015 by jcoones Quote Link to comment Share on other sites More sharing options...
jcoones Posted February 10, 2015 Author Share Posted February 10, 2015 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 15Invalid query: Thanks Quote Link to comment Share on other sites More sharing options...
CroNiX Posted February 10, 2015 Share Posted February 10, 2015 Did you check the docs for mysqli_query() regarding the parameters? You can't just add a "i" to the end of the old mysql functions for them to work in mysqli. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 10, 2015 Share Posted February 10, 2015 Your original code had the right parameters for mysqli but you forgot the "i" when you called the functions, using mysql_query() instead of mysqli_query() etc Quote Link to comment Share on other sites More sharing options...
Solution jcoones Posted February 10, 2015 Author Solution Share Posted February 10, 2015 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.