geniesgona Posted March 5, 2015 Share Posted March 5, 2015 i want to connect database but unable using this code <?php session_start(); if(isset($_POST['submit'])) { $link = mysql_connect("localhost","root",""); $db = mysql_select_db('cms',$con); $email=$_POST['email']; $name=$_POST['name']; $subject=$_POST['subject']; $designation=$_POST['designation']; $num=$_POST['#']; //$add_type=$_POST['add_type']; // echo $q= "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error()); $sql = mysql_query($q); $q= "select * from contactus " or die(mysql_error()); $sql = mysql_query("select * from contactus"); //$res=mysql_fetch_object($sql); $rows = mysql_num_rows($sql); ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted March 5, 2015 Share Posted March 5, 2015 i want to connect database but unable What does that mean? if you are getting errors then post them in full here. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 5, 2015 Share Posted March 5, 2015 The database connection link is being saved to $link. $link = mysql_connect("localhost","root",""); You need to use the same variable when calling mysql_select_db(). Try changing this: $db = mysql_select_db('cms',$con); To this: $db = mysql_select_db('cms',$link); Note that there are other problems with the code, but let's start with establishing a database connection. Quote Link to comment Share on other sites More sharing options...
geniesgona Posted March 5, 2015 Author Share Posted March 5, 2015 What does that mean? if you are getting errors then post them in full here. <?php session_start(); if(isset($_POST['submit'])) { $con = mysql_connect("localhost","root",""); $db = mysql_select_db('cms',$con); $email=$_POST['email']; $name=$_POST['name']; $subject=$_POST['subject']; $designation=$_POST['designation']; $num=$_POST['#']; //$add_type=$_POST['add_type']; // echo $q= "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error()); $sql = mysql_query($q); $q= "select * from contactus " or die(mysql_error()); $sql = mysql_query("select * from contactus"); //$res=mysql_fetch_object($sql); $rows = mysql_num_rows($sql); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>sessions</title> <link type="text/css" rel="stylesheet" href="styles.css" /> </head> <body style="margin-top:0px; margin-left: 0px; background-color:#E6E6FA"> <div id="content" style="height:130px; width:1365px; float:center; background-color:#BA55D3" > <img src="images/image.png"> <ul id="menu"> <div id="header2"> <li><a href="home.php"><b>Home</b><a></li> <li><a href="login.php"><b>Login</b><a></li> <li><a href="signup.php"><b>Signup</b><a></li> <li><a href="about.php"><b>About</b><a></li> <li><a href="contact us.php"><b>Contact Us</b></a></li> </ul> </div> </div> <div align="center"> <form method="post" action=""> <h2>DETAIL OF OFFICERS OF CS&&IT DEPARTMENT</h2></div> </br></br> <table id="abc" border="1" align="center" bgcolor="white" style="width:60%"> <?php while($rows=mysql_fetch_array($sql)){ ?> <?php echo $row[0]; ?><br /> <?php echo $row[1]; ?><br /> <?php echo $row[2]; ?><br /> <?php } ?> </div> </table> </form> </html> Quote Link to comment Share on other sites More sharing options...
geniesgona Posted March 5, 2015 Author Share Posted March 5, 2015 The database connection link is being saved to $link. $link = mysql_connect("localhost","root",""); You need to use the same variable when calling mysql_select_db(). Try changing this: $db = mysql_select_db('cms',$con); To this: $db = mysql_select_db('cms',$link); Note that there are other problems with the code, but let's start with establishing a database connection. tried but do not work <?php session_start(); if(isset($_POST['submit'])) { $con = mysql_connect("localhost","root",""); $db = mysql_select_db('cms',$con); $email=$_POST['email']; $name=$_POST['name']; $subject=$_POST['subject']; $designation=$_POST['designation']; $num=$_POST['#']; //$add_type=$_POST['add_type']; // echo $q= "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error()); $sql = mysql_query($q); $q= "select * from contactus " or die(mysql_error()); $sql = mysql_query("select * from contactus"); //$res=mysql_fetch_object($sql); $rows = mysql_num_rows($sql); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>sessions</title> <link type="text/css" rel="stylesheet" href="styles.css" /> </head> <body style="margin-top:0px; margin-left: 0px; background-color:#E6E6FA"> <div id="content" style="height:130px; width:1365px; float:center; background-color:#BA55D3" > <img src="images/image.png"> <ul id="menu"> <div id="header2"> <li><a href="home.php"><b>Home</b><a></li> <li><a href="login.php"><b>Login</b><a></li> <li><a href="signup.php"><b>Signup</b><a></li> <li><a href="about.php"><b>About</b><a></li> <li><a href="contact us.php"><b>Contact Us</b></a></li> </ul> </div> </div> <div align="center"> <form method="post" action=""> <h2>DETAIL OF OFFICERS OF CS&&IT DEPARTMENT</h2></div> </br></br> <table id="abc" border="1" align="center" bgcolor="white" style="width:60%"> <?php while($rows=mysql_fetch_array($sql)){ ?> <?php echo $row[0]; ?><br /> <?php echo $row[1]; ?><br /> <?php echo $row[2]; ?><br /> <?php } ?> </div> </table> </form> </html> Quote Link to comment Share on other sites More sharing options...
geniesgona Posted March 5, 2015 Author Share Posted March 5, 2015 What does that mean? if you are getting errors then post them in full here. Also it says SYNTAX error line 62 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 5, 2015 Share Posted March 5, 2015 tried but do not work What do the error messages say? Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 5, 2015 Share Posted March 5, 2015 Also it says SYNTAX error line 62 It looks like you're missing an end curly quote for the first if condition. I would imagine that one is suppose to go here: //$res=mysql_fetch_object($sql); $rows = mysql_num_rows($sql); } Quote Link to comment Share on other sites More sharing options...
geniesgona Posted March 5, 2015 Author Share Posted March 5, 2015 It looks like you're missing an end curly quote for the first if condition. I would imagine that one is suppose to go here: //$res=mysql_fetch_object($sql); $rows = mysql_num_rows($sql); } Thanx a little more help needed now it look likes Quote Link to comment Share on other sites More sharing options...
CroNiX Posted March 5, 2015 Share Posted March 5, 2015 Pretty self explanatory....you are using a variable, $sql, that you haven't defined. So your query fails. Quote Link to comment Share on other sites More sharing options...
geniesgona Posted March 5, 2015 Author Share Posted March 5, 2015 Pretty self explanatory....you are using a variable, $sql, that you haven't defined. So your query fails. Then what should i do now? will u please help me? Quote Link to comment Share on other sites More sharing options...
Landslyde Posted March 5, 2015 Share Posted March 5, 2015 geniesgona: Read this: http://codular.com/php-mysqli It will give you beginner information for connecting to a database. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 5, 2015 Share Posted March 5, 2015 Then what should i do now? will u please help me? Basically, most of your database stuff happens here: <?php session_start(); if(isset($_POST['submit'])) { $con = mysql_connect("localhost","root",""); $db = mysql_select_db('cms',$con); $email=$_POST['email']; $name=$_POST['name']; $subject=$_POST['subject']; $designation=$_POST['designation']; $num=$_POST['#']; //$add_type=$_POST['add_type']; // echo $q= "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error()); $sql = mysql_query($q); $q= "select * from contactus " or die(mysql_error()); $sql = mysql_query("select * from contactus"); //$res=mysql_fetch_object($sql); $rows = mysql_num_rows($sql); } ?> Note that the code only executes after the form submits. And then you have the following code which executes whether or not the form has been submitted: <?php while($rows=mysql_fetch_array($sql)){ ?> <?php echo $row[0]; ?><br /> <?php echo $row[1]; ?><br /> <?php echo $row[2]; ?><br /> <?php } ?> So...$sql is not going to be defined until the form is submitted. Also note that the following lines are all messed up: //$add_type=$_POST['add_type']; // echo $q= "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error()); $sql = mysql_query($q); $q= "select * from contactus " or die(mysql_error()); $sql = mysql_query("select * from contactus"); //$res=mysql_fetch_object($sql); $rows = mysql_num_rows($sql); First, you're using $q before it's defined. The " or die(..." part needs to be connected with mysql_query. Then you use mysql_query() again which doesn't use $q. Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted March 5, 2015 Share Posted March 5, 2015 Your code is wrong. You cannot or its not logical to put db connection within an isset function wrong code: <?php session_start(); if(isset($_POST['submit'])) { $con = mysql_connect("localhost","root",""); $db = mysql_select_db('cms',$con); $email=$_POST['email']; $name=$_POST['name']; $subject=$_POST['subject']; $designation=$_POST['designation']; $num=$_POST['#']; //$add_type=$_POST['add_type']; // echo $q= "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error()); $sql = mysql_query($q); $q= "select * from contactus " or die(mysql_error()); $sql = mysql_query("select * from contactus"); //$res=mysql_fetch_object($sql); $rows = mysql_num_rows($sql); } ?> Right code: <?php session_start(); $con = mysql_connect("localhost","root",""); $db = mysql_select_db('cms',$con); if(isset($_POST['submit'])) { $email=$_POST['email']; $name=$_POST['name']; $subject=$_POST['subject']; $designation=$_POST['designation']; $num=$_POST['#']; //$add_type=$_POST['add_type']; // echo $q= "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error()); $sql = mysql_query($q); $q= "select * from contactus " or die(mysql_error()); $sql = mysql_query("select * from contactus"); //$res=mysql_fetch_object($sql); $rows = mysql_num_rows($sql); } ?> Preferably include or require_once (dbcon.php) //if isset($_POST) goes here: hope this helps Quote Link to comment Share on other sites More sharing options...
geniesgona Posted March 5, 2015 Author Share Posted March 5, 2015 Your code is wrong. You cannot or its not logical to put db connection within an isset function wrong code: <?php session_start(); if(isset($_POST['submit'])) { $con = mysql_connect("localhost","root",""); $db = mysql_select_db('cms',$con); $email=$_POST['email']; $name=$_POST['name']; $subject=$_POST['subject']; $designation=$_POST['designation']; $num=$_POST['#']; //$add_type=$_POST['add_type']; // echo $q= "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error()); $sql = mysql_query($q); $q= "select * from contactus " or die(mysql_error()); $sql = mysql_query("select * from contactus"); //$res=mysql_fetch_object($sql); $rows = mysql_num_rows($sql); } ?> Right code: <?php session_start(); $con = mysql_connect("localhost","root",""); $db = mysql_select_db('cms',$con); if(isset($_POST['submit'])) { $email=$_POST['email']; $name=$_POST['name']; $subject=$_POST['subject']; $designation=$_POST['designation']; $num=$_POST['#']; //$add_type=$_POST['add_type']; // echo $q= "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error()); $sql = mysql_query($q); $q= "select * from contactus " or die(mysql_error()); $sql = mysql_query("select * from contactus"); //$res=mysql_fetch_object($sql); $rows = mysql_num_rows($sql); } ?> Preferably include or require_once (dbcon.php) //if isset($_POST) goes here: hope this helps thanx but do not work Quote Link to comment Share on other sites More sharing options...
geniesgona Posted March 5, 2015 Author Share Posted March 5, 2015 Basically, most of your database stuff happens here: <?php session_start(); if(isset($_POST['submit'])) { $con = mysql_connect("localhost","root",""); $db = mysql_select_db('cms',$con); $email=$_POST['email']; $name=$_POST['name']; $subject=$_POST['subject']; $designation=$_POST['designation']; $num=$_POST['#']; //$add_type=$_POST['add_type']; // echo $q= "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error()); $sql = mysql_query($q); $q= "select * from contactus " or die(mysql_error()); $sql = mysql_query("select * from contactus"); //$res=mysql_fetch_object($sql); $rows = mysql_num_rows($sql); } ?> Note that the code only executes after the form submits. And then you have the following code which executes whether or not the form has been submitted: <?php while($rows=mysql_fetch_array($sql)){ ?> <?php echo $row[0]; ?><br /> <?php echo $row[1]; ?><br /> <?php echo $row[2]; ?><br /> <?php } ?> So...$sql is not going to be defined until the form is submitted. Also note that the following lines are all messed up: //$add_type=$_POST['add_type']; // echo $q= "select * from contactus where email='".$email."' and password='".$password."' " or die(mysql_error()); $sql = mysql_query($q); $q= "select * from contactus " or die(mysql_error()); $sql = mysql_query("select * from contactus"); //$res=mysql_fetch_object($sql); $rows = mysql_num_rows($sql); First, you're using $q before it's defined. The " or die(..." part needs to be connected with mysql_query. Then you use mysql_query() again which doesn't use $q. Thanx For You rEffort But did'nt work Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 5, 2015 Share Posted March 5, 2015 (edited) Thanx For You rEffort But did'nt work It might help to see what you tried. Please surround the code with tags. It's makes your post and code easier to follow. Note that my post didn't change any of the code...I was just pointing out some of the inadequacies. Edited March 5, 2015 by cyberRobot Quote Link to comment Share on other sites More sharing options...
geniesgona Posted March 5, 2015 Author Share Posted March 5, 2015 It might help to see what you tried. Please surround the code with tags. It's makes your post and code easier to follow. Note that my post didn't change any of the code...I was just pointing out some of the inadequacies. Look how i show table in the center of the page this is my display.php <code><?php error_reporting(0); $name=$_POST ['name']; $password=$_POST ['pass']; $sel=$_REQUEST['sel']; echo $sel; session_start(); if( !isset($_SESSION['username'])){ header('Location:login.php'); die(); } ?> <?php $con=mysql_connect("localhost","root",""); $db=mysql_select_db("ss1"); $query=mysql_query("select * from user where password='$sel'"); ?> <html xmlns="http://www.w3.org/1999/xhtml"> <html> <head> <style> table { margin-left:350px; margin-top:200px; text-align:center; } th{ background-color:gainsboro; } tr:hover{ background-color: #00FF66; } h1{ color:red; tab-size: 10px; font-family: initial; font-style: oblique; font-variant: small-caps; font-weight: bolder; } </style> <title>Table page</title> </head> <body bgcolor="pink"> <?php if(isset($_REQUEST['sel'])) { ?> <table cellspacing="50" border="1" bgcolor="#CCCCCC" bordercolor="#CC3300"> <tr><th>name</th><th>password</th><th>City</th></tr> <?php while($row=mysql_fetch_array($query)) {?> <tr><td><?php echo $row['0'];?></td> <td><?php echo $row[1];?></td> <td><?php echo $row[2];?></td> </tr> <?php } ?> </table> <?php } ?> <form method="post" action=""> <select name="sel"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> <input type="submit"/> </form> <a href="logout.php">Logout</a> </body> </html> </code> Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 5, 2015 Share Posted March 5, 2015 It looks like you have errors disabled. error_reporting(0); Try turning them on during the debugging process: error_reporting(E_ALL); ini_set('display_errors', 1); Are you seeing any errors? If so, what are they? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.