0

i can't seem to run this code under local host, my goal is to make a table that is supposed to show in a website but when i try to connect i get the error:

Warning: mysql_connect(): in C:\xampp\htdocs\PhpProject2\hent.php on line 5 can not connect

my site name: http://localhost/PhpProject2/hent.php

the code:

<html>
<body>
<?php 

mysql_connect('<server is here>','<my username here>','<password here>') 
or die('can not connect' );
mysql_select_db('<my username here>') or die ('can not connect to <username here>');  

$sql = "Select * from Customer";
$result = mysql_query($sql);
$number= mysql_num_rows($result);

for($i=0; $i < $number; $i++)
{
    $table = mysql_fetch_row($result);
    echo  $table[0], $table[1];
    echo '<br>';
}
?>
    </body>
</html> 

i'm using xampp and MySQL is running on port 3306:]

instead of my < username here >, < server is here >, < password here > there is real code :]

i would Appreciate any answer :]

Carlos Gavidia-Calderon
  • 7,145
  • 9
  • 34
  • 59
Madde
  • 471
  • 1
  • 7
  • 22
  • If this is new code start with using MYSQLI_ instead of MYSQL. MYSQL will be depreciated from PHP 5.5 onwards. – Mr. Radical Jan 22 '13 at 00:40
  • Are you using the correct localhost, username and password combination? Everything should be in "", like so "localhost", "root" and "" if you are using the default settings. – Mr. Radical Jan 22 '13 at 00:43
  • you mean with " " instead og ' '? – Madde Jan 22 '13 at 00:49
  • Have you tried the commandline? In the XAMPP control panel select SHELL. Enter in the commandline "mysql -h hostname -u username -p". Next give you password. – Mr. Radical Jan 22 '13 at 00:53
  • In quotes marks. You can also use the single quotes. – Mr. Radical Jan 22 '13 at 00:53
  • i logged in now, something special i can do there? :] – Madde Jan 22 '13 at 00:58
  • Connect to database with "connect databasename". You can do everything with the command line just like with PHP. I asked you to do this because I needed to check if you were using a wrong host, username, password and database combination. – Mr. Radical Jan 22 '13 at 00:59
  • i can't seem to be able to connect, uncertain if i'm actually using the correct name, i'm relative new at this :] – Madde Jan 22 '13 at 01:10
  • Were you able to login in with mysql -h hostname -u username -p? And not able to connect to you database? – Mr. Radical Jan 22 '13 at 01:13
  • i was able to login with msql -h hostname -u username -p :], i tried again now at it dosen't seem to work anymore – Madde Jan 22 '13 at 01:42
  • Could you try this: in the control panel select admin button after the mysql this is the second admin button from the top. Next you will get the phpmyadmin login screen. Type in your username and password. Then you will get a list of databases on the left side. B.T.W. have you inserted data in the database via this "program"? – Mr. Radical Jan 22 '13 at 01:43
  • First try phpmyadmin see my comment above. If this doesn't work proceed with the following suggestion.Did you close the command line window before logging in? I guess not so you are probably still logged in. In that case logging in again doesn't work. To make sure, close the command line window and open a new instance via the control panel. If you were able to login once you will be able to login in again. – Mr. Radical Jan 22 '13 at 01:54
  • oh, ye i se them :], i have normally used msyql workbench and putty :] i have changed the database name and tried to run what you said under, but it still seems to be the same problem :/ – Madde Jan 22 '13 at 02:00
  • I also use MySQL workbench and putty so your fine with that. In workbench look for the name of the database you are using. – Mr. Radical Jan 22 '13 at 02:26
  • it's called test, haha :D – Madde Jan 22 '13 at 02:42
  • it didn't work no, but the problem is rather that the host is unstable i think since it worked a little while at school :] i accepted your answer :D – Madde Jan 22 '13 at 09:58
  • I don't get why the host will be unstable. I should either work or it should not work at all. Are you using the same laptop with xampp installed on it while you are at school. If you are make sure that you start Apache server and the MySQL via the control panel or if you have installed a server check via the control panel if it is running. To check if everything is working you could also run the same procedure as in you previous post with regard to netstat. http://stackoverflow.com/questions/14245474/apache-wont-run-in-xampp – Mr. Radical Jan 22 '13 at 12:17

4 Answers4

1

Here is what you can do to see your error:

mysql_connect('<server is here>','<my username here>','<password here>') 
or die('Error: '.mysql_error() );

