need help with mysql data base

gamewars

New Member
Messages
16
Reaction score
1
Points
0
my web site is http://gamewars.x10hosting.com/ click xbox button and post wars button and enter in any old info and you will see what is going wrong. Basically im getting an error and im not entering my data base name i guess correctly. plz help
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
You have to post all your PHP code related to the error (omitting the SQL user/pass) for us to help you :)
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Not so much all the related code as a minimal test case, along with other pertinent and relevant information, such as the behavior you expect and the behavior you get, including error messages. Don't make us do extra work.

Remember to search the forums and the web at large before posting. If you're dealing with an error message, it makes searching particularly easy.
 
Last edited:

gamewars

New Member
Messages
16
Reaction score
1
Points
0
i have searched all over the internet but this problem is not going to be solved by searching the internet i dont know how to enter my database name username and password on my php code but heres the code




<?php
$con = mysql_connect("gamewars_xbox360postedwars","gamewars_gamewars","xxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$sql="INSERT INTO Persons (Game, Gamertag, Time and Date, Number of rounds, Number of players)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[quantity]','$_POST[items]','$_POST[players]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)
?>
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Code:
$con = mysql_connect( '[B]localhost[/B]', 'gamewars_gamewars' , 'your[B]secret[/B]passwd');
 
mysql_select_db("my_db");


The first term is the location of the mysql server. In your case, it is probably 'localhost'.
If you posted your real password, please change it.

Finally, look at the mysqli interface
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Whatever ancient tutorials keep screwing up people's code with terrible examples need to be hunted down and shot.

You neglected to post the error. Here it is, for people searching for this error in the future:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'gamewars_xbox360postedwars' (4) in /home/gamewars/public_html/postwarsxbox360.php on line 2
Could not connect: Can't connect to MySQL server on 'gamewars_xbox360postedwars' (4)
You really shouldn't make us wait for the connection to time out to get the error when you could have easily posted it yourself.

The server name (the first argument to mysql_connect) should be "localhost". "gamewars_xbox360postedwars" looks like the DB name. Rather than looking at the documentation for mysql_connect, instead use PDO (PHP Data Objects). The mysql driver was been succeeded by mysqli, which has been succeeded in turn by PDO.

Your script is also vulnerable to SQL injection ([2], [3]). Prepared statements (available with PDO and mysqli) mean you don't need to worry about sanitizing.

If you have spaces in a column name, you need to enclose it in backquotes ("`"), otherwise the query will fail with an SQL syntax error.

Don't use die in production code.

Move the DB connection code to another script to isolate the user credentials and prevent typos from causing problems.

PHP:
# localDB.php defines local_db_connect
include_once('localDB.php')

# the following should be abstracted away into classes
$fields=array_intersect(
  array('firstname' => 'Game', 'lastname' => 'Gamertag', 
        'quantity' => 'Time and Date', 
        'items' => 'Number of rounds', 'players' => 'Number of players'),
  $_POST
);

# define 'validate()' elsewhere and fill out function call here.
$errors = validate($_POST, ...);

if (count($errors)) {
    foreach ($errors as $name => $msg) {
        # reprint the form, with error messages inlined.
    }
} else {
   # this code in particular should be refactored into classes
    try {
        $db = local_db_connect('pdo', array('db' => 'gamewars_xbox360postedwars'));
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        # Note: the following four lines could be done in one, but have been separated 
        # for readability.
        $columnClause = '`' . implode('`, `', $fields) . '`';
        $valueClause = ':' . implode(', :', array_keys($fields)) ;
        $query = "INSERT INTO Players ($columnClause) VALUES ($valueClause)"; 
        $stmt = $db->prepare($query);

        $stmt->execute($_POST);

    } catch (PDOException $pdoe) {
        # error reporting should be abstracted away into a function or class
        $errorMsg = __FILE__ . '@' . __LINE__ . ':' . $pdoe->getMessage();
        error_log($errorMsg);
        # note: $siteAdmin and $from need to be set somewhere, a strong argument for a class.
        mail($siteAdmin, $from, "Script error on $_SERVER[SERVER_NAME]",$errorMsg);
        echo "Internal error. It's been logged, ";
    }
}
 
Top