Regards,
I am new to PHP. Here is a small login script that i wrote for a login page. It checks username and password from the database.
<?php
$con = mysqli_connect("localhost","root","saw","gehriroute");
if(mysqli_connect_errno())
{
echo "could not connect to the database";
exit;
}
else
{
echo "database runing <br>";
}
$uname = trim($_POST['uname']);
$password = trim($_POST['password']);
echo "username and password are $uname and $password <br>";
$query = "select count(*) from login where username = '".$uname."' and password = '".$password."' ";
$result = mysqli_query($query,$con);
if(!$result)
{
echo "query failed";
}
$row = mysqli_fetch_row($result);
$count = $row[0];
if($count > 0 )
{
echo "yes your are logged in";
}
else
{
echo "wrong username or password";
}
?>
It is not having issues connecting to the database but it is not responding either. I bet the script dosnt pass the mysql_error() function. I tried to debug it using mysql_error()
function, it returned nothing. What are the other things i can use to debug and correct the error ?
Output:
database runing username and password are ateev and saw query failed
Why not first run the SQL first via either the command line or through the workbench?
Then this will enable you to make sure that the query is valid and you are getting the expected results.
EDIT
Also if you read mysqli_query manual page the line
$result = mysqli_query($query,$con);
is incorrect and should be
$result = mysqli_query($con, $query);
Try to add after your query.This will check/determine wether your query is executed succesfully or returned an error
$result = mysqli_query($query,$con);
if(!$result){
// means your query failed
// error checking...
printf("Error: %s
", mysqli_error($con));
}
else {
echo 'query executed.';
}
Most likely the php script isn't being called. try setting your .php file to just echo 'x';
and see if x
gets outputted. if not, then your calling the wrong script and need to specify the right filename (eg: myScript.php).
heres a working example of what your trying to do. just replace SELECT Code, Name FROM Country ORDER BY Name
with your query.
happy coding :)
Could try this:
if (!mysqli_query($link, $query)) { //if the query fails find the error
echo mysqli_error($link);
}
Echo $query ; exit
copy Query and run it in to Your Query Executer !
you will get the SQL error if any
other wise Query will run successfully.
Let's Try this code. It works now.
<?php
$con = mysqli_connect("localhost","root","saw","gehriroute");
if(mysqli_connect_errno())
{
echo "could not connect to the database";
exit;
}
else
{
echo "database runing <br>";
}
$uname = trim($_POST['uname']);
$password = trim($_POST['password']);
echo "username and password are $uname and $password <br>";
$query = "select count(*) from login where username = '".$uname."' and password = '".$password."' ";
$result = mysqli_query($con,$query); // Connection link should first
//echo "Error: ".mysqli_info();
$count = mysqli_num_rows($result); // Get Count row like this..
if($count > 0 )
{
echo "yes your are logged in";
}
else
{
echo "wrong username or password";
}
?>