[PHP]Connecting to DB failure

Master Summoner
Joined
Oct 9, 2008
Messages
572
Reaction score
14
- And I can't see anything that goes wrong. Well, I've been trying for the past 10 minutes to figgure the error out, but I'm not getting anything. Heres my script:

PHP:
<?php
   session_start();
     $my_host = 'localhost';
     $my_user = 'root';
     $my_pass = 'root';
     $my_scheme = 'user_settings';
     $my_table = 'settings';
     $ip = $_SERVER['REMOTE_ADDR'];
     if (!$_GET['width'] && !$_GET['heigth']){
       Header('Location:./settings.php');
     }

     if ($_GET['width'] && $_GET['heigth']){
      $_SESSION['w'] = mysql_real_escape_string($_GET['width']);
      $_SESSION['h'] = mysql_real_escape_string($_GET['heigth']);

       $screen_width = $_SESSION['w'];
       $screen_heigth = $_SESSION['h'];

       $con = mysql_connect($my_host,$my_user,$my_pass) or die(mysql_error());
       mysql_select_db($my_scheme) or die(mysql_error());
       $sql = array("SELECT * FROM $my_scheme
                     WHERE ip = '$ip'",

                     "INSERT INTO $my_table
                     VALUES ('','$ip','$screen_width','$screen_heigth');",

                     "UPDATE $my_table
                     SET screen_width = '$screen_width', screen_heigth = '$screen_heigth'
                     WHERE ip = '$ip'");

       $result[0] = mysql_query($sql[0]);
       $rowcount = mysql_num_rows($result[0]);
       
       if($rowcount == 1) {
         $result[1] = mysql_query($sql[2]) or die(mysql_error());
       }
       else if ($rowcount = 0) {
         $result[2] = mysql_query($sql[1]) or die(mysql_error());
       }
     mysql_close($con);
     }
?>

Any help is very appreciated!

*Updates script, but still not working.
 
Newbie Spellweaver
Joined
Jan 1, 2009
Messages
46
Reaction score
1
Is this working?

PHP:
<?php
    // Start session
    session_start();

    // Database details
    $name_db = "user_settings";
    $user_db = "root";
    $pass_db = "root";
    $host_db = "localhost";
    $connect_db = mysql_connect($host_db, $user_db, $pass_db);

    // Making connection to our database         
    mysql_select_db($name_db, $connect_db) or die(mysql_error());
    
    // Running script
    $ip = $_SERVER['REMOTE_ADDR'];
    
    if (!isset($_GET['width']) && !isset($_GET['heigth']))
    {
        Header('Location:./settings.php');
    } 
    else 
    {
        $_SESSION['w'] = mysql_real_escape_string($_GET['width']);
        $_SESSION['h'] = mysql_real_escape_string($_GET['heigth']);

        $screen_width = $_SESSION['w'];
        $screen_heigth = $_SESSION['h'];

        $sQuery = mysql_query("SELECT 1 FROM settings WHERE ip = '" . $ip . "'") or die(mysql_error());
        $res[0] = mysql_num_rows($sQuery);
         
        if($res[0] == 1) 
        {
            $res[1] = mysql_query("INSERT INTO settings VALUES ('','" . $ip ."','" . $screen_width ."','" .$screen_heigth . "')") or die(mysql_error());
        }
        elseif($res[0] == 0) 
        {
            $res[2] = mysql_query("UPDATE settings SET screen_width = '" . $screen_width . "', screen_heigth = '" .$screen_heigth . "' WHERE ip = '" . $ip . "'") or die(mysql_error());
        }
    }
     
    // Closing mysql connection
    mysql_close($connect_db);
?>
 
Last edited:
Joined
Dec 26, 2006
Messages
1,305
Reaction score
167
PHP:
       $con = mysql_connect($my_host,$my_user,$my_pass) or die(mysql_error());
       mysql_select_db($my_scheme) or die(mysql_error());

should be

PHP:
       $con = mysql_connect($my_host,$my_user,$my_pass) or die(mysql_error());
       mysql_select_db($my_scheme, $con) or die(mysql_error());

second var in the select db = the connection.
 
Master Summoner
Joined
Oct 7, 2008
Messages
580
Reaction score
15
also make sure u enable the Mysql connection in your php.ini
 
Master Summoner
Joined
Oct 9, 2008
Messages
572
Reaction score
14

Well, sort of. Now its telling me "Incorrect integer value: '' for column 'id' at row 1", which I don't get since I made the id row auto increment.



Thank you, but I never put $con after what db I wanted to get data from.

also make sure u enable the Mysql connection in your php.ini

I am not a complete Special person ;-), but thanks anyway.
 
Newbie Spellweaver
Joined
May 31, 2009
Messages
40
Reaction score
0
$_SESSION['h'] = mysql_real_escape_string($_GET['heigth']);

if (!$_GET['width'] && !$_GET['heigth']){

Don't you mean 'height'?
 

Leo

Custom Title Activated
Loyal Member
Joined
Nov 21, 2004
Messages
1,897
Reaction score
19
Since your id column is auto-incrementing then you should have null in your values as the first value in your MySQL queries.

PHP:
$res[1] = mysql_query("INSERT INTO settings VALUES ('','" . $ip ."','" . $screen_width ."','" .$screen_heigth . "')") or die(mysql_error());

Should be

PHP:
$res[1] = mysql_query("INSERT INTO settings VALUES (null,'" . $ip ."','" . $screen_width ."','" .$screen_heigth . "')") or die(mysql_error());

As you're telling MySQL,

"Ok database, don't give a value to the id column"

MySQL responds to you:

"Yeah I know, I'm just going to auto-increment it!"