Jump to content

Error check?


Worqy

Recommended Posts

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?

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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;
}
?>

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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))

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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.