Jump to content

confusing mysql errors..

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
ok guys, I cant find any reason for this code not to work.. I am trying to make an error DB, so when a user gets an error, they it will report it and add it to the DB. Here is the code.
Code of page making the error, the error is made on purpose to test the error.php:
$connect = mysql_connect("$sqlhost", "$sqlusername", "$sqlpassword");

if(!$connect){

  $error = (mysql_error());

  $report = "yes";

  Include("error.php");

  die();

}
Error.php:
        include("sql.php");

        $connect = mysql_connect("$sqllhost", "$sqlusername", "$sqlpassword");

        if(!$connect){

           die(mysql_error());

        }

        $select_db = mysql_select_db("virtualeuphoria", $connect);

        if(!$select_db){

            die(mysql_error());

        }

        $insert = mysql_query("INSERT INTO errors (date, time, error) VALUES ('$date', '$time', '$error')");

        if(!$insert){

            die("There's little problem: ".mysql_error());

        }
Here is the error

Quote

There's little problem: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'root'@'localhost' (using password: YES)')' at line 1

BUT! When I change the veriable error to bla, I dont get an error! I gotta change the variable, the value of it, and the name of the column in the table, but it works.

Can I not use a variable like "$error"? I also tried "$problem" and it didnt work.

#2
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
wait, could it have something to do with $error being an direct quote of the mysql error its reporting?

#3
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Your variable, $error, has single quotes in it ('). MySQL will not let you insert these unless you escape them. Use the addslashes function:

$error = addslashes($error);
Example
<?php
$str = "Is your name O'reilly?";

// Outputs: Is your name O\'reilly?
echo addslashes($str);
?>
Source for example and more information: PHP: addslashes - Manual

#4
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
Oh wow, I guess that was easier than I thought! Thanks jordan!

#5
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
Jordan, what about this one? Im getting an error about my syntax, but I dont see whats wrong.
                  $select_db = mysql_select_db("virtualeuphoria", $connect);

                  if(!$select_db){

                    die(mysql_error());

                  }

                  $insert = mysql_query("INSERT INTO pm (from, to, status, importance, date, time, subject, message) VALUES ('Admin', 'Admin', 'unread', 'high', 'today', 'now', 'test', 'testing')");

                  if(!$insert){

                    die("There's little problem: ".mysql_error());

                  }

Quote

There's little problem: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from, to, status, importance, date, time, subject, message) VALUES ('Admin', 'Ad' at line 1


#6
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
Has anyone looked at this yet :-\

#7
Guest_Jordan_*

Guest_Jordan_*
  • Guests
from is a keyword in SQL, don't use it as the name of a column.

#8
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
OHHHHH! wow, what other key words should I not use?

#9
Guest_Jordan_*

Guest_Jordan_*
  • Guests
'IN', 'WHERE', 'FROM', 'SELECT', 'INSERT', 'UPDATE', 'DELETE', 'JOIN' - that is all I could think of off the top of my head. There are probably more.

#10
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts

Jordan said:

'IN', 'WHERE', 'FROM', 'SELECT', 'INSERT', 'UPDATE', 'DELETE', 'JOIN' - that is all I could think of off the top of my head. There are probably more.

'TO'

#11
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
As mentioned twice already, placing ticks around column names will prevent this error from happening.

#12
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts

John said:

As mentioned twice already, placing ticks around column names will prevent this error from happening.

Thats funny, I tried it once, and it didn't work, I'll try it a second time