And try to refrain from using all functions starting with mysql_*. They are currently being depricated.

Use mysqli or pdo

Ibu
  • 42,752
  • 13
  • 76
  • 103
  • oh, thx :] the error says "error: A connection attempt failed because the connected party did not respond properly after a period of time, or established connection failed because the connected host did not respond." – Madde Jan 22 '13 at 00:44
  • Can you connect to mysql at all? check your host – Ibu Jan 22 '13 at 00:44
  • if you mean on xampp? :] it says that MySQL is running on port 3306, when i try to run other documents local it works fine – Madde Jan 22 '13 at 00:48
1

Try this:

<?php
  $host = "hostname";
  $user = "username";
  $password = "password";
  $database = "database";

  $link = mysqli_connect($host, $user, $password, $database);
  If (!$link){
      echo ("Unable to connect to database!");
  }
  else {
  $query = "SELECT * FROM Customer";    
  $result = mysqli_query($link,$query);

  while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
   echo  $row['<insert column name>']. "<br>";
  }
}
mysqli_close($link);
?>

I have used the MYSQL library in this code. You should check if you column in mysql is called 0 and 1. B.T.W. I am using WHILE instead of FOR loop that is just a personal preference.

Mr. Radical
  • 1,847
  • 1
  • 19
  • 29
  • is it possible to find out what database i'm using? :] – Madde Jan 22 '13 at 01:24
  • In the command line type "SHOW DATABASES;" Default database are: information_schema, cdcol, mysql, performance_schema, phpmyadmin and webauth. All other database could be yours. – Mr. Radical Jan 22 '13 at 01:30
  • @Madde just to be sure did you create or add anything to a database using phpmyadmin / command line /etc. Because you need to put data in a database to get it out ;-) – Mr. Radical Jan 22 '13 at 01:32
  • i didn't put anything in with the phpadmin no, but i have put in data in the table if that is what you mean? :] ex "insert into Customer VALUES(null, 'Hilda', 'Olsen', 'Hakedalsveien 1', '1482');" :) – Madde Jan 22 '13 at 02:41
  • @Madde phpMyAdmin is a well know program which is comparable to MySQL Workbench. However, MySQL Workbench has a much more powerful guided user interface (GUI). phpMyAdmin is in between the pure command line from MySQL and the more visual MySQL Workbench. All three allow you to insert / select /delete / update / etc. data into the database. – Mr. Radical Jan 22 '13 at 11:58
0

I would suggest you to start using PDO;

index.php

<?php 
require "dbc.php";

$getList = $db->getAllData(25);

foreach ($getList as $key=> $row) {
         echo $row['columnName'] .' key: '. $key;
    }
?>

dbc.php

<?php
class dbc {
public $dbserver = 'server';
public $dbusername = 'user';
public $dbpassword = 'pass';
public $dbname = 'db';

function openDb() {    
    try {
        $db = new PDO('mysql:host=' . $this->dbserver . ';dbname=' . $this->dbname . ';charset=utf8', '' . $this->dbusername . '', '' . $this->dbpassword . '');
    } catch (PDOException $e) {
        die("error, please try again " . $e);
    }        
    return $db;
}

function getAllData($qty) {
    //prepared query to prevent SQL injections
    $query = "Select * from Customer where zip= ?";
    $stmt = $this->openDb()->prepare($query);
    $stmt->bindValue(1, $qty, PDO::PARAM_INT);
    $stmt->execute();
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $rows;
}    
?>
Andrew
  • 7,619
  • 13
  • 63
  • 117
0

This is how I usually connect to an MySQL database. I have config.php

<?php

function db_connect(){
   $conn = new mysqli('localhost', 'root', 'leave_black_if_no_password_set', 'database_name');
   if (!$conn) {
      return false;
   }
   $conn->autocommit(TRUE);  
    return $conn;

}

?>

//end config

Now, in your other file just call config.php: `include 'config.php';

    <html>
    <body>
    <?php 
    include 'php/config.php';
    $dbCon = db_connect();


    $sql = "Select * from Customer";
    $result = mysqli_query($dbCon, $sql);
    $number= mysqli_num_rows($result);

   while($row=mysqli_fetch_array($result)){
    {
        echo  $row['column_name'];
        echo '<br>';
    }
//close conn
 mysqli_close($dbCon);
    ?>
        </body>
    </html> 
user1965451
  • 327
  • 2
  • 4
  • 12