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'];
}

?>

Joy
  • 1
  • 1
  • 2
  • 1
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jan 15 '14 at 09: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. – Joy Jan 15 '14 at 09:40
  • 1
    There are several links in the comment that elaborate. – Quentin Jan 15 '14 at 09: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? – Ryan Jan 15 '14 at 09:41
  • @Ryan I have used the same username and password as stated on http://localhost/phpmyadmin/. I have checked and the user has ALL PRIVILEGES to the database too... – Joy Jan 15 '14 at 09:45
  • The fact is you can't of, you're getting `Access denied` which means either the user doesn't exist, the password is wrong, or the user isn't permitted to access the database from the server that the script is running on. – Ryan Jan 15 '14 at 09:47

2 Answers2

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.

Ryan
  • 3,552
  • 1
  • 22
  • 39
  • 1
    But the error message says that there is no password being used, not that the password is wrong. – Quentin Jan 15 '14 at 09:39
  • I have done that in the actual code, but I wouldn't paste them in a public forum ;) – Joy Jan 15 '14 at 09:41
  • Then you need to check your credentials. – Ryan Jan 15 '14 at 09:42
  • 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!) – Joy Jan 15 '14 at 09:46
  • Are you sure you're not getting the error from an alternative script? – Ryan Jan 15 '14 at 09:47
  • ( ! ) Warning: mysql_query() [function.mysql-query]: Access denied for user ''@'localhost' (using password: NO) in C:\wamp\www\blabla\blabla.php on line 34 Call Stack # Time Memory Function Location 1 0.0048 381720 {main}( ) ..\blabla.php:0 2 0.0049 382440 mysql_query ( ) ..\blabla.php:34 – Joy Jan 15 '14 at 09:48
  • Where exactly are you calling `connect_db`? – Ryan Jan 15 '14 at 09:49
  • the function is only where you see it, do I need to call it again? Copy pasting the whole function or just call it? (I know, extremely rookie questions :) ) – Joy Jan 15 '14 at 09:53
  • Any code within a function will only be executed when it is called, for example `connect_db()`. That must be called before executing your query so that the connection is active. Also - you're taking in 3 parameters to your function, so you will either need to remove those if you're hardcoding in the parameters for `mysql_connect()`, or pass over the `$host, $id, $pw` parameters to the function and use those in `mysql_connect()`. – Ryan Jan 15 '14 at 09:56
0

try to given access table to user :

grant all On [tabelName] to [userName];

example

grant all On mysql.* to root;
Asromi rOmi
  • 197
  • 1
  • 7