Jump to content

Recommended Posts

ok i am doing a user authentication but my problem is that when i type in user name and password it is

 

saying

 

Query failed

 

Query was empty

 

here is my code

 


<?
$host = "localhost";
$username = "tdsfsdf";
$password = "abdfsdf3";
$database = "tdsfsdf";

$conn = mysql_connect($host, $username, $password)
or die ('this that ' .mysql_error());

$db = mysql_select_db($database, $conn) or die('Could not connect to database. ' .mysql_error());



$headline = addslashes ($_POST['headline']);
$body = addslashes($_POST['body']);
$writter = addslashes($_POST['writer']);
$date= addslashes($_POST['date']);
$picture = addslashes($_POST['$picture']);
$user = addslashes($_POST['$user']);
$password= addslashes($_POST['$password']);

if ( isset($_POST['user']) && isset($_POST['password']) ) {


$query = "SELECT * FROM news WHERE user='$user' AND password ='$password' ";


}

  if ($result = mysql_query($query)) 
{
    if (mysql_num_rows($result))
{
      $row = mysql_fetch_assoc($result);

      $headline= $row['headline'];
      $body = $row['body'];
      $date =  $row['date'];
      $picture = $row['picture'];
      $writer =$row['writer'];
      $song1 = $row['song1'];
      $song2 =$row['song2'];
      $song3 = $row['song3'];
      $song4= $row['song4'];
      $song5 =  $row['song5'];
      $picture2 = $row['picture2'];
      $eventhead = $row['eventhead'];
      $eventpic=$row['eventpic'];
      $eventbody= $row['eventbody'];
    
    } 
else 
{
      echo "<p><b>username and/or password not found. Try
again?

</b></p>";
exit;
    }
  } 
else 
{
    echo "Query failed<br />$query<br />". mysql_error();
exit;
  }



?>

 

any idea please

Link to comment
https://forums.phpfreaks.com/topic/60861-empty-query-query-fail/
Share on other sites

This is how I connect to my godaddy database:

$x = "123.mygodaddy_database.net" ;
$y = "my_database" ;
$z = "STRONG_password" ;
$xyz = @mysql_connect($x,$y,$z);
if (!$xyz) {
    die('Could not connect to database');
}
$db = @mysql_select_db ($y,$xyz);
if (!$db) {
    die ('Database cannot be found');
}

 

and an actual query:

$Query = mysql_query("SELECT * FROM my_database");
$Row = mysql_fetch_array($Query);
extract($Row);
echo "$field1\n";

this is whaT I GET NOW  Query was empty

 

HERE ARE THE CHANGES I MADE TO THE CODE

 


if ( isset($_POST['user']) && isset($_POST['password']) ) 
{


$query = "SELECT * FROM news WHERE user='$user' AND password ='$password' ";


}

$result = mysql_query($query) or die(mysql_error());

    if (mysql_num_rows($result))
{
      $row = mysql_fetch_assoc($result);

      $headline= $row['headline'];
      $body = $row['body'];
      $date =  $row['date'];
      $picture = $row['picture'];
      $writer =$row['writer'];
      $song1 = $row['song1'];
      $song2 =$row['song2'];
      $song3 = $row['song3'];
      $song4= $row['song4'];
      $song5 =  $row['song5'];
      $picture2 = $row['picture2'];
      $eventhead = $row['eventhead'];
      $eventpic=$row['eventpic'];
      $eventbody= $row['eventbody'];
    
    } 
else 
{
      echo "<p><b>username and/or password not found. Try
again?

</b></p>";
exit;
    }

   




?>

 

PLEASE HELP

For your query, have you actually set the variables $user and $password?

 

How about this:

 

<?php
if ( isset($_POST['user']) && isset($_POST['password']) ){
     $user = $_POST['user'];
     $password = $_POST['password'];
     $Query = mysql_query("SELECT * FROM news WHERE user='$user' AND password ='$password'");
     if (mysql_num_rows($Query) != 0){
          $Row = mysql_fetch_array($Query);
          extract($Row);
          /*extract takes the mysql array "$Row", and makes each array value a variable, same as what you were trying to do with:
           $headline= $row['headline'];
           $body = $row['body'];
           $date =  $row['date'];
           $picture = $row['picture'];
           $writer =$row['writer'];
           $song1 = $row['song1'];
           $song2 =$row['song2'];
           $song3 = $row['song3'];
           $song4= $row['song4'];
           $song5 =  $row['song5'];
           $picture2 = $row['picture2'];
           $eventhead = $row['eventhead'];
           $eventpic=$row['eventpic'];
           $eventbody= $row['eventbody'];
           */
          //now just access your variables and do what you want with them like this:
          echo "$headline";
          echo "$body";
          echo "$date";
          echo "$picture";
          //etc...
     }
}else{
     echo "<p><b>username and/or password not found. Try again?</b></p>";
     exit;
}
?>

I MADE SOME CHANGES AND NOW THE SCRIPT DOESNT DO ANYTHING, IT JUST TAKE THE USER RIGHT INTO THE CONTROL PANER WETHER THE USER AND PASSWORD MATCH OR NOT

 

HER IS MY NEW CODE

 


if ( isset($_POST['user']) && isset($_POST['password']) )
{
     $user = $_POST['user'];
     $password = $_POST['password'];
     $Query = mysql_query("SELECT * FROM news WHERE user='$user' AND password ='$password'");
     if (mysql_num_rows($Query) != 0)
 {
          $Row = mysql_fetch_array($Query);
          extract($Row);

      $headline= $row['headline'];
      $body = $row['body'];
      $date =  $row['date'];
      $picture = $row['picture'];
      $writer =$row['writer'];
      $song1 = $row['song1'];
      $song2 =$row['song2'];
      $song3 = $row['song3'];
      $song4= $row['song4'];
      $song5 =  $row['song5'];
      $picture2 = $row['picture2'];
      $eventhead = $row['eventhead'];
      $eventpic=$row['eventpic'];
      $eventbody= $row['eventbody'];
    
   } 
else 
{
      echo "<p><b>username and/or password not found. Try
again?

</b></p>";
exit;
    }
}
   




?>

Try this real quick and see what happens:

 

<?php
if ( isset($_POST['user']) && isset($_POST['password']) ){
     $user = $_POST['user'];
     $password = $_POST['password'];
     $Query = mysql_query("SELECT * FROM news WHERE user='$user' AND password ='$password'");
     if (mysql_num_rows($Query) != 0){
          $Row = mysql_fetch_array($Query);
          extract($Row);
           if (($password == $_POST['password']) && ($user == $_POST['user'])){          
          echo "$headline";
          echo "$body";
          echo "$date";
          echo "$picture";
          }else{
                    echo "<p><b>username and/or password not found. Try again?</b></p>";
                    exit;
          }
     }
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.