0

Good morning, I am running crazy trying one of the my first php codes with mysql. I have tested this on two servers, one hosted somewhere else and also using a local wamp server, the results are the same so I must have something bad in the code creating the connection or later when I run the query.

For wamp I have already verified the php.ini and also the MySQL privileges, in the code I am using the credentials stated there. I would appreciate some guidance.

 <?php

 // Function: connect to a database. Returns the database connection.




  function connect_db($host, $id, $pwd)
  {

  $connection = @mysql_connect('localhost', 'username', 'password')
        or die('connection problem:' . mysql_error());
    mysql_select_db('database_db');
    return $connection;
    if (!$connection)
  {
    print ("internal error " . mysql_errno() );
  }
  }

 //get names from form

$ID = $_POST['ID'];
$Event= $_POST['Event'];
$Date = $_POST['Date'];
$Time = $_POST['Time'];
$Venue= $_POST['Venue'];
$TypeID= $_POST['TypeID'];
$Score= $_POST['Score'];
$Member = $_POST['Member'];

//insert values
$myquery = "INSERT INTO Results(ID, Event, Date, Time, Venue, TypeID, Score, Member )VALUES('$ID', '$Event', '$Date', '$Time', '$Venue', '$TypeID', '$Score', '$Member')";
$answer = mysql_query($myquery);

if (!$answer) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $myquery;
    die($message);
}

while ($row = mysql_fetch_assoc($answer)) {
    echo "Your results were entered successfully";
    echo "<br>";
    echo $row['Id'];
    echo $row['Event'];
    echo $row['Date'];
    echo $row['Time'];
    echo $row['Venue'];
    echo $row['TypeID'];
    echo $row['Score'];
    echo $row['Member'];
}

?>

6
  • 1
    Danger: You are using an obsolete database API and should use a modern replacement. You are also vulnerable to SQL injection attacks that a modern API would make it easier to defend yourself from. Commented Jan 15, 2014 at 9:38
  • @Quentin, can you elaborate? As said before this is my first experience with programming. At this stage I need the fix the connection to the database. The API thing can be sorted later, I have no plans to publish this work. Commented Jan 15, 2014 at 9:40
  • 1
    There are several links in the comment that elaborate. Commented Jan 15, 2014 at 9:40
  • 1
    @Joy as Quentin picked up in my answer, your error message states the connection hasn't provided a username and password, yet in your code you have a username and password - have you provided the correct code and the correct error message? Commented Jan 15, 2014 at 9:41
  • @Ryan I have used the same username and password as stated on localhost/phpmyadmin. I have checked and the user has ALL PRIVILEGES to the database too... Commented Jan 15, 2014 at 9:45

2 Answers 2

1

You need to change 'username' and 'password' to your actual credentials in your mysql_connect call.

Unless of course, those are your desired credentials - then you need to ensure that the user actually exists and has permission to access the database.

As a side note, use of mysql_* functions is deprecated and it's recommended to use mysqli_* or prepared statements.

Sign up to request clarification or add additional context in comments.

9 Comments

But the error message says that there is no password being used, not that the password is wrong.
I have done that in the actual code, but I wouldn't paste them in a public forum ;)
Then you need to check your credentials.
I find it odd in the message that the username is empty while it is in the code and that I get a similar problem on the public server as on the local server...(of course I am using different credentials for each!)
Are you sure you're not getting the error from an alternative script?
|
0

try to given access table to user :

grant all On [tabelName] to [userName];

example

grant all On mysql.* to root;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.