Jump to content

Recommended Posts

Hello, I'm fairly new to this, having trouble with a basic query. I can run the query in Terminal, but when I write it into PHP I get the following error message:

 

Warning: mysql_query() expects parameter 2 to be resource, null given in (etc.)

 

and here is the problematic line as per the error reporting:

mysql_query ( 'INSERT INTO users (id, username, password, first_name, last_name) VALUES (1, \'john\', \'password\', \'john\', \'thompson\')', NULL )

 

it's not clear to me where that NULL is coming from.

 

 

here's what i'm writing in the page i'm trying to load:

 

$sql = sprintf("INSERT INTO users (id, username, password, first_name, last_name) VALUES (1, 'john', 'password', 'john', 'thompson')");

$result = $db->query($sql);

 

and here's how the method is written on the page that's being included:

 

public function query($sql) {

$result = mysql_query($sql, $this->connection);

$this->confirm_query($result);

return $result;

}

 

Again, I can just enter the row into MySql directly and no trouble, but I can't get it to work through PHP. Any advice? I feel like something simple and glaring is at fault here, but I can't see it.

 

Thanks so much -

 

Elliot

this is above the query.  would this error message override the query method message if the connection was failing?  or would the query message still the one to appear since that's what i called directly?

sorry is these are silly questions -

 

 

public function open_connection() {

$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);

if (!$this->connection) {

die("databse connection failed, bzzzt " . mysql_error());

} else {

$db_select = mysql_select_db(DB_NAME, $this->connection);

if (!db_select) {

die("database selection failed: " . mysql_error());

}

}

}

 

I only added the sprintf while trying to randomly trouble-shoot.  But I got the same NULL error message before that.

 

Earlier today I changed localhost to 127.0.0.1 in one of the mysql library files... I think, or one of the PHP files, I can't remember anymore; which was to address my inability to mysql to work.  it fixed it-- but do i now have to change localhost in a range of other files as well?  and on the page where the DB values are being given in this file structure here?  does that seem like it could be relevant?

 

thanks again

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.