Dear friends,
I am facining an other problem to in cloude a connetion file in PHP form.my code is below

<?php
   include"conn.php";

if(isset($_POST['login'])){
$uname=$_post['username'];
$uname=$_post['password'];

$sql="SELECT * FROM login1 WHERE L1='$uname'";
$result=$conn->query("$sql");

if($result){
          echo("welcome to new page");

}                      else {
                               echo ("login problem");

}
       }

?>

can not continue connectoin .... and i cannt login ,please suggest me....

Recommended Answers

All 6 Replies

My conn.php file is here..

<?php
$username="root";
$password="";
$server="127.0.0.1";
$db="login1";

$conn=new mysqli($server,$username,$password,$db);
if(!$conn){

echo "Connection not established";

}

?>

login1 is the database name or the table name, or both?

Instead of:

if(!$conn){
    echo "Connection not established";
}

Do:

if ($conn->connect_errno) {
    printf("Connect failed: %s\n", $conn->connect_error);
    exit();
}

As suggested in the documentation. And return the error here.

ok But this code does give result ....any mistake here?

$sql="SELECT * FROM login1 WHERE L1='$uname'";
$result=$conn->query("$sql");
if($result){
          echo("welcome to new page");
}                      else {
                               echo ("login problem");
}

It seems fine, the only problem I can see is related to the space between the include and the string:

include"conn.php";

It should be:

include "conn.php";

But at query level use $result->error:

if($result){
    echo "welcome to new page";
}

else{
    echo $result->error;
}

It will display the error related to the query, if any.

  if($result->num_rows==true){
                                   echo("welcome");

                       }
                       else {

                       echo ("invalid username"."<br>");

                       }

I have
solved with that code...its ok?

Mysqli num_rows returns integers not booleans (true|false) so if you want to check how many rows you're returning change the if statement to:

if($result->num_rows > 0){

But, if the query fails for an error this statement will not prevent a PHP notice:

PHP Notice:  Trying to get property of non-object ...

It happens because $result will be false, so a more complete check is to verify if $result is not false:

if($result && $result->num_rows > 0){

Note that mysqli_query on success can return an object or simply boolean true, the documentation explains the cases:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

So for SELECT queries you can almost always rely on num_rows but if you perform an INSERT, DELETE or UPDATE then you cannot use num_rows, this for example:

$result = $mysqli->query("create temporary table mytest engine = memory as select 1+1 as 'sum' from dual");

Is a perfect valid query, it's the equivalent of an INSERT ... SELECT, and it will create a temporary table, but it will not return an object, it will return only boolean, so this check:

if($result && $result->num_rows > 0)

Will fail generating the same PHP notice as above, in this case use only:

if($result)

Edit: adding info
I forgot to add an exception: a select query can return only boolean true if you use the statement SELECT ... INTO to assign variables or write to a file, as example:

$result = $mysqli->query("select 1+1 as 'sum' from dual into @sum");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.