This question already has answers here:
How do I get PHP errors to display?
(27 answers)
Closed 5 years ago.
<?php
//Get Value
$username = $_POST['user'];
$password = $_POST['pass'];
//Connet To The Server And Select Database
mysqli_connect("192.168.xxx.xxx", "xxx", "xxxxxxxxxxxxx");
mysqli_select_db("xxxxx");
//Query The Database For User
$result = mysqli_query("select * from user where username = '$username' and password = '$password'")
or die("Failed to query database ".mysqli_connect_error());
$row = mysqli_fetch_array($result);
if (empty($username)) {
header('Location: fa.html');
} elseif (empty($password)) {
header('Location: fa.html');
} elseif ($row['username'] == $username && $row['password'] == $password){
header('Location: su.html');
} else{
header('Location: fa.html');
}
?>
I have no experience to code PHP so i have no idea what's wrong is my code.
I have replace "mysql" into "mysqli" but it is still not working correctly.
It's work fine when running "mysql_*" and using my local Window web server.
But when i put it into Linux server it occur error message "Failed to query database ".
Add the fourth parameter here as database name like
mysqli_connect("192.168.xxx.xxx", "xxx", "xxxxxxxxxxxxx", "database_name");
and remove
mysqli_select_db("xxxxx");
A sample way of writing php code would be
$link = mysqli_connect("server_name","username","password","database_name");
if(mysqli_connect_error()) {
die("There was an error connecting to the database");
}
$query = 'yourQuery';
$result=mysqli_query($link,$query);
if ( false==$result ) {
printf("error: %s\n", mysqli_error($link));
}
This will work
Hope it helps
You need to trap for more errors to see where it's failing. Right now all you know is that it's failing at-least by the mysqli_query() line where you do trap for errors.
First you have to ensure that you can actually connect to the database server.
Change:
mysqli_connect("192.168.xxx.xxx", "xxx", "xxxxxxxxxxxxx");
to
if(!mysqli_connect("192.168.xxx.xxx", "xxx", "xxxxxxxxxx"))
{
die("Could not connect");
}
then you should also trap for errors on selecting the database. Change:
mysqli_select_db("xxxxx");
to
if(!mysqli_select_db("xxxx"))
{
die("Could not select database");
}
Edit: Also you really didn't need to blank-out your IP. 192.168.1.* is a class C private address - meaning it is not accessible from outside your local network.
You need to learn more, because these are really just the basics. The reason why your code works locally and doesn't on the remote server is probably caused by your DB access - it obviously isn't the same. You need to change your credentials and your db IP to match your remote server if you want to deploy your code there.
For some reason, the following code inside the query works in my MySQL command console, yet when I try to run it as a Query in PHP, something keeps going wrong and I'm not sure what. Here is the code I've done so far.
//2. Perform database query
$query = "SELECT skills.element_id, content_model_reference.element_id, element_name FROM skills, content_model_reference WHERE (skills.element_id = content_model_reference.element_id)";
$result = mysql_query($query);
//Tests if there was a query error
if(!$result){
die("Database query failed.");
}
Is there something preventing the code that worked in MySQL (The line with SELECT) from working, or is my syntax somehow wrong?
EDIT: So it's saying I didn't select a database. Yet I thought I had. Here is the code above it:
//1. Create a database connection
$dbhost = "host"; //Host: Can be either an IP address, or a domain (like google.com).
$dbuser = "user";//User: The user that is connecting to the database.
$dbpass = "pass";//Password: This is the password that the user is using.
$dbname = "db";//Name: This is the name of the database.
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);//The value, 'handle,' is the connection.
//Test if connection occurred. Die ends the program/php, and in this case, also prints a message
if(mysqli_connect_errno()){
die("Database connection failed: ".
mysqli_connect_error().
" (". mysqli_connect_errno() . ")"
);
}
Like I said, the error message I am getting is pertaining only to the query, the server is fine with my database connection.
You're using mysqli_* for the connection, but you're using mysql_* for the QUERY... don't think you can do that, has to be one or the other (MYSQLI_ preffered). Also the query should be:
$result = mysqli_query($connection,$query);
Can anyone help me on this? I am a php beginner. Error I am getting is:
Warning: "mysqli_query() [function.mysqli-query]: Empty query in C:\xampp\htdocs\option1\db_def.php on line 49"
i.e where if statement starts.
<?php
include ('config.php');
$con= mysqli_connect("localhost","root","") or die ("could not connect to mysql");
if(isset($_POST['submit']))
{
$date= $_POST["date"];
$sno= $_POST["sno"];
// and so on
$sql= mysql_query("INSERT INTO data(date,sno,block,name,so_wo_do,plot_size,hno,hno1,street,mohalla,ws_id,sid,ws_conn,s_conn,dispo_conn,elec_acc,residential_commercial,trade_licence,hno2,street2,mohalla2,contact,email,year_construction,structure,nature_unit,usage,basement,gnd_floor,first_floor,sec_floor,third_floor,any_floor,total,area_sft,oid_no,remarks) VALUES('$_POST[date]','$_POST[sno]','$_POST[block]','$_POST[name]','$_POST[so_wo_do]','$_POST[plot_size]','$_POST[hno]','$_POST[hno1]','$_POST[street]','$_POST[mohalla]','$_POST[ws_id]','$_POST[sid]','$_POST[ws_conn]','$_POST[s_conn]','$_POST[dispo_conn]','$_POST[elec_acc]','$_POST[residential_commercial]','$_POST[trade_licence]','$_POST[hno2]','$_POST[street2]','$_POST[mohalla2]','$_POST[contact]','$_POST[email]','$_POST[year_construction]','$_POST[structure]','$_POST[nature_unit]','$_POST[usage]','$_POST[basement]','$_POST[gnd_floor]','$_POST[first_floor]','$_POST[sec_floor]','$_POST[third_floor]','$_POST[any_floor]','$_POST[total]','$_POST[area_sft]','$_POST[oid_no]','$_POST[remarks]')");
if(!mysqli_query($con,$sql))
{
echo("Member Registered!");
}
else
{
echo("Input data is fail");
}
}
mysqli_close($con);
?>
you are using mysqli_* functions and also mixing it with mysql_query
here $sql= mysql_query(----) your are using again $Sql in mysqli_query(); that is incorrect.
please change it to $sql = "INSERT INTO data( // al col names) VALUES(//all vals)"
and use $sql it inside mysqli_query($con,$sql).
Remove mysql_query()
You should try this. Can you please define why you include('config.php') file? and you are making new connection also ..
<?php
//include ('config.php');
$con= mysqli_connect("localhost","root","") or die ("could not connect to mysql");
if(isset($_POST['submit']))
{
$date= $_POST["date"];
$sno= $_POST["sno"];
// and so on
$sql= "INSERT INTO data(date,sno,block,name,so_wo_do,plot_size,hno,hno1,street,mohalla,ws_id,sid,ws_conn,s_conn,dispo_conn,elec_acc,residential_commercial,trade_licence,hno2,street2,mohalla2,contact,email,year_construction,structure,nature_unit,usage,basement,gnd_floor,first_floor,sec_floor,third_floor,any_floor,total,area_sft,oid_no,remarks) VALUES('$_POST[date]','$_POST[sno]','$_POST[block]','$_POST[name]','$_POST[so_wo_do]','$_POST[plot_size]','$_POST[hno]','$_POST[hno1]','$_POST[street]','$_POST[mohalla]','$_POST[ws_id]','$_POST[sid]','$_POST[ws_conn]','$_POST[s_conn]','$_POST[dispo_conn]','$_POST[elec_acc]','$_POST[residential_commercial]','$_POST[trade_licence]','$_POST[hno2]','$_POST[street2]','$_POST[mohalla2]','$_POST[contact]','$_POST[email]','$_POST[year_construction]','$_POST[structure]','$_POST[nature_unit]','$_POST[usage]','$_POST[basement]','$_POST[gnd_floor]','$_POST[first_floor]','$_POST[sec_floor]','$_POST[third_floor]','$_POST[any_floor]','$_POST[total]','$_POST[area_sft]','$_POST[oid_no]','$_POST[remarks]')";
$query=mysqli_query($con,$sql);
if($query)
echo("Member Registered!");
}
else
{
echo("Input data is fail");
}
}
mysqli_close($con);
?>
please use only mysqli query
$sql = "INSERT INTO data(date,sno,block,name,so_wo_do,plot_size,hno,hno1,street,mohalla,ws_id,sid,ws_conn,s_conn,dispo_conn,elec_acc,residential_commercial,trade_licence,hno2,street2,mohalla2,contact,email,year_construction,structure,nature_unit,usage,basement,gnd_floor,first_floor,sec_floor,third_floor,any_floor,total,area_sft,oid_no,remarks) VALUES('$_POST[date]','$_POST[sno]','$_POST[block]','$_POST[name]','$_POST[so_wo_do]','$_POST[plot_size]','$_POST[hno]','$_POST[hno1]','$_POST[street]','$_POST[mohalla]','$_POST[ws_id]','$_POST[sid]','$_POST[ws_conn]','$_POST[s_conn]','$_POST[dispo_conn]','$_POST[elec_acc]','$_POST[residential_commercial]','$_POST[trade_licence]','$_POST[hno2]','$_POST[street2]','$_POST[mohalla2]','$_POST[contact]','$_POST[email]','$_POST[year_construction]','$_POST[structure]','$_POST[nature_unit]','$_POST[usage]','$_POST[basement]','$_POST[gnd_floor]','$_POST[first_floor]','$_POST[sec_floor]','$_POST[third_floor]','$_POST[any_floor]','$_POST[total]','$_POST[area_sft]','$_POST[oid_no]','$_POST[remarks]')";
mysqli_query($con,$sql);
I had the same problem, the problem was the range, I had to move the $query = ($con, "SELECT blah blah"); a couple of lines.
Never had this before in the old way of coding, this is msqli vs msql.
You just need to relearn everything you have learned if you only know how to code the old way.
This reply is for the topic title only, my post is not a reaction or solving answer to this topic.
It might help somebody else if ;)
I think it is a range or global problem, because it is returning an empty query.
But without more information on db_def.php on line 49 it is hard to say.
include ('config.php');
$con= mysqli_connect("localhost","root","") or die ("could not connect to mysql");
You need to create a global variable in "db_def.php" that declaires $con or create one every time you connect or query in 'db_def.php'