mercredi 29 juin 2016

Mysql password expired. Can't connect

I just wiped my Mac and did a fresh install of El Capitan. I'm struggling to connect to Mysql now. Having gone through a web server setup process, I've created a simple PHP test file:

<?php
  $conn = new mysqli("127.0.0.1", "root", "xxxxxxxx");
  if ($conn->connect_error) echo "Connection failed: " . $conn->connect_error; 
  else echo "Connected successfully";
  phpinfo();
?>

When I run it, I get this error:

Warning: mysqli::mysqli(): (HY000/1862): Your password has expired. To log in you must change it using a client that supports expired passwords. in /Users/rich/Documents/DESIGN/test/index.php on line 3
Connection failed: Your password has expired. To log in you must change it using a client that supports expired passwords.

I've never seen that response from a connection before. How do I fix it if I can't connect?

EDIT

In terminal I entered the command:

mysql -u root -p

This asked me for my password (current one) which I put in. I now have access to mysql commands but anything I try results in this error:

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

How do I reset the password using ALTER USER?

How do I remove duplicate records

Query:

Select table_c.id_number, table_c.name, table_s.site_name,table_co.Contract_name 
FROM table_c , table_s, table_m, table_o, table_a, table_con 
  WHERE 
   table_s.objid = table_c.sobjid
   AND table_m.cobjid (+) = table_c.objid 
   AND table_o.objid (+) = table_m.olobjid   
   AND table_a.objid (+) = table_o.aobjid 
   AND table_co.objid (+) = table_a.conobjid;

Here I have 6 tables. table_c and table_s have one 2 one relationship. It is possible that for 1 table_o record, we can have 2 table_c records or may be no record.Since I need to take table_co values in SELECT along with table_c and table_s tables, I used left out join on all tables table_c->table_m->table_o->table_a->table_co.

Now on running this query it gives me duplicate records. I have millions of records in table_c so if i use distinct or Union ALL to remove duplicate records, my query takes very long time and this is not acceptable solution.

Can I right this query in such a way that it gives me unique records without performance issue.

Please note, this query is part of a sql view is used by external systems to get data.

Thanks in advance.

mardi 28 juin 2016

PostgreSQL does not accept column alias in WHERE clause

In this pgexercises about joining 3 different tables, the answer is given as following:

select mems.firstname || ' ' || mems.surname as member, 
    facs.name as facility, 
    case 
        when mems.memid = 0 then
            bks.slots*facs.guestcost
        else
            bks.slots*facs.membercost
    end as cost
        from
                cd.members mems                
                inner join cd.bookings bks
                        on mems.memid = bks.memid
                inner join cd.facilities facs
                        on bks.facid = facs.facid
        where
        bks.starttime >= '2012-09-14' and 
        bks.starttime < '2012-09-15' and (
            (mems.memid = 0 and bks.slots*facs.guestcost > 30) or
            (mems.memid != 0 and bks.slots*facs.membercost > 30)
        )
order by cost desc;

Why can't I refer to the cost alias in the SELECT list in the WHERE clause?
If I run the same query with:

        ...
        where
        bks.starttime >= '2012-09-14' and 
        bks.starttime < '2012-09-15' and
        cost > 30
order by cost desc;

an error occurs:

ERROR: column "cost" does not exist

It's clear with me from this answer that it's because of the order of evaluation. But why order by cost desc; is allowed?

Postgresql can not understand alias column

In this pgexercises about joining 3 different tables, the answer is given as following:

select mems.firstname || ' ' || mems.surname as member, 
    facs.name as facility, 
    case 
        when mems.memid = 0 then
            bks.slots*facs.guestcost
        else
            bks.slots*facs.membercost
    end as cost
        from
                cd.members mems                
                inner join cd.bookings bks
                        on mems.memid = bks.memid
                inner join cd.facilities facs
                        on bks.facid = facs.facid
        where
        bks.starttime >= '2012-09-14' and 
        bks.starttime < '2012-09-15' and (
            (mems.memid = 0 and bks.slots*facs.guestcost > 30) or
            (mems.memid != 0 and bks.slots*facs.membercost > 30)
        )
order by cost desc;

What I don't understand is that why can't I refer to the SELECT cost alias at the WHERE clause? If i run the same query with

        where
        bks.starttime >= '2012-09-14' and 
        bks.starttime < '2012-09-15' and
        cost > 30
order by cost desc;

then an error occurs:

ERROR: column "cost" does not exist

It's clear with me from this answer that it's because of the order of evaluation. But why order by cost desc; is allowed?

