So I'm trying to connect my very basic android application to an apache server.
I went as far as downloading wampserver and have started coding the php files to connect and post (both separate files). While it is connecting to my database, it is failing to retrieve or to post data into the database using php.
<?php
require "init.php";
$name="Lucky";
$message="Getlow";
$sql_query="select * from post_its where name = '$name';";
$result= mysqli_query($con,$sql_query);
if(mysqli_num_rows($result)>0)
{
echo "Login Successful";
}
else
{
echo "Error";
}
?>
My php file for connecting to the database is given below:
<?php
$db_name="webappdb";
$mysql_user="root";
$mysql_pass="";
$server_name="localhost";
$con=mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
if(!$con)
echo Connection Error;
else
echo <h3>Connection Success</h3>
?>
In my browser when I try and run my post.php file, it shows me:
Connection Success ?>(followed by random chinese characters)
Please help!
Related
I currently working on PHP app which is run on blumix and I have used the following code to connected to this app to Compose for MySQL.But the connection has failed and shown me an error message on the browser " lastapp.eu-gb.mybluemix.net is currently unable to handle this request. HTTP ERROR 500"
the code below has been used to create the connection.
enter link description here enter image description here
When executing this code I could connect to your database:
<?php
$conn = mysqli_connect("sl-eu-lon-2-portal.11.dblayer.com", "admin", "FWXJWBNMHRFNUOVG", "compose", 27707);
if (!$conn) {
echo "Error!";
die();
}
else {
echo "Success!<br>";
echo "Details: ".mysqli_get_host_info($conn);
}
mysqli_close($conn);
?>
How is it is can not create a database on phpMyAdmin? I connect to PHP just fine, no error message. But when I am trying to create a database so phpMyAdmin, I keep getting error. In case it matters I have one page with a <form> and has a submit to my second.php file where my database code is (second.php is shown below).
I am pretty much trying to display the database in phpMyAdmin but nothing shows up:
<html>
<body>
<?php
$con = mysqli_connect("localhost", "user", "password");
if(!$con){
echo "could not connect";
} else {
echo "good connection";
}
//creation of database
$sql= 'CREATE DATABASE project';
if(mysql_query($sql,$con)){
echo 'DB created succesfully';
} else {
echo 'error creating DB' . mysql_errno();
}
?>
</body>
</html>
One issue is you connected with mysqli and are using mysql the rest of the time. Pick 1 (not mysql) library and use only that.
if(mysqli_query($con, $sql)){
and
echo 'error creating DB' . mysqli_error($con);
My connection file is conn.php, adminname and password are the table field name and form text box name. when this code run on server FTP it shows No database selected. But i include connection file and update database on FTP server. This code is run on local wamp server.
**my login coding is:**
// this is my login page.
<?php
session_start();
// start here session
include('conn.php');
// here include connection file
if(isset($_POST['login']))
{
$sql="select * from admin where adminname='".$_POST['adminname']."'and password='".$_POST['password']."'";
// this is my sql query which select adminname and password in table
$result=mysql_query($sql) or die(mysql_error());
if($result)
{
$row=mysql_fetch_array($result);
if(mysql_num_rows($result)>0)
{
$_SESSION['admin']=$row['adminname'];
header("location:home.php");
}
else
{
header("location:index.php");
}
}
}
?>
add one line in conn file after getting connection from database
<?php
mysql_select_db ( string $database_name);
?>
Make sure about few things:
First check if you have created a database or not?
Make sure you entered the correct db_hostame, db_username, db_password, dbname
tricky way to make a database connection that works in your localhost and real server is:
$host = $_SERVER['HOST_NAME'];
if( $host == "localhost" ){
// localhost settings
}
else{
// Server Settings
}
it's just a simple trick.
I use this code to connect to SQL Server 2012, but it does not work
<?php
$objConnect = mssql_connect("localhost","usr","pass");
if($objConnect)
{
echo "Database Connected.<br />";
echo mssql_error();
}
else
{
echo "Database Connect Failed.<br />";
}
mssql_close($objConnect);
?>
It always prints Database Connect Failed.
Also how can I connect to my local SQL Server from an external web server?
If your edition is SQL Server Express, you should probably be using:
$objConnect = mssql_connect("localhost\SQLEXPRESS","usr","pass");
Or if it is otherwise a named instance, then
$objConnect = mssql_connect("localhost\InstanceName","usr","pass");
If you need to connect remotely, then obviously you shouldn't be using localhost since how does the remote web server locate your localhost? You should be using one of the following (assuming the remote web server can see your machine with IP address 192.168.5.22):
$objConnect = mssql_connect("192.168.5.22\SQLEXPRESS","usr","pass");
$objConnect = mssql_connect("192.168.5.22\NamedInstance","usr","pass");
$objConnect = mssql_connect("192.168.5.22","usr","pass");
Of course your firewall must have port 1433 (and possibly 1434) open in order to accept that connection, and there are a variety of other things that can go wrong here as well.
However, a little debugging 101 suggestion. Instead of:
if($objConnect)
{
echo "Database Connected.<br />";
echo mssql_error();
}
else
{
echo "Database Connect Failed.<br />";
}
Why not:
if($objConnect)
{
echo "Database Connected.<br />";
}
else
{
echo "Database Connect Failed.<br />";
echo mssql_error();
}
Surely you don't need to write an error to the page when the database connects successfully. And telling us the actual error message you receive may better equip us to point you in the direction of a solution. A generic "Database Connect Failed" message that you wrote is not going to give anyone any clue about what actually went wrong. But I bet mssql_error() might!
To connect mssql from my android application, I wrote the following script:
<?php
$myServer= "*****";
$myUser="*****";
$myPass="***";
$db="*****";
echo "hi";
$dbhandle = mssql_connect($myServer, $myUser, $myPass);
echo $dbhandle;
if($dbhandle) {
echo "success";
} else {
echo "failed";
}
$database = mssql_select_db($db);
?>
When I test it in a browser, it's showing only "hi" and it's not executing remaining lines of code. What would be the problem here?
You have not enabled the mssql module in PHP. If you find the error logs, it would say something like "mssql_connect is not a function".
The mssql support built into PHP has been broken for some time. I would suggest following the instructions on this page to help you get it configured.