Jump to content

Recommended Posts

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 17

Warning: 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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/290254-php-error-help-needed/
Share on other sites

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 by CroNiX

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