How to use javascript and HTML to add a sql and button?

I have an application where I want to ADD an AND button that, than creates the option to add an AND statement to the query. This is the php and html code I have to do this at the moment. The problem is I don't know how to connect the php part to the javascript part to create a button that adds exacte the same code?

This the html code:

This is the php code:

<?php

    include "connect.php";

    $table          = $_POST['tableSelected'];
    $field          = $_POST['fieldSelected'];
    $attribute      = $_POST['attributeSelected'];
    $operator       = $_POST['operatorSelected'];
    $fieldList      = $_POST['fieldList'];

    if (!empty($table)){

        if (!empty($fieldList)){
        $fieldstr = $fieldList . ",ST_AsGeoJSON(ST_Transform(l.geom,4326),6)";
        } else {
        $fieldstr = "";   
        }

        $pairs = [];

        foreach ($_POST['fieldSelected'] as $key => $field) {
            if (!empty($field) && !empty($_POST['operatorSelected'][$key]) && !empty($_POST['attributeSelected'][$key])) {
                $pairs[] = $field . " " . $_POST['operatorSelected'][$key] . " '" . $_POST['attributeSelected'][$key] . "'";
            }
        }

        if (count($pairs) > 0) {
            $sql .= ' WHERE ' . implode(' AND ', $pairs);
        }

        //echo ($sql);
?>

And this my html at the moment:

<select name="field[]">...</select>
<select name="operator[]">...</select>
<select name="value[]">...</select>

This is what I want: enter image description here

Error grouping rows with similar ids together

I am trying to group rows with similar ids (order_id in this case) together. I want each group of similar ids in a separate table. My presence query displays all rows in one table. I don't know how to go about it.

This is my code:

<div class='panel panel-body'>
          <table class='table table-condensed'>
              <thead>
                  <tr class='cart_menu'>
                      <td class='price'>Order ID</td>
                      <td class='price'>Item(s)</td>
                      <td class='price'>Quantity</td>
                  </tr>
              </thead>
              <tbody>
              ";

              $select = "SELECT DISTINCT a.*, b.* FROM shipping_order a JOIN shipping_details b ON b.order_id = a.order_id WHERE user_id = :user_id AND a.order_id = b.order_id";
              foreach ($db->query($select, array('user_id' => $id)) AS $items){

              echo"
              <tr>
                  <td class='cart_price'>
                      <h4>{$items['order_id']}</h4>
                  </td>
                  <td class='cart_price'>
                      <h4><a href=''>{$items['item']}</a></h4>
                      <!-- <p>Web ID: </p> -->
                  </td>
                  <td class='cart_price'>
                      <p>{$items['quantity']}</p>
                  </td>
              </tr>

              ";
              }
              echo"
              {$items['total']}
              </tbody>
          </table>
        </div>

C# wpf Sql Table, encrypted passwords

I am working with a application when user can login in the program by typing their information. Passwords are stored in table as encrypted. But when i try to login using this password it doesn't work.

For encrypting data I use this stored procedure

   ALTER procedure [dbo].[inn]
   @use varchar (50) = null,
   @ins varchar (50) = null

 AS


   INSERT INTO [kole].[dbo].[koll]
       ([Userr]
      ,[ins])


 VALUES
        (@use,
       ((EncryptByPassPhrase('8', @ins)))

And for decrypte I use this stored Procedure

  ALTER procedure [dbo].[sle]
 @Use varchar (50) = null,
 @ins varchar (50) = null

 AS
 SELECT Userr,CONVERT(varchar(50),DECRYPTBYPASSPHRASE ('8',ins)) as Password
FROM [kole].[dbo].[koll]
 where Userr = @Use and ins = @ins

   GO 

In c# I use this code for calling data, and log in into program.

              private void btn_Click(object sender, RoutedEventArgs e)
    {
        SqlConnection conn = new SqlConnection("Server = localhost;Database = kole; Integrated Security = true");

            SqlCommand cmd = new SqlCommand("sle", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Userr", txt.Text);
            cmd.Parameters.AddWithValue("@ins", psw.Password);
            conn.Open();
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapt.Fill(ds);
            conn.Close();
            int count = ds.Tables[0].Rows.Count;

            if (count == 0)
            {
                MessageBox.Show("This user don't exist");
                SystemSounds.Hand.Play();
                txt.Text = "";
                psw.Password = "";

            }
            else if (count == 1)
            {
                MessageBox.Show("Granted!");
                SystemSounds.Asterisk.Play();
                conn.Open();
    }

How can I decrypt the password in correct way , to use for login into program. Thanks