BinaryMan
BinaryMan

Reputation: 289

How to connect to database in real server

I am trying to connect to my database wich it created in PhpMyAdmin in my own server. I write the code in a php file as below:

<?php 
   echo "welcome";
   echo "<br>";

   $conn = mysqli_connect("mydomainename.com:2080", "database_user_in phpmyadmin", "password of my database name", "name of database");
   $result = mysqli_query( $this->conn, "SELECT * FROM `ad` WHERE 1"); 
   while ( $row = mysqli_fetch_array( $result ) ) {
      $ad_level = $row['ad_level']; 
   }
   echo $ad_level;
?>

If I access to this page , its just return : welcome I think that the error in the syntax of 'mysqli_connect' .... ist correct ?? have anyone any idea about this ?????

How do I do that? I googled a lot, but either I used the wrong keywords or there are no simple solutions on the internet. I hope somebody here can help me.

Best regards and thanks in advance, Fadel.

Upvotes: 2

Views: 4700

Answers (5)

BinaryMan
BinaryMan

Reputation: 289

I replaced

$conn = mysqli_connect("mydomainename.com:2080", "database_user_in phpmyadmin", "password of my database name", "name of database");

by this:

$conn = mysqli_connect("localhost", "database_user_in phpmyadmin", "password of my database name", "name of database");

but why !!! i have not any idea !! just it worck ... why ???? have anyone any idea about this ????

Upvotes: 0

Rohan Kumar
Rohan Kumar

Reputation: 40639

Try this

Use $conn in place of $this->conn

$result = mysqli_query($conn, "SELECT * FROM `ad` WHERE 1");

If you have error in mysqli connection the use die after this function like

$conn= mysqli_connect("myhost","myuser","mypassw","mybd") 
                     or die("Error " . mysqli_error($conn));// use die here

Read this http://php.net/manual/en/function.mysqli-connect.php

Upvotes: 1

Hassan Sardar
Hassan Sardar

Reputation: 4523

Try this way. I am currently using this method and its working perfect:

<?php

    $server = "localhost";
    $login = "root";
    $pw = "myPassword";
    $db = "myDatabase";


$con = mysql_connect($server, $login, $pw);

mysql_select_db($db, $con);

$qry = "select * from members where Username ='$Username' and Password = '$Password' and Status = 1" ;

    $conQry = mysql_query($qry , $con);



?>

NOW YOU CAN FETCH YOUR DATA

Upvotes: -1

dhpratik
dhpratik

Reputation: 1138

the real syntax is

mysqli_connect(host,username,password,dbname,port,socket);

so as you have written host name along with the port there must be an error. you can refer the below link for more insite

http://www.w3schools.com/php/func_mysqli_connect.asp

Upvotes: 4

Hassan Sardar
Hassan Sardar

Reputation: 4523

Here is the general mySQL DB Connection Process:

<?php
   // Create connection
   $con = mysqli_connect("example.com", "peter", "abc123", "my_db");

   // Check connection
   if ( mysqli_connect_errno( $con ) ) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
?>

Upvotes: 1

Related Questions