1

Description :

I have started using mysqli_* functions before that I was using mysql_* now the mysqli_* requires a variable lets say $con to be passed as aenter code heren argument to the mysqli_* function which contains this;

$con = mysqli_connect("localhost","my_user","my_password","my_db"); 

Now I have a different page at which I am connecting to the database which is just included at every php page to keep the work going like this;

--------- connect.php----------

<?php
if(!mysql_connect("localhost","root",""))
{
    echo "cannot connet to the server";
}
if(!mysql_select_db("katchup"))
{
    echo "Cannot connect to the database";
}
?>

and other pages like this

----------- get_products.php -----------

include 'connect.php';

$result = mysql_query("any query"); // this is what I have 

$result = mysqli_query($con , "any query"); // this is what I want

My question is how do I get the $con in connect.php in other pages?

4
  • you have to go with mysqli from the scratch dont use mysql the tag is deprecated now and php dont support it anymore Commented Oct 25, 2014 at 6:44
  • 3
    @AitazazKhan That's what he's trying to learn how to do. Commented Oct 25, 2014 at 6:44
  • @Arif_suhail_123 but we should give him reason why this was nt working Commented Oct 25, 2014 at 6:46
  • PHP deprecated mysql api you should use mysqli instead. $con = mysqli_connect("localhost","my_user","my_password","my_db"); this is right way... and if you are creating function for query you just need to call global $con inside the function. Commented Oct 25, 2014 at 6:49

2 Answers 2

2

put this in your connection file

<?php
//mysqli_connect("servername","mysql username","password",'database')
$con = mysqli_connect("localhost","root","",'business');

if(!$con)
{
    echo "cannot connet to the server";
}

?>

And in your get product.php etc file use mysqli like this.

<?php
 include('connection.php');
 $query=mysqli_query($con,"your query");
//for single record
 if($row=mysqli_fetch_array($query))
 {
   your data will be here
 }
//for multiple records
while($row=mysqli_fetch_array($query))
{
//your data will be fetched here
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Very simple.

In connect.php

$con = mysqli_connect("localhost","my_user","my_password","my_db");
if ($con->connect_errno) echo "Error - Failed to connect to database: " . $con->connect_error;

Then $con will be available in the php scripts you include connect.php in (after you include), and you can use this;

$result = mysqli_query($con , "any query");

or you can use OO instead if you want, like this;

$result = $con->query("any query");

To use the connection in functions, you can either pass as a variable, or make $con global inside the function using global $con;.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.