i had make php file into it function [getLink()] make connection with mysqli_connect and return link of connection to use it in another php file to get result from database with mysqli_query()

connection.php `

<?php


  $userName="root";
  $serverName="localhost";
  $userPassword="*****";
  $nameOfDataBase="ITI_System";


    function getLink(){

       $link=@mysqli_connect($serverName,$userName,$userPassword,$nameOfDataBase);
         if(!$link){

                echo "connection Error".mysqli_connect_errno();
         }

         return $link;

    }

?>

and anothor file Login.php

<?php

           include("connection.php");

                   $link=getLink();

                   $SQLstatement="select user_name from student";                  
                   $result=mysqli_query($link , $SQLstatement);                
                   var_dump($result); 
                   if(mysqli_num_rows($result) != 0){                  
                        while($data=mysqli_fetch_assoc($result)){                    
                            $studentNames[]=$data['user_name'] ;
                        }
                    }

                    $SQLstatement="select admin_name from administrator";
                    $result=mysqli_query($link,$SQLstatement);
                    var_dump($result); 
                    if(mysqli_num_rows($result) != 0){                  
                        while($data=mysqli_fetch_assoc($result)){
                            $adminNames[]=$data['admin_name'];
                        }                       
                    }

?>

but $result is false
and mysqli_query doesn`t make sql statement and fetch data from database

Recommended Answers

All 5 Replies

You should pass your getLink function connection parameters since it can not access variables out of it's scope:

 function getLink($serverName,,$userName,$userPassword,$nameOfDataBase){
    $link=@mysqli_connect($serverName,$userName,$userPassword,$nameOfDataBase);
    if(!$link){
        echo "connection Error".mysqli_connect_errno();
    }
    return $link;
}

There are many ways to kill a rat. So, try this. It also works;

<?php
$host="localhost"; // Host name 
            $username="root"; // Mysql username 
            $password=""; // Mysql password 
            $db_name="database_name"; // Database name 

            // Connect to server and select database.
            mysql_connect("$host", "$username", "$password")or die("cannot connect".mysql_error()); //connecting to localhost
            mysql_select_db("$db_name")or die("cannot select DB"); //selecting database


?>

mysql_connect this extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. :(

@broj1
but i had make ( connection.php )file to put hostname and username and password and database name only one time and for security issue (to be hidden in this file)

if i make function getLink take hostname and username and password and database name as parameters every time then i think there no reason to make connection.php file

and i test into ( Login.php ) if $link return or not

$link=getLink();

and $link identical to $link into ( connection.php )

Member Avatar for diafol

I think you're missing the point amhed.

There are many ways to do this:

  1. The easiest maybe is as webville suggests - although use the mysqli version as pointed out by homeboy.

  2. As broj1 suggests (but take out the extra comma):

    function getLink($serverName,$userName,$userPassword,$nameOfDataBase)

  3. Place the connections details inside the getLink() function

You could even have something like:

include('conndetails.php');  //contains connection details
include('functions.php'); //contains getLink() function (as well as other common functions)

If your problem is scope - you could store your connection details and call them as global variables (NOT RECOMMENDED) or use constants (convention is to use capitals for the name):

define("DBUSER","myname");
define("DBPASS","mypassword");

etc. These constants are then available throughout your code:

function getLink(){
 $link=@mysqli_connect(DBSERVER,DBUSER,DBPASS,DBNAME);
    if(!$link){
        echo "connection Error".mysqli_connect_errno();
    }
    return $link;
}

Whichever way you choose, sensitive data should be stored above the public root.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.