...

View Full Version : error reporting being called even on successfull query.



jussa
06-01-2007, 01:01 AM
hey everyone,

i have this function in my class that i am developing. its a mysql wrapper class. (i know there are lots of these around, but i generally like to write my own code, and since oop in php is new to me, there are lots of examples to learn from :P)...

this is the function in question.



function sql_query($query) {

$this->query = $query;

$this->result = @mysql_query($query, $this->link) or $this->error(mysql_error(), $query);

$count = $this->query_count;
$count++;
$this->query_count = $count;

}


being called like so...


require("includes/config.php");
require("includes/class/class.mysql.php");

$db = new mysql(DB_HOST, DB_USER, DB_PASS, DB_NAME);

$db->sql_query("SELECT * FROM projects");

echo "Queries: " . $db->query_count;

now, the code runs fine, except that it displays the error when runnign the sql_query function, even tho there is no error?

can anyone work out why?

Thanks,
Justin

CFMaBiSmAd
06-01-2007, 01:42 AM
Specifically, what error is it displaying and you probably need to post your error(..) function code as well.

If it is displaying a mysql_error() error, that would indicate that the mysql_query() failed and the "or" caused the $this->error(...) to be evaluated.

You don't seem to be using $this->query, so you probably don't need this line of code - $this->query = $query;

The three lines of code that are incrementing the counter can be replaced with just - $this->query_count++;

jussa
06-01-2007, 02:19 AM
oh, sorry about that. what i mean to say was its calling that error function as in the "or $this->error(mysql_error(), $query);" part, even tho there is no mysql_error(). its just returning an empty error, but displaying it (as that error() function display other hardcoded text as well.)

its runs as tho there is an error, the statement is fine.

thanks for the tip in the $this->query_count++.


Cheers,
Justin

CFMaBiSmAd
06-01-2007, 02:54 AM
I put your function (less the $this->link to save on typing) into a mini made up class and it works. When the query contains an error (the table name did not exist), the error() function was called and when the query was correct, the error() function was not called -

<?php
class mysql {
var $result; // the result set
var $query_count; // just some counter

function sql_query($query) {
$this->result = @mysql_query($query) or $this->error(mysql_error(), $query);
$this->query_count++;

}

function error($error, $query){
echo "called the error function<br />";
echo "Error: $error, Query: $query<br />";
}
}It is highly likely that your query is failing but your error() function is not displaying the actual information. You would need to post your error() function, as has already been requested, to get the quickest solution to your problem. Either it will contain an error or it will point elsewhere to the problem, but until you post it, it is the most likely reason for your incorrect results.

Edit: I just thought of another issue. The mysql_query function call might be failing totally, not just because of a query statement problem. Remove the @ sign to see what you get.

jussa
06-01-2007, 03:09 AM
Hi,

Here is my error function, which is in that same class...



function error($err_msg, $query = "") {

$error_markup = "
<html><head><title>MySQL Error!</title>
<link rel=\"stylesheet\" href=\"\" /></head>
<body>
<div id=\"error_frame\">
<h1 class=\"h1_err\">MySQL Error!</h1>
<p class=\"p_err\"><strong>Error Message:</strong></p><br />
<p class=\"p_err\">".$err_msg."</p><br /><br />
<p class=\"p_err\"><strong>MySQL Query:</strong></p><br />
<p class=\"p_err\">".$query."</p><br />
</div></body></html>
";

echo $error_markup;

}


i will remove the @ sign and see what i get.

Thanks,

Justin

jussa
06-01-2007, 03:26 AM
when ran without the @, i get a


Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource on line 62

line 62 is


$this->result = mysql_query($query, $this->link) or $this->error(mysql_error(), $query);

here is the part of my class that connects to db (which runs without errors).




class mysql {

var $query_count;
var $query;
var $result;
var $link;

function mysql($db_host, $db_user, $db_pass, $db_name) {

$this->query_count = 0;
$this->link = 0;

$this->connect($db_host, $db_user, $db_pass, $db_name);

}

function connect($db_host, $db_user, $db_pass, $db_name) {

$this->link = @mysql_connect($db_host, $db_user, $db_pass) or $this->error(mysql_error());

$this->select_db($db_name);

}

function select_db($db_name) {

$this->link = @mysql_select_db($db_name) or $this->error(mysql_error());

}
//rest of class here, which im not using yet, including the error() function posted above...

}



thanks again for your help,

Justin

CFMaBiSmAd
06-01-2007, 04:08 AM
The most likely problem is the following line in the select_db() function -

$this->link = @mysql_select_db($db_name) or $this->error(mysql_error());the mysql_select_db() returns a bool true/false. However, you are putting this into the $this->link, which is overwriting any actual resource link.

jussa
06-01-2007, 04:55 AM
The most likely problem is the following line in the select_db() function -

$this->link = @mysql_select_db($db_name) or $this->error(mysql_error());the mysql_select_db() returns a bool true/false. However, you are putting this into the $this->link, which is overwriting any actual resource link.



Hi, thanks for you help... never used link identifiers before, but now im guessing you only set then on the connection, not actually setting the db.

that worked like a treat!

thanks heaps,

Justin

CFMaBiSmAd
06-01-2007, 05:04 AM
but now im guessing you only set then on the connection, not actually setting the db.No guessing is required -
http://www.php.net/manual/en/function.mysql-connect.php
http://www.php.net/manual/en/function.mysql-select-db.php

jussa
06-01-2007, 05:41 AM
lol.

thanks, it was an "educated" guess. hehe

Thanks again, :>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum