Worqy Posted April 25, 2010 Share Posted April 25, 2010 I'm trying to build a system that checks for errors. Now to connect to MySQL i use this code: // Connect to server and select databse. $connect = mysql_connect("$host", "$username", "$password")or die("cannot connect"); $selectdb = mysql_select_db("$database") or die(mysql_error()); And for error checking i used this code: if(!$connect) { echo "Error - Connect"; } elseif(!$selectdb) { echo "Error - Select Database"; } But if I changed my connection code to this: // Connect to server and select databse. $connect = mysql_connect("$host", "$username", "$password")or die("cannot connect"); $selectdb = mysql_select_db("sdfdsfdsfdsf") or die(mysql_error()); I just print out the mysql_error() not the "Error - Select Database" Why not? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2010 Share Posted April 25, 2010 Where were you putting the conditional if(!connect) statement? Quote Link to comment Share on other sites More sharing options...
Worqy Posted April 25, 2010 Author Share Posted April 25, 2010 Where were you putting the conditional if(!connect) statement? immediately after the connecting and database selecting part. // Connect to server and select databse. $connect = mysql_connect("$host", "$username", "$password")or die("cannot connect"); $selectdb = mysql_select_db("$database") or die(mysql_error()); if(!$connect) { echo "Error - Connect"; } elseif(!$selectdb) { echo "Error - Select database"; } Quote Link to comment Share on other sites More sharing options...
cags Posted April 25, 2010 Share Posted April 25, 2010 The die statement causes the script to stop executing. If "sdfdsfdsfdsf" isn't a valid database then mysql_select_db will fail, if it fails the or statement will come into affect and thus the application will die outputting only mysql_error. You either need to remove the or die() statement and just have the if/else block, or remove the if/else block. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2010 Share Posted April 25, 2010 Ah, I see. The problem is that if there's been an error, the script has already been killed by the or die() statements, and it never reaches the conditional. Quote Link to comment Share on other sites More sharing options...
Worqy Posted April 25, 2010 Author Share Posted April 25, 2010 Ok. But it does not still work. Its maby the best that I show the hole code. This is a login script. <?php /* checklogin.php */ Session_start(); include 'config.php'; // Settings date_default_timezone_set('Europe/Helsinki'); // Connect to server and select databse. $connect = mysql_connect("$host", "$username", "$password"); $selectdb = mysql_select_db("546"); if(!$selectdb) { $_SESSION['document'] = "checklogin.php"; $_SESSION['details'] = "Error when connecting"; $_SESSION['date/time'] = $datetime=date("d/m/y h:i:s"); // Date & Time header('Location:index.php'); } elseif(!$connect) { $_SESSION['document'] = "checklogin.php"; $_SESSION['details'] = "Error when selecting database"; $_SESSION['date/time'] = $datetime=date("d/m/y h:i:s"); // Date & Time header('Location:index.php'); } // Username & Password are taken from login form ('login.php') $myusername=$_POST['username']; $mypassword=$_POST['password']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); //Select from MySQL table $sql="SELECT * FROM members WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql is counting number of rows $count=mysql_num_rows($result); // If result matched '$myusername' and '$mypassword', table row count is 1 if($count==1){ // Makes $_Session['Login'] to 'true' $_SESSION['login'] = true; //Heads for 'index.php' header('Location:index.php'); // Settings $_SESSION['user'] = $myusername; } else { header('Location:login.php?action=login_failed'); } ?> Now when I run the script (with a non exicting database) I end up here: www.mywebsite.com/login.php?action=login_failed (By a header at line 62) Thats not what I wanted... Quote Link to comment Share on other sites More sharing options...
cags Posted April 25, 2010 Share Posted April 25, 2010 When you use the header function, the script will continue running afterwards. Thus when you use a 'Location' redirect with the header function it is important that you call exit directly afterwards. <?php /* checklogin.php */ Session_start(); include 'config.php'; // Settings date_default_timezone_set('Europe/Helsinki'); // Connect to server and select databse. $connect = mysql_connect("$host", "$username", "$password"); $selectdb = mysql_select_db("546"); if(!$selectdb) { $_SESSION['document'] = "checklogin.php"; $_SESSION['details'] = "Error when connecting"; $_SESSION['date/time'] = $datetime=date("d/m/y h:i:s"); // Date & Time header('Location:index.php'); exit; } elseif(!$connect) { $_SESSION['document'] = "checklogin.php"; $_SESSION['details'] = "Error when selecting database"; $_SESSION['date/time'] = $datetime=date("d/m/y h:i:s"); // Date & Time header('Location:index.php'); exit; } // Username & Password are taken from login form ('login.php') $myusername=$_POST['username']; $mypassword=$_POST['password']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); //Select from MySQL table $sql="SELECT * FROM members WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql is counting number of rows $count=mysql_num_rows($result); // If result matched '$myusername' and '$mypassword', table row count is 1 if($count==1){ // Makes $_Session['Login'] to 'true' $_SESSION['login'] = true; // Settings $_SESSION['user'] = $myusername; //Heads for 'index.php' header('Location:index.php'); exit; } else { header('Location:login.php?action=login_failed'); exit; } ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2010 Share Posted April 25, 2010 I'm a little confused as to what your goal is here. How do you want the script to behave if the db connect fails? What about if the connect succeeds, but the select fails? Shold it terminate and display an error in both cases, or something different? Quote Link to comment Share on other sites More sharing options...
Worqy Posted April 25, 2010 Author Share Posted April 25, 2010 Thank you. It worked with exit; I'm making a error log, a "messagebox" filled with all errors. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2010 Share Posted April 25, 2010 Not to be snarky, but wouldn't it be easier to enable logging, and use trigger_error()? Quote Link to comment Share on other sites More sharing options...
Worqy Posted April 26, 2010 Author Share Posted April 26, 2010 Hmm.. maby. Haven't every hear of that, so for me no. But made later on I will change the code. Now I come up with another error. It really hasn't anything to do with my last error, but I post it here however. Here is the code: // Connect to MySQL etc... <table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td width="5%" align="center" bgcolor="#E6E6E6"><strong>ID</strong></td> <td width="15%" align="center" bgcolor="#E6E6E6"><strong>From</strong></td> <td width="33%" align="center" bgcolor="#E6E6E6"><strong>Subject</strong></td> <td width="55%" align="center" bgcolor="#E6E6E6"><strong>Message</strong></td> <td width="30%" align="center" bgcolor="#E6E6E6"><strong>Time</strong></td> </tr> <?php //Select from MySQL table $sql="SELECT * FROM mail_inbox WHERE to='$user'"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)) { // Start looping table row ?> <tr> <td align="center" bgcolor="#FFFFFF"><? echo $rows['id']; ?></td> <td align="center" bgcolor="#FFFFFF"><? echo $rows['from']; ?></td> <td align="center" bgcolor="#FFFFFF"><? echo $rows['subject']; ?></td> <td align="center" bgcolor="#FFFFFF"><? echo $rows['message']; ?></td> <td align="center" bgcolor="#FFFFFF"><? echo $rows['time']; ?></td> </tr> <?php // Exit looping } // Close connection mysql_close(); ?> Now this part is the problem: $sql="SELECT * FROM mail_inbox WHERE to='$user'"; When I run that I get a error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /dir..../getinbox.php on line 68 When I change it to: $sql="SELECT * FROM mail_inbox"; I don' get any error. So why does it not work? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 26, 2010 Share Posted April 26, 2010 It means your query is failing. I don't see anywhere in the script where the $user variable is being assigned a value. Echo the query out to the browser to make sure it contains the values you'd expect it to contain. $sql="SELECT * FROM mail_inbox WHERE to='$user'"; $result=mysql_query($sql); echo "QUERY: " . $sql; // ADD THE ECHO STATEMENT FOR DEBUGGING Quote Link to comment Share on other sites More sharing options...
Worqy Posted April 26, 2010 Author Share Posted April 26, 2010 Now when I echo the query I get this output: QUERY: SELECT * FROM mail_inbox WHERE to='Admin' Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a7838708/public_html/getinbox.php on line 69 And line 69 is: while($rows=mysql_fetch_array($result)) Quote Link to comment Share on other sites More sharing options...
Worqy Posted April 26, 2010 Author Share Posted April 26, 2010 Error fixed. Solution: The old query was: SELECT * FROM mail_inbox WHERE to='Admin' Now when I changed some names in MySQL, I changed my query to: SELECT * FROM mail_inbox WHERE receiver='Admin' And then everything works. Resultate: You can't have the word "to" in your query ? Quote Link to comment Share on other sites More sharing options...
cags Posted April 26, 2010 Share Posted April 26, 2010 There are a certain number of reserved words that you cannot use... http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html Quote Link to comment 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.