franknu Posted July 19, 2007 Share Posted July 19, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/60861-empty-query-query-fail/ Share on other sites More sharing options...
sKunKbad Posted July 19, 2007 Share Posted July 19, 2007 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"; Quote Link to comment https://forums.phpfreaks.com/topic/60861-empty-query-query-fail/#findComment-302828 Share on other sites More sharing options...
cooldude832 Posted July 19, 2007 Share Posted July 19, 2007 pull result out of the if statement $result = mysql_query($query) or die(mysql_error()); and add the error tag then you can remove that if because it will die if it errors anyway Quote Link to comment https://forums.phpfreaks.com/topic/60861-empty-query-query-fail/#findComment-302837 Share on other sites More sharing options...
franknu Posted July 19, 2007 Author Share Posted July 19, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/60861-empty-query-query-fail/#findComment-302868 Share on other sites More sharing options...
franknu Posted July 19, 2007 Author Share Posted July 19, 2007 please help, i think that it might be because some of the if statements i have there Quote Link to comment https://forums.phpfreaks.com/topic/60861-empty-query-query-fail/#findComment-302881 Share on other sites More sharing options...
cooldude832 Posted July 19, 2007 Share Posted July 19, 2007 I don't know how you can get an error query was empty i didn't know thats a mysql_error() Quote Link to comment https://forums.phpfreaks.com/topic/60861-empty-query-query-fail/#findComment-302884 Share on other sites More sharing options...
franknu Posted July 19, 2007 Author Share Posted July 19, 2007 oh yes it is, i have gotten in the pass, i just dont remenber what i did to fix it. Quote Link to comment https://forums.phpfreaks.com/topic/60861-empty-query-query-fail/#findComment-302886 Share on other sites More sharing options...
AndyB Posted July 20, 2007 Share Posted July 20, 2007 Take a closer look at your conditional logic and you'll see why the query is empty. Quote Link to comment https://forums.phpfreaks.com/topic/60861-empty-query-query-fail/#findComment-302998 Share on other sites More sharing options...
sKunKbad Posted July 20, 2007 Share Posted July 20, 2007 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/60861-empty-query-query-fail/#findComment-303133 Share on other sites More sharing options...
franknu Posted July 20, 2007 Author Share Posted July 20, 2007 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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/60861-empty-query-query-fail/#findComment-303439 Share on other sites More sharing options...
sKunKbad Posted July 20, 2007 Share Posted July 20, 2007 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; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/60861-empty-query-query-fail/#findComment-303464 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.