Pratik Zinzuvadiya
Pratik Zinzuvadiya

Reputation: 1

not able to establish database connection with php

firstly i have index.php in which code consists as below

<form action="connection.php" method="post">
<table>
    <thead><tr><td>Enter Item Details</td></tr></thead>
    <tr>
        <td>Item name :- </td>
        <td><input type="text" maxlength="30" name="item_name"></td>
    </tr>
    <tr>
    <td>Item Desc :- </td>
    <td><input type="text" maxlength="150" name="item_desc"></td>
    </tr>
    <tr>
    <td colspan="2"><input type="submit" value="Add Item">
    </td></tr>
</table>

</form>

and my connection.php consists code as below

$host="localhost";
$username="root";
$password="";
$db_name="eatery";
$tbl_name="test_item";
mysql_connect("$host", "$username", "$password") or die("Not able to connect to MySql");

echo "connected";
mysql_select_db("$db_name") or die ("Not able to connect to eatery database");

I have installed wamp package to run apache server and my php code. Now while clicking on submit button of index.php, instead of database connection successful / fail message it shows inside code of connection.php. so what am i missing here?

Upvotes: 0

Views: 59

Answers (3)

lagbox
lagbox

Reputation: 50561

Wrap that connection.php file in php tags:

<?php

// your php code

?>

UPDATE:

If this still isnt' working. Maybe you should make sure apache is using php. And you are serving your files through apache and not directly from the filesystem.

Upvotes: 1

echo_Me
echo_Me

Reputation: 37243

you have to pass variable as variable not as string .

change this

  mysql_connect("$host", "$username", "$password")or die ....
  mysql_select_db("$db_name")or die...

to

 <?php
.....
.....
.....
 mysql_connect($host, $username, $password) or die ....
 mysql_select_db($db_name)or die ...
 ......
 ?>

and to debug the error and see the exact error use this instead of you error messages.

or die(mysql_error());

Upvotes: 1

DKReigns
DKReigns

Reputation: 23

In your connection.php try using this instead:

$con = mysqli_connect("localhost","root","","eatery");

or you can just use:

mysqli_connect("localhost","root","","eatery");

it should work! Cheers! :)

Upvotes: 0

Related Questions