Problem in displaying and connectivity with mysql in php - 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!');
}
$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!');
}

Related

sql queries embeded in php code don't get executed while working with cpanel

I am new at cpanel and I encountered a problem. I have made a database and a database user for it in cpanel and successfully connected to them via my php code. In spite of that I have a successful connection to database but none of my queries run in the application code (while they run in the phpMyadmin!).
<?php
session_start();
$conn =new mysqli('localhost','myDBname','myDBpass','myDBuser');
if($conn)
echo "<script>alert('successful connection');</script>";
$rawresults ="SELECT * FROM `articles`";
$result = $conn->query($rawresults);
if($result->num_rows>0)
{
echo "<script>alert('dd')</script>";
$_SESSION["i"]=0;
while($results = $result->fetch_assoc())
{
setcookie("searchResult","yes");
$_SESSION["topic".$_SESSION["i"]]=$results['topic'];
$_SESSION["name".$_SESSION["i"]]=$results['fileName'];
$_SESSION["texts".$_SESSION["i"]]=$results['texts'];
$_SESSION["i"]++;
}
header('location:index.php');
}
else if($result->num_rows==0)
{
echo "<script>alert('cc')</script>";
setcookie("searchResult","yes");
header('location:index.php');
}
?>
The problem is that I permanently face: alert(cc)! While table 'articles'
contains lots of information and num_rows is a positive value.
I 'd like to mention again that connection to db has no problem and I get alert(successful connection).
Try this after connecting:
if ($conn->connect_error) {
die('Connect Error (' . $conn ->connect_errno . ') '
. $conn->connect_error);
}

Convert mysql_* to PDO

I have made a program using PHP and trying to store data into Local Server Xampp, but whenever i run my php script using this url:
http://127.0.0.1/test.php
Getting error message: {"StatusID":"0","Error":"Cannot save data!"}
Please someone help me in this how can i make it useful for me, please check below PHP Script:
<?php
$objConnect = mysql_connect("localhost","root","");
mysql_error($ObjConnect);
$objDB = mysql_select_db("registration_login");
mysql_error($ObjDB);
$strUsername = $_POST["sUsername"];
$strPassword = $_POST["sPassword"];
$strName = $_POST["sName"];
$strEmail = $_POST["sEmail"];
$strTel = $_POST["sTel"];
/*** Insert ***/
$strSQL = "INSERT INTO member (Username,Password,Name,Email,Tel)
VALUES (
'".$strUsername."',
'".$strPassword."',
'".$strName."',
'".$strEmail."',
'".$strTel."'
)
";
$objQuery = mysql_query($strSQL);
mysql_error($ObjQuery);
if(!$objQuery)
{
$arr["Status"] = "0";
$arr["Message"] = "Cannot Save Data!";
echo json_encode($arr);
exit();
}
else
{
$arr["Status"] = "1";
$arr["Message"] = "Register Successfully!";
echo json_encode($arr);
exit();
}
mysql_close($objConnect);
?>
Note: I have created registration_login database and member table under this DB..
Why don't you return the error reported by mysql or log it somewhere?
$objConnect = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
You forgot to check the return the return value to see if this was successful - if it failed, the reason is in mysql_error()
$objDB = mysql_select_db(DB_DATABASE);
You forgot to check the return the return value to see if this was successful - if it failed, the reason is in mysql_error()
$objQuery = mysql_query($strSQL);
At least this time you check the return value - but you don't check what the error was.
BTW your script is wide open to SQL injection.
Convert mysql_* to PDO
What has that got to do with your post?

mysql permissions with 1and1

Does anyone know to give my user account access to a table? I connect to the database fine but when I try to select the table it tells me Access denied for user 'dboxxxxx'#'%' to database.
I'm using basic php code
$con = mysqli_connect('xxxxxx.db.1and1.com', 'xxxxx', 'xxxxxx');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
else {echo 'success!';}
$selected = mysqli_select_db($con,'userTable');
if(!$selected){
die ('cannot use db: '.mysqli_error($con));
}
There is no permissions options on the 1and1 phpmyadmin that I can see unless I'm totally missing it. Anyone have any ideas?
First of all, you need to supply dboxxxxx as a username, not 'dboxxxxx'#'%'
Are you running this code localy or within your server? The 1und1 firewall blocks connections from the foreign networks
You're probably not making the connection to the database correctly.
Try the changes I made. Note that I do not know that you are using classes and functions.
UPDATE
$mysqli = new mysqli('xxxxx.db.1and1.com', 'xxxx', 'xxxx','xxxx');
if(mysqli_connect_errno ())
{
printf('Unable to connect to database ' . mysqli_connect_error());
}
else
{
$stmt = $mysqli->stmt_init();
if(!$stmt)
{
echo "init failed";
}
else
{
$cmd = "insert into `userTable` (`oauthtoken`,`oauthtokensec`,`userid`,screenname`) VALUES ('?','?','?','?')";
if($stmt->prepare($cmd))
{
$stmt->bind_param('1','1','1','1');
$stmt->execute();
echo $stmt->affected_rows . " Row(s) Inserted";
$stmt->close();
}
else
{
echo "Prepare Failed";
}
} $mysqli->close();
}

mysql not connect with php scripts?

<?php
$cons=mysql_connect("localhost","root","");
mysql_select_db("infogallery") or die('Not Connected to the data base:'.mysql_error());
?>
I write above code for connection with mysql but when i run this scripts..nothing display on the brouser...what can i do for the connection with mysql....
If nothing is displayed, then it means it succeeded. Add more code which queries the database and displays some results.
Don't connect as the root account. Create an account specifically for playing around with.
Once you've done that, modify your code as follows:
$cons = mysql_connect('localhost', 'username', 'password');
if ($cons === FALSE) {
die("Failed to connect to MySQL: " . mysql_error());
}
mysql_select_db(etc.....);
You don't check if the connection failed, then try to do a database operation on that potentially failed connection. The or die(...) you have will only show the error caused by the select attempt, and the error message from the failed connection will be lost.
I like to just do
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("infogallery") or die(mysql_error());
echo "So far, so good.";
How about something like the following:
<?php
try {
$cons = mysql_connect("localhost","username","password");
} catch ($e) {
die('Failed to connect to the database: ' . mysql_error());
}
try {
mysql_select_db("infogallery", $cons);
} catch ($e) {
die('Failed to select the database: ' . mysql_error());
}
?>

Trouble connecting to MySQL

I'm trying to connect to a mysql database with php and myadmin. I've tried a lot of codes I could find online, but I just can't put this thing to work...
Can anyone please tell me what I might be doing wrong?
this is the php script I am using:
<?php
$useremail = $_POST["useremail"];
$password = $_POST["password"];
if($useremail && $password){
// open database
$connect = mysql_connect("localhost", "carlos", "nenem");
if(!$connect){
die ("Not able to connected to the database: " .mysql_error());
}
// select database
$select_db = mysql_select_db("vergilioDB", $connect);
if(!$connect_db){
die("Not able to connect to the database: " .mysql_error());
}
mysql_close($connect);
} else {
die("Please enter useremail and password, or REGISTER if you are a new user!");
}
?>
Carefull, $select_db != $connect_db
The variable names are different, rewrite to:
$select_db = mysql_select_db("vergilioDB", $connect);
if(!$select_db){
die("Not able to connect to the database: " .mysql_error());
}
note that you are closing your MySQL connection regardless of anything else before you ever use it for anything..
mysql_close($connect);
You likely want to separate your connection logic from your login logic. You likely need the DB connection to validate the password no matter what and your DB connection should be the first thing you do to make sure you can even DO anything with a given page...

Categories