Qbasicnews.com

Full Version: Woohoo! (mySQL)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
So Nexinarus got me started on mySQL...
And I'm wondering why this doesn't add stuff to my database:
Code:
<?php
$username="[censored]";
$password="[censored]";
$database="[censored]";
mysql_connect(localhost,$username,$password);
$query="INSERT INTO news VALUES ('$newstitle','$newsdate','$newsinfo')";
mysql_query ($query);
mysql_close();
?>
Where $newstitle, $newsdate, and $newsinfo are variables passed via the POST method from a regular HTML form. The table is called "news", and there are three fields in the database.
It won't add them to the database, for some reason.
You first have to select a database.

Code:
<?php
$username = "[censored]";
$password = "[censored]";
$database = "[censored]";
mysql_connect('localhost', $username, $password);
mysql_select_db($database);
$query="INSERT INTO news VALUES ('$newstitle','$newsdate','$newsinfo')";
mysql_query ($query);
mysql_close();
?>

I think that this should solve your problem
Yeah, thanks. Big Grin
lol

Zack and I spent a while trying to figure out why it wouldn't work and we both didn't realize the lack of mysql_select_db() lol. Thanks for solving the problem Smile
Yeah....just me...
I worked a lot with MySQL & PHP
Then expect me to be pestering you with questions over the next little while. :wink:
The Geekery is (probably) going to have it's own little file-addition-page (like Q-Tech and RPG-DEV.net) so you don't have to ask me before you add files. Big Grin
Ah, I love mySQL.
Just ask me if you want to
mysql is great, eh?

I use postgres where possible... returning objects can be fun sometimes Smile

Just a quickie: can you select a database by:

mysql_query("SELECT $database")

?

Oh, and zack: you might want to make a class or set of functions to handle your database, it's waaaaay much cleaner that way. Cos then you can add error handling to it etc.

For example a class could be used like:

Code:
// Connect to db and set things up
$dbh = new Database($dbhost, $dbname, $dbuser, $dbpass, true);

// Some random query
$sql = "SELECT foo FROM bar WHERE 1 = 2";

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

// Get data about query

$rows = $dbh->numrows;
$affectedrows = $dbh->affected_rows;
$error = $dbh->error;

// Get data

while ( $row = $dbh->fetchrow )
{
  // blah, blah...
}

// Free result

$dbh->freeresult;

// Close connection

$dbh->close;
I haven't done classes yet in php - just simple loops, ifs, etc. And simple mySQL. Big Grin
Classes are pretty much the same as in any other lingo. You've done C, right? Did ya do classes in that?

Anyway, you could write a function or two that would help you out anyway. The function syntax is:

Code:
function func_name([$arg1[,$arg2[,...]]])
{
  [global $global_var[, $global_var[,...]]];
  // function code
  return $result;
}
Pages: 1 2 3