-1

I am a beginner . I copied a simple php page on the server web http://www.barman-team.ir/tt.php and i import sql data base and make user & and pass on data base (in cpanel)

this page work on local host( xamp) but dont run on server

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body dir="rtl">

<?php
        $link=mysql_connect("localhost","uname","pass");
        mysql_select_db("barmante_ebram",$link);
        $q="SELECT * FROM site_sug";//
        $r=mysql_query($q,$link);
        $line= mysql_fetch_array($r);
        //while($line== $result->fetch_assoc()){
        echo $line['site_suggestion_id'].$line['site_suggestion']."<br>";       
        //echo $row["site_suggestion_id"].$row["site_suggestion_name"].$row["site_suggestion_date"].$row["site_suggestion"]."<br>";
        //}
    ?>
</body>
</html>

error log is:

Warning: mysql_connect(): Access denied for user 'barmante_ebram_u'@'localhost' (using password: YES) in /home/barmante/public_html/tt.php on line 11

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/barmante/public_html/tt.php on line 12

Warning: mysql_query() expects parameter 2 to be resource, boolean given in /home/barmante/public_html/tt.php on line 14

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/barmante/public_html/tt.php on line 15
Raptor
  • 53,206
  • 45
  • 230
  • 366
hosein
  • 1
  • 3
  • What's unclear about the error message? Your live environment needs a different username and/or password to your test environment. – Quentin Jan 14 '15 at 09:06
  • 1
    **Warning**: 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). – Quentin Jan 14 '15 at 09:06

3 Answers3

0

The first error is saying that your password to the username your supplied meaning that it can't connect to your mysql and the other error are saying that you should have provide resource to the 2nd parameter. You just interchanged the sequence of the parameters. Use the code below

<?php
        $link=mysql_connect("localhost","uname","pass");
        mysql_select_db($link,"barmante_ebram");
        $q="SELECT * FROM site_sug";//
        $r=mysql_query($link);
        $line= mysql_fetch_array($r);
        //while($line== $result->fetch_assoc()){
        echo $line['site_suggestion_id'].$line['site_suggestion']."<br>";       
        //echo $row["site_suggestion_id"].$row["site_suggestion_name"].$row["site_suggestion_date"].$row["site_suggestion"]."<br>";
        //}
    ?>

Note*: Don't use mysql_* functions, it is deprecated . Use mysqli or PDO prepared statements Hope this helps you

Utkarsh Dixit
  • 4,267
  • 3
  • 15
  • 38
0

Use this kind of debugging to trace error. I guess your all warning is related to USERNAME or PASSWARD. use correct username and password. and it will resolve problem.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

Use mysql_error() with each query so that you will get to know what is causing problem ( useful for beginner )

Gagan Upadhyay
  • 255
  • 2
  • 11
0

Assuming that you have correct permissions for the user, the following code should work. It will tell you whether you have error in connecting to the sql server, or there is any error in your query.

<?php
    $db_hostname = 'localhost';
    $db_database = 'barmante_ebram';
    $db_username = 'uname';
    $db_password = 'pass';
    $db_link= new MySQLi($db_hostname, $db_username, $db_password, $db_database);
    if(! $db_link)      //if($db_link->connect_errno > 0)
        die("Could not connect to database server\n" + $db_link->connect_error);

    $query = "SELECT * FROM site_sug";
    $result = $db_link->query($query);
    if(!result)
        die("Error in query".$db_link->error);
    $row = $result->fetch_assoc();
    echo $row['site_suggestion_id'].$line['site_suggestion'];

?>

Note: Please always do error checking (if( !successful) die(" ")). Your code does not have any. Its a good practice to follow.

diwakar
  • 21
  • 4