jackcoder Posted August 3, 2014 Share Posted August 3, 2014 hello I keep getting these errors: Warning: mysql_query() expects parameter 2 to be resource, string given in C:\xampp\htdocs\PHP & JQUERY\WEBSITE\index.php on line 17Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in C:\xampp\htdocs\PHP & JQUERY\WEBSITE\index.php on line 19 This is the code I am using: $conn = mysql_connect("localhost", "root"); $q = "SELECT * FROM users"; $r = mysql_query($conn, $q); $retrieve = mysql_fetch_assoc($r); print $retrieve['user_name']; Any help would be appreciated. Quote Link to comment Share on other sites More sharing options...
jackcoder Posted August 3, 2014 Author Share Posted August 3, 2014 changed code to: $conn = mysql_connect("localhost", "root", "", "users"); $data = mysql_query("SELECT * FROM users") or die(mysql_error()); $info = mysql_fetch_assoc($data); echo $info['user_name']; Quote Link to comment Share on other sites More sharing options...
CroNiX Posted August 3, 2014 Share Posted August 3, 2014 (edited) You really shouldn't use the mysql driver. It's been deprecated and your code won't work on newer php versions so it's kind of obsolete from the get-go. You won't do yourself any favors learning something that is obsolete. http://php.net/manual/en/migration55.deprecated.php You really should learn to use PDO, and if not that, use the mysqli (with an i) driver. Edited August 3, 2014 by CroNiX Quote Link to comment Share on other sites More sharing options...
jackcoder Posted August 3, 2014 Author Share Posted August 3, 2014 thank you! Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted August 4, 2014 Share Posted August 4, 2014 Out of curiosity, were you able to get the query working? It looks like you corrected the argument order for mysql_query()...so it's probably fixed. Quote Link to comment Share on other sites More sharing options...
mogosselin Posted August 4, 2014 Share Posted August 4, 2014 This is an example with mysqli_ + OOP style: <?php define('DB_SERVER', "localhost"); define('DB_USER', "user"); define('DB_PASSWORD', "password"); define('DB_TABLE', "db_name"); $conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_TABLE); if ($conn->connect_error) { trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR); } $query = "SELECT * FROM users"; $result = $conn->query($query); if($result === false) { trigger_error('Wrong SQL: ' . $query . ' Error: ' . $conn->error, E_USER_ERROR); } else { $result->data_seek(0); while($row = $result->fetch_assoc()){ echo $row['username'] . '<br>'; } } $result->free(); $conn->close(); ?> Here's an example with mysqli_, procedural style: <?php define('DB_SERVER', "localhost"); define('DB_USER', "user"); define('DB_PASSWORD', "password"); define('DB_TABLE', "db_name"); // The procedural way $mysqli = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_TABLE); if (mysqli_connect_errno($mysqli)) { trigger_error('Database connection failed: ' . mysqli_connect_error(), E_USER_ERROR); } $query = "SELECT text AS txt FROM `table_test`"; $result = mysqli_query($mysqli, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($mysqli), E_USER_ERROR); if($result) { while($row = mysqli_fetch_assoc($result)) { echo $row['txt'] . '<br>'; } } mysqli_close($mysqli); ?> This is an example with PDO: $db = new PDO("server", "username", "password"); $statement = $db->prepare("select * from users"); $statement->execute(); $row = $statement->fetch(); Please consider specifying the names of the fields instead of SELECT *. This is a, generally, a bad practice to use * (security & performance wise). As it's already been said, don't use mysql_ but mysqli_ or PDO. 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.