Problem in displaying and connectivity with mysql in php
I am new in php, I created a page with mysql db connectivity but when the page is run it displays a blank page. If i write a echo statement before the connection statement then only echo statement is displayed and nothing else is displayed. Here is my code..
$con = mysql_connect('localhost','root','admin');
mysql_select_db('testdb',$con);
if ($con)
{
die('Connected to database!');
}开发者_C百科
$sql = " INSERT INTO customer ([Name],[Website]) VALUES('$_POST[fname]','$_POST[lname]') ";
$result = mysql_query($sql, $con);
if(!$result)
{
echo mysql_error();
exit;
}
// close the connection
mysql_free_result($result);
mysql_close($con);
Anyone please help why this problem occurs and is there is anything wrong in the page.
Display Errors was off. I edit in the php.ini file 'display_errors' On. But still the connectivity issue is not resolved. It displays an fatal error at connectivity line statement.
fatal error: Call to undefined function mysql_connect()
$con = mysql_connect('localhost','root','admin');
mysql_select_db('testdb',$con);
if (!$con)
{
die('Not Connected to database!');
}
$sql = " INSERT INTO customer ([Name],[Website]) VALUES('$_POST[fname]','$_POST[lname]') ";
$result = mysql_query($sql, $con);
if(!$result)
{
echo mysql_error();
exit;
}
else
{
echo 'Query Success';
}
// close the connection
mysql_free_result($result);
mysql_close($con);
As you have
die('Connected to database!');
this will stop the script here, script written after it will not be executed , use instead
echo('Connected to database!');
[Name],[Website] should be replace with Name, website
your script dies everytime it success to connect to DB
change
if ($con)
{
die('Connected to database!');
}
to
if (!$con)
{
die('Connected to database!');
}
精彩